Path: blob/master/tools/testing/selftests/alsa/mixer-test.c
26285 views
// SPDX-License-Identifier: GPL-2.01//2// kselftest for the ALSA mixer API3//4// Original author: Mark Brown <[email protected]>5// Copyright (c) 2021-2 Arm Limited67// This test will iterate over all cards detected in the system, exercising8// every mixer control it can find. This may conflict with other system9// software if there is audio activity so is best run on a system with a10// minimal active userspace.1112#include <stdio.h>13#include <stdlib.h>14#include <stdbool.h>15#include <limits.h>16#include <string.h>17#include <getopt.h>18#include <stdarg.h>19#include <ctype.h>20#include <math.h>21#include <errno.h>22#include <assert.h>23#include <alsa/asoundlib.h>24#include <poll.h>25#include <stdint.h>2627#include "../kselftest.h"28#include "alsa-local.h"2930#define TESTS_PER_CONTROL 73132struct card_data {33snd_ctl_t *handle;34int card;35snd_ctl_card_info_t *info;36const char *card_name;37struct pollfd pollfd;38int num_ctls;39snd_ctl_elem_list_t *ctls;40struct card_data *next;41};4243struct ctl_data {44const char *name;45snd_ctl_elem_id_t *id;46snd_ctl_elem_info_t *info;47snd_ctl_elem_value_t *def_val;48int elem;49int event_missing;50int event_spurious;51struct card_data *card;52struct ctl_data *next;53};5455int num_cards = 0;56int num_controls = 0;57struct card_data *card_list = NULL;58struct ctl_data *ctl_list = NULL;5960static void find_controls(void)61{62char name[32];63int card, ctl, err;64struct card_data *card_data;65struct ctl_data *ctl_data;66snd_config_t *config;67char *card_name, *card_longname;6869card = -1;70if (snd_card_next(&card) < 0 || card < 0)71return;7273config = get_alsalib_config();7475while (card >= 0) {76sprintf(name, "hw:%d", card);7778card_data = malloc(sizeof(*card_data));79if (!card_data)80ksft_exit_fail_msg("Out of memory\n");8182err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);83if (err < 0) {84ksft_print_msg("Failed to get hctl for card %d: %s\n",85card, snd_strerror(err));86goto next_card;87}8889err = snd_card_get_name(card, &card_name);90if (err != 0)91card_name = "Unknown";92err = snd_card_get_longname(card, &card_longname);93if (err != 0)94card_longname = "Unknown";9596err = snd_ctl_card_info_malloc(&card_data->info);97if (err != 0)98ksft_exit_fail_msg("Failed to allocate card info: %d\n",99err);100101err = snd_ctl_card_info(card_data->handle, card_data->info);102if (err == 0) {103card_data->card_name = snd_ctl_card_info_get_id(card_data->info);104if (!card_data->card_name)105ksft_print_msg("Failed to get card ID\n");106} else {107ksft_print_msg("Failed to get card info: %d\n", err);108}109110if (!card_data->card_name)111card_data->card_name = "Unknown";112113ksft_print_msg("Card %d/%s - %s (%s)\n", card,114card_data->card_name, card_name, card_longname);115116/* Count controls */117snd_ctl_elem_list_malloc(&card_data->ctls);118snd_ctl_elem_list(card_data->handle, card_data->ctls);119card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);120121/* Enumerate control information */122snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);123snd_ctl_elem_list(card_data->handle, card_data->ctls);124125card_data->card = num_cards++;126card_data->next = card_list;127card_list = card_data;128129num_controls += card_data->num_ctls;130131for (ctl = 0; ctl < card_data->num_ctls; ctl++) {132ctl_data = malloc(sizeof(*ctl_data));133if (!ctl_data)134ksft_exit_fail_msg("Out of memory\n");135136memset(ctl_data, 0, sizeof(*ctl_data));137ctl_data->card = card_data;138ctl_data->elem = ctl;139ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,140ctl);141142err = snd_ctl_elem_id_malloc(&ctl_data->id);143if (err < 0)144ksft_exit_fail_msg("Out of memory\n");145146err = snd_ctl_elem_info_malloc(&ctl_data->info);147if (err < 0)148ksft_exit_fail_msg("Out of memory\n");149150err = snd_ctl_elem_value_malloc(&ctl_data->def_val);151if (err < 0)152ksft_exit_fail_msg("Out of memory\n");153154snd_ctl_elem_list_get_id(card_data->ctls, ctl,155ctl_data->id);156snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);157err = snd_ctl_elem_info(card_data->handle,158ctl_data->info);159if (err < 0) {160ksft_print_msg("%s getting info for %s\n",161snd_strerror(err),162ctl_data->name);163}164165snd_ctl_elem_value_set_id(ctl_data->def_val,166ctl_data->id);167168ctl_data->next = ctl_list;169ctl_list = ctl_data;170}171172/* Set up for events */173err = snd_ctl_subscribe_events(card_data->handle, true);174if (err < 0) {175ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",176card, err);177}178179err = snd_ctl_poll_descriptors_count(card_data->handle);180if (err != 1) {181ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n",182err, card);183}184185err = snd_ctl_poll_descriptors(card_data->handle,186&card_data->pollfd, 1);187if (err != 1) {188ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for card %d: %d\n",189card, err);190}191192next_card:193if (snd_card_next(&card) < 0) {194ksft_print_msg("snd_card_next");195break;196}197}198199snd_config_delete(config);200}201202/*203* Block for up to timeout ms for an event, returns a negative value204* on error, 0 for no event and 1 for an event.205*/206static int wait_for_event(struct ctl_data *ctl, int timeout)207{208unsigned short revents;209snd_ctl_event_t *event;210int err;211unsigned int mask = 0;212unsigned int ev_id;213214snd_ctl_event_alloca(&event);215216do {217err = poll(&(ctl->card->pollfd), 1, timeout);218if (err < 0) {219ksft_print_msg("poll() failed for %s: %s (%d)\n",220ctl->name, strerror(errno), errno);221return -1;222}223/* Timeout */224if (err == 0)225return 0;226227err = snd_ctl_poll_descriptors_revents(ctl->card->handle,228&(ctl->card->pollfd),2291, &revents);230if (err < 0) {231ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n",232ctl->name, err);233return err;234}235if (revents & POLLERR) {236ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n",237ctl->name);238return -1;239}240/* No read events */241if (!(revents & POLLIN)) {242ksft_print_msg("No POLLIN\n");243continue;244}245246err = snd_ctl_read(ctl->card->handle, event);247if (err < 0) {248ksft_print_msg("snd_ctl_read() failed for %s: %d\n",249ctl->name, err);250return err;251}252253if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)254continue;255256/* The ID returned from the event is 1 less than numid */257mask = snd_ctl_event_elem_get_mask(event);258ev_id = snd_ctl_event_elem_get_numid(event);259if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {260ksft_print_msg("Event for unexpected ctl %s\n",261snd_ctl_event_elem_get_name(event));262continue;263}264265if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {266ksft_print_msg("Removal event for %s\n",267ctl->name);268return -1;269}270} while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);271272return 1;273}274275static bool ctl_value_index_valid(struct ctl_data *ctl,276snd_ctl_elem_value_t *val,277int index)278{279long int_val;280long long int64_val;281282switch (snd_ctl_elem_info_get_type(ctl->info)) {283case SND_CTL_ELEM_TYPE_NONE:284ksft_print_msg("%s.%d Invalid control type NONE\n",285ctl->name, index);286return false;287288case SND_CTL_ELEM_TYPE_BOOLEAN:289int_val = snd_ctl_elem_value_get_boolean(val, index);290switch (int_val) {291case 0:292case 1:293break;294default:295ksft_print_msg("%s.%d Invalid boolean value %ld\n",296ctl->name, index, int_val);297return false;298}299break;300301case SND_CTL_ELEM_TYPE_INTEGER:302int_val = snd_ctl_elem_value_get_integer(val, index);303304if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {305ksft_print_msg("%s.%d value %ld less than minimum %ld\n",306ctl->name, index, int_val,307snd_ctl_elem_info_get_min(ctl->info));308return false;309}310311if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {312ksft_print_msg("%s.%d value %ld more than maximum %ld\n",313ctl->name, index, int_val,314snd_ctl_elem_info_get_max(ctl->info));315return false;316}317318/* Only check step size if there is one and we're in bounds */319if (snd_ctl_elem_info_get_step(ctl->info) &&320(int_val - snd_ctl_elem_info_get_min(ctl->info) %321snd_ctl_elem_info_get_step(ctl->info))) {322ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",323ctl->name, index, int_val,324snd_ctl_elem_info_get_step(ctl->info),325snd_ctl_elem_info_get_min(ctl->info));326return false;327}328break;329330case SND_CTL_ELEM_TYPE_INTEGER64:331int64_val = snd_ctl_elem_value_get_integer64(val, index);332333if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {334ksft_print_msg("%s.%d value %lld less than minimum %lld\n",335ctl->name, index, int64_val,336snd_ctl_elem_info_get_min64(ctl->info));337return false;338}339340if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {341ksft_print_msg("%s.%d value %lld more than maximum %ld\n",342ctl->name, index, int64_val,343snd_ctl_elem_info_get_max(ctl->info));344return false;345}346347/* Only check step size if there is one and we're in bounds */348if (snd_ctl_elem_info_get_step64(ctl->info) &&349(int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %350snd_ctl_elem_info_get_step64(ctl->info)) {351ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",352ctl->name, index, int64_val,353snd_ctl_elem_info_get_step64(ctl->info),354snd_ctl_elem_info_get_min64(ctl->info));355return false;356}357break;358359case SND_CTL_ELEM_TYPE_ENUMERATED:360int_val = snd_ctl_elem_value_get_enumerated(val, index);361362if (int_val < 0) {363ksft_print_msg("%s.%d negative value %ld for enumeration\n",364ctl->name, index, int_val);365return false;366}367368if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {369ksft_print_msg("%s.%d value %ld more than item count %u\n",370ctl->name, index, int_val,371snd_ctl_elem_info_get_items(ctl->info));372return false;373}374break;375376default:377/* No tests for other types */378break;379}380381return true;382}383384/*385* Check that the provided value meets the constraints for the386* provided control.387*/388static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)389{390int i;391bool valid = true;392393for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)394if (!ctl_value_index_valid(ctl, val, i))395valid = false;396397return valid;398}399400/*401* Check that we can read the default value and it is valid. Write402* tests use the read value to restore the default.403*/404static void test_ctl_get_value(struct ctl_data *ctl)405{406int err;407408/* If the control is turned off let's be polite */409if (snd_ctl_elem_info_is_inactive(ctl->info)) {410ksft_print_msg("%s is inactive\n", ctl->name);411ksft_test_result_skip("get_value.%s.%d\n",412ctl->card->card_name, ctl->elem);413return;414}415416/* Can't test reading on an unreadable control */417if (!snd_ctl_elem_info_is_readable(ctl->info)) {418ksft_print_msg("%s is not readable\n", ctl->name);419ksft_test_result_skip("get_value.%s.%d\n",420ctl->card->card_name, ctl->elem);421return;422}423424err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);425if (err < 0) {426ksft_print_msg("snd_ctl_elem_read() failed: %s\n",427snd_strerror(err));428goto out;429}430431if (!ctl_value_valid(ctl, ctl->def_val))432err = -EINVAL;433434out:435ksft_test_result(err >= 0, "get_value.%s.%d\n",436ctl->card->card_name, ctl->elem);437}438439static bool strend(const char *haystack, const char *needle)440{441size_t haystack_len = strlen(haystack);442size_t needle_len = strlen(needle);443444if (needle_len > haystack_len)445return false;446return strcmp(haystack + haystack_len - needle_len, needle) == 0;447}448449static void test_ctl_name(struct ctl_data *ctl)450{451bool name_ok = true;452453ksft_print_msg("%s.%d %s\n", ctl->card->card_name, ctl->elem,454ctl->name);455456/* Only boolean controls should end in Switch */457if (strend(ctl->name, " Switch")) {458if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {459ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",460ctl->card->card, ctl->elem, ctl->name);461name_ok = false;462}463}464465/* Writeable boolean controls should end in Switch */466if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&467snd_ctl_elem_info_is_writable(ctl->info)) {468if (!strend(ctl->name, " Switch")) {469ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",470ctl->card->card, ctl->elem, ctl->name);471name_ok = false;472}473}474475ksft_test_result(name_ok, "name.%s.%d\n",476ctl->card->card_name, ctl->elem);477}478479static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val,480snd_ctl_elem_value_t *read_val)481{482long long orig_int, read_int;483int i;484485for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {486switch (snd_ctl_elem_info_get_type(ctl->info)) {487case SND_CTL_ELEM_TYPE_BOOLEAN:488orig_int = snd_ctl_elem_value_get_boolean(orig_val, i);489read_int = snd_ctl_elem_value_get_boolean(read_val, i);490break;491492case SND_CTL_ELEM_TYPE_INTEGER:493orig_int = snd_ctl_elem_value_get_integer(orig_val, i);494read_int = snd_ctl_elem_value_get_integer(read_val, i);495break;496497case SND_CTL_ELEM_TYPE_INTEGER64:498orig_int = snd_ctl_elem_value_get_integer64(orig_val,499i);500read_int = snd_ctl_elem_value_get_integer64(read_val,501i);502break;503504case SND_CTL_ELEM_TYPE_ENUMERATED:505orig_int = snd_ctl_elem_value_get_enumerated(orig_val,506i);507read_int = snd_ctl_elem_value_get_enumerated(read_val,508i);509break;510511default:512return;513}514515ksft_print_msg("%s.%d orig %lld read %lld, is_volatile %d\n",516ctl->name, i, orig_int, read_int,517snd_ctl_elem_info_is_volatile(ctl->info));518}519}520521static bool show_mismatch(struct ctl_data *ctl, int index,522snd_ctl_elem_value_t *read_val,523snd_ctl_elem_value_t *expected_val)524{525long long expected_int, read_int;526527/*528* We factor out the code to compare values representable as529* integers, ensure that check doesn't log otherwise.530*/531expected_int = 0;532read_int = 0;533534switch (snd_ctl_elem_info_get_type(ctl->info)) {535case SND_CTL_ELEM_TYPE_BOOLEAN:536expected_int = snd_ctl_elem_value_get_boolean(expected_val,537index);538read_int = snd_ctl_elem_value_get_boolean(read_val, index);539break;540541case SND_CTL_ELEM_TYPE_INTEGER:542expected_int = snd_ctl_elem_value_get_integer(expected_val,543index);544read_int = snd_ctl_elem_value_get_integer(read_val, index);545break;546547case SND_CTL_ELEM_TYPE_INTEGER64:548expected_int = snd_ctl_elem_value_get_integer64(expected_val,549index);550read_int = snd_ctl_elem_value_get_integer64(read_val,551index);552break;553554case SND_CTL_ELEM_TYPE_ENUMERATED:555expected_int = snd_ctl_elem_value_get_enumerated(expected_val,556index);557read_int = snd_ctl_elem_value_get_enumerated(read_val,558index);559break;560561default:562break;563}564565if (expected_int != read_int) {566/*567* NOTE: The volatile attribute means that the hardware568* can voluntarily change the state of control element569* independent of any operation by software.570*/571bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);572ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",573ctl->name, index, expected_int, read_int, is_volatile);574return !is_volatile;575} else {576return false;577}578}579580/*581* Write a value then if possible verify that we get the expected582* result. An optional expected value can be provided if we expect583* the write to fail, for verifying that invalid writes don't corrupt584* anything.585*/586static int write_and_verify(struct ctl_data *ctl,587snd_ctl_elem_value_t *write_val,588snd_ctl_elem_value_t *expected_val)589{590int err, i;591bool error_expected, mismatch_shown;592snd_ctl_elem_value_t *initial_val, *read_val, *w_val;593snd_ctl_elem_value_alloca(&initial_val);594snd_ctl_elem_value_alloca(&read_val);595snd_ctl_elem_value_alloca(&w_val);596597/*598* We need to copy the write value since writing can modify599* the value which causes surprises, and allocate an expected600* value if we expect to read back what we wrote.601*/602snd_ctl_elem_value_copy(w_val, write_val);603if (expected_val) {604error_expected = true;605} else {606error_expected = false;607snd_ctl_elem_value_alloca(&expected_val);608snd_ctl_elem_value_copy(expected_val, write_val);609}610611/* Store the value before we write */612if (snd_ctl_elem_info_is_readable(ctl->info)) {613snd_ctl_elem_value_set_id(initial_val, ctl->id);614615err = snd_ctl_elem_read(ctl->card->handle, initial_val);616if (err < 0) {617ksft_print_msg("snd_ctl_elem_read() failed: %s\n",618snd_strerror(err));619return err;620}621}622623/*624* Do the write, if we have an expected value ignore the error625* and carry on to validate the expected value.626*/627err = snd_ctl_elem_write(ctl->card->handle, w_val);628if (err < 0 && !error_expected) {629ksft_print_msg("snd_ctl_elem_write() failed: %s\n",630snd_strerror(err));631return err;632}633634/* Can we do the verification part? */635if (!snd_ctl_elem_info_is_readable(ctl->info))636return err;637638snd_ctl_elem_value_set_id(read_val, ctl->id);639640err = snd_ctl_elem_read(ctl->card->handle, read_val);641if (err < 0) {642ksft_print_msg("snd_ctl_elem_read() failed: %s\n",643snd_strerror(err));644return err;645}646647/*648* We can't verify any specific value for volatile controls649* but we should still check that whatever we read is a valid650* vale for the control.651*/652if (snd_ctl_elem_info_is_volatile(ctl->info)) {653if (!ctl_value_valid(ctl, read_val)) {654ksft_print_msg("Volatile control %s has invalid value\n",655ctl->name);656return -EINVAL;657}658659return 0;660}661662/*663* Check for an event if the value changed, or confirm that664* there was none if it didn't. We rely on the kernel665* generating the notification before it returns from the666* write, this is currently true, should that ever change this667* will most likely break and need updating.668*/669err = wait_for_event(ctl, 0);670if (snd_ctl_elem_value_compare(initial_val, read_val)) {671if (err < 1) {672ksft_print_msg("No event generated for %s\n",673ctl->name);674show_values(ctl, initial_val, read_val);675ctl->event_missing++;676}677} else {678if (err != 0) {679ksft_print_msg("Spurious event generated for %s\n",680ctl->name);681show_values(ctl, initial_val, read_val);682ctl->event_spurious++;683}684}685686/*687* Use the libray to compare values, if there's a mismatch688* carry on and try to provide a more useful diagnostic than689* just "mismatch".690*/691if (!snd_ctl_elem_value_compare(expected_val, read_val))692return 0;693694mismatch_shown = false;695for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)696if (show_mismatch(ctl, i, read_val, expected_val))697mismatch_shown = true;698699if (!mismatch_shown)700ksft_print_msg("%s read and written values differ\n",701ctl->name);702703return -1;704}705706/*707* Make sure we can write the default value back to the control, this708* should validate that at least some write works.709*/710static void test_ctl_write_default(struct ctl_data *ctl)711{712int err;713714/* If the control is turned off let's be polite */715if (snd_ctl_elem_info_is_inactive(ctl->info)) {716ksft_print_msg("%s is inactive\n", ctl->name);717ksft_test_result_skip("write_default.%s.%d\n",718ctl->card->card_name, ctl->elem);719return;720}721722if (!snd_ctl_elem_info_is_writable(ctl->info)) {723ksft_print_msg("%s is not writeable\n", ctl->name);724ksft_test_result_skip("write_default.%s.%d\n",725ctl->card->card_name, ctl->elem);726return;727}728729/* No idea what the default was for unreadable controls */730if (!snd_ctl_elem_info_is_readable(ctl->info)) {731ksft_print_msg("%s couldn't read default\n", ctl->name);732ksft_test_result_skip("write_default.%s.%d\n",733ctl->card->card_name, ctl->elem);734return;735}736737err = write_and_verify(ctl, ctl->def_val, NULL);738739ksft_test_result(err >= 0, "write_default.%s.%d\n",740ctl->card->card_name, ctl->elem);741}742743static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)744{745int err, i, j;746bool fail = false;747snd_ctl_elem_value_t *val;748snd_ctl_elem_value_alloca(&val);749750snd_ctl_elem_value_set_id(val, ctl->id);751752for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {753for (j = 0; j < 2; j++) {754snd_ctl_elem_value_set_boolean(val, i, j);755err = write_and_verify(ctl, val, NULL);756if (err != 0)757fail = true;758}759}760761return !fail;762}763764static bool test_ctl_write_valid_integer(struct ctl_data *ctl)765{766int err;767int i;768long j, step;769bool fail = false;770snd_ctl_elem_value_t *val;771snd_ctl_elem_value_alloca(&val);772773snd_ctl_elem_value_set_id(val, ctl->id);774775step = snd_ctl_elem_info_get_step(ctl->info);776if (!step)777step = 1;778779for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {780for (j = snd_ctl_elem_info_get_min(ctl->info);781j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {782783snd_ctl_elem_value_set_integer(val, i, j);784err = write_and_verify(ctl, val, NULL);785if (err != 0)786fail = true;787}788}789790791return !fail;792}793794static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)795{796int err, i;797long long j, step;798bool fail = false;799snd_ctl_elem_value_t *val;800snd_ctl_elem_value_alloca(&val);801802snd_ctl_elem_value_set_id(val, ctl->id);803804step = snd_ctl_elem_info_get_step64(ctl->info);805if (!step)806step = 1;807808for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {809for (j = snd_ctl_elem_info_get_min64(ctl->info);810j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {811812snd_ctl_elem_value_set_integer64(val, i, j);813err = write_and_verify(ctl, val, NULL);814if (err != 0)815fail = true;816}817}818819return !fail;820}821822static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)823{824int err, i, j;825bool fail = false;826snd_ctl_elem_value_t *val;827snd_ctl_elem_value_alloca(&val);828829snd_ctl_elem_value_set_id(val, ctl->id);830831for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {832for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {833snd_ctl_elem_value_set_enumerated(val, i, j);834err = write_and_verify(ctl, val, NULL);835if (err != 0)836fail = true;837}838}839840return !fail;841}842843static void test_ctl_write_valid(struct ctl_data *ctl)844{845bool pass;846847/* If the control is turned off let's be polite */848if (snd_ctl_elem_info_is_inactive(ctl->info)) {849ksft_print_msg("%s is inactive\n", ctl->name);850ksft_test_result_skip("write_valid.%s.%d\n",851ctl->card->card_name, ctl->elem);852return;853}854855if (!snd_ctl_elem_info_is_writable(ctl->info)) {856ksft_print_msg("%s is not writeable\n", ctl->name);857ksft_test_result_skip("write_valid.%s.%d\n",858ctl->card->card_name, ctl->elem);859return;860}861862switch (snd_ctl_elem_info_get_type(ctl->info)) {863case SND_CTL_ELEM_TYPE_BOOLEAN:864pass = test_ctl_write_valid_boolean(ctl);865break;866867case SND_CTL_ELEM_TYPE_INTEGER:868pass = test_ctl_write_valid_integer(ctl);869break;870871case SND_CTL_ELEM_TYPE_INTEGER64:872pass = test_ctl_write_valid_integer64(ctl);873break;874875case SND_CTL_ELEM_TYPE_ENUMERATED:876pass = test_ctl_write_valid_enumerated(ctl);877break;878879default:880/* No tests for this yet */881ksft_test_result_skip("write_valid.%s.%d\n",882ctl->card->card_name, ctl->elem);883return;884}885886/* Restore the default value to minimise disruption */887write_and_verify(ctl, ctl->def_val, NULL);888889ksft_test_result(pass, "write_valid.%s.%d\n",890ctl->card->card_name, ctl->elem);891}892893static bool test_ctl_write_invalid_value(struct ctl_data *ctl,894snd_ctl_elem_value_t *val)895{896int err;897898/* Ideally this will fail... */899err = snd_ctl_elem_write(ctl->card->handle, val);900if (err < 0)901return false;902903/* ...but some devices will clamp to an in range value */904err = snd_ctl_elem_read(ctl->card->handle, val);905if (err < 0) {906ksft_print_msg("%s failed to read: %s\n",907ctl->name, snd_strerror(err));908return true;909}910911return !ctl_value_valid(ctl, val);912}913914static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)915{916int i;917bool fail = false;918snd_ctl_elem_value_t *val;919snd_ctl_elem_value_alloca(&val);920921for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {922snd_ctl_elem_value_copy(val, ctl->def_val);923snd_ctl_elem_value_set_boolean(val, i, 2);924925if (test_ctl_write_invalid_value(ctl, val))926fail = true;927}928929return !fail;930}931932static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)933{934int i;935bool fail = false;936snd_ctl_elem_value_t *val;937snd_ctl_elem_value_alloca(&val);938939for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {940if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {941/* Just under range */942snd_ctl_elem_value_copy(val, ctl->def_val);943snd_ctl_elem_value_set_integer(val, i,944snd_ctl_elem_info_get_min(ctl->info) - 1);945946if (test_ctl_write_invalid_value(ctl, val))947fail = true;948949/* Minimum representable value */950snd_ctl_elem_value_copy(val, ctl->def_val);951snd_ctl_elem_value_set_integer(val, i, LONG_MIN);952953if (test_ctl_write_invalid_value(ctl, val))954fail = true;955}956957if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {958/* Just over range */959snd_ctl_elem_value_copy(val, ctl->def_val);960snd_ctl_elem_value_set_integer(val, i,961snd_ctl_elem_info_get_max(ctl->info) + 1);962963if (test_ctl_write_invalid_value(ctl, val))964fail = true;965966/* Maximum representable value */967snd_ctl_elem_value_copy(val, ctl->def_val);968snd_ctl_elem_value_set_integer(val, i, LONG_MAX);969970if (test_ctl_write_invalid_value(ctl, val))971fail = true;972}973}974975return !fail;976}977978static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)979{980int i;981bool fail = false;982snd_ctl_elem_value_t *val;983snd_ctl_elem_value_alloca(&val);984985for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {986if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {987/* Just under range */988snd_ctl_elem_value_copy(val, ctl->def_val);989snd_ctl_elem_value_set_integer64(val, i,990snd_ctl_elem_info_get_min64(ctl->info) - 1);991992if (test_ctl_write_invalid_value(ctl, val))993fail = true;994995/* Minimum representable value */996snd_ctl_elem_value_copy(val, ctl->def_val);997snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);998999if (test_ctl_write_invalid_value(ctl, val))1000fail = true;1001}10021003if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {1004/* Just over range */1005snd_ctl_elem_value_copy(val, ctl->def_val);1006snd_ctl_elem_value_set_integer64(val, i,1007snd_ctl_elem_info_get_max64(ctl->info) + 1);10081009if (test_ctl_write_invalid_value(ctl, val))1010fail = true;10111012/* Maximum representable value */1013snd_ctl_elem_value_copy(val, ctl->def_val);1014snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);10151016if (test_ctl_write_invalid_value(ctl, val))1017fail = true;1018}1019}10201021return !fail;1022}10231024static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)1025{1026int i;1027bool fail = false;1028snd_ctl_elem_value_t *val;1029snd_ctl_elem_value_alloca(&val);10301031snd_ctl_elem_value_set_id(val, ctl->id);10321033for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {1034/* One beyond maximum */1035snd_ctl_elem_value_copy(val, ctl->def_val);1036snd_ctl_elem_value_set_enumerated(val, i,1037snd_ctl_elem_info_get_items(ctl->info));10381039if (test_ctl_write_invalid_value(ctl, val))1040fail = true;10411042/* Maximum representable value */1043snd_ctl_elem_value_copy(val, ctl->def_val);1044snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);10451046if (test_ctl_write_invalid_value(ctl, val))1047fail = true;10481049}10501051return !fail;1052}105310541055static void test_ctl_write_invalid(struct ctl_data *ctl)1056{1057bool pass;10581059/* If the control is turned off let's be polite */1060if (snd_ctl_elem_info_is_inactive(ctl->info)) {1061ksft_print_msg("%s is inactive\n", ctl->name);1062ksft_test_result_skip("write_invalid.%s.%d\n",1063ctl->card->card_name, ctl->elem);1064return;1065}10661067if (!snd_ctl_elem_info_is_writable(ctl->info)) {1068ksft_print_msg("%s is not writeable\n", ctl->name);1069ksft_test_result_skip("write_invalid.%s.%d\n",1070ctl->card->card_name, ctl->elem);1071return;1072}10731074switch (snd_ctl_elem_info_get_type(ctl->info)) {1075case SND_CTL_ELEM_TYPE_BOOLEAN:1076pass = test_ctl_write_invalid_boolean(ctl);1077break;10781079case SND_CTL_ELEM_TYPE_INTEGER:1080pass = test_ctl_write_invalid_integer(ctl);1081break;10821083case SND_CTL_ELEM_TYPE_INTEGER64:1084pass = test_ctl_write_invalid_integer64(ctl);1085break;10861087case SND_CTL_ELEM_TYPE_ENUMERATED:1088pass = test_ctl_write_invalid_enumerated(ctl);1089break;10901091default:1092/* No tests for this yet */1093ksft_test_result_skip("write_invalid.%s.%d\n",1094ctl->card->card_name, ctl->elem);1095return;1096}10971098/* Restore the default value to minimise disruption */1099write_and_verify(ctl, ctl->def_val, NULL);11001101ksft_test_result(pass, "write_invalid.%s.%d\n",1102ctl->card->card_name, ctl->elem);1103}11041105static void test_ctl_event_missing(struct ctl_data *ctl)1106{1107ksft_test_result(!ctl->event_missing, "event_missing.%s.%d\n",1108ctl->card->card_name, ctl->elem);1109}11101111static void test_ctl_event_spurious(struct ctl_data *ctl)1112{1113ksft_test_result(!ctl->event_spurious, "event_spurious.%s.%d\n",1114ctl->card->card_name, ctl->elem);1115}11161117int main(void)1118{1119struct ctl_data *ctl;11201121ksft_print_header();11221123find_controls();11241125ksft_set_plan(num_controls * TESTS_PER_CONTROL);11261127for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {1128/*1129* Must test get_value() before we write anything, the1130* test stores the default value for later cleanup.1131*/1132test_ctl_get_value(ctl);1133test_ctl_name(ctl);1134test_ctl_write_default(ctl);1135test_ctl_write_valid(ctl);1136test_ctl_write_invalid(ctl);1137test_ctl_event_missing(ctl);1138test_ctl_event_spurious(ctl);1139}11401141ksft_exit_pass();11421143return 0;1144}114511461147