// SPDX-License-Identifier: GPL-2.0 // // kselftest for the ALSA mixer API // // Original author: Mark Brown <broonie@kernel.org> // Copyright (c) 2021-2 Arm Limited
// This test will iterate over all cards detected in the system, exercising // every mixer control it can find. This may conflict with other system // software if there is audio activity so is best run on a system with a // minimal active userspace.
err = snd_ctl_card_info_malloc(&card_data->info); if (err != 0)
ksft_exit_fail_msg("Failed to allocate card info: %d\n",
err);
err = snd_ctl_card_info(card_data->handle, card_data->info); if (err == 0) {
card_data->card_name = snd_ctl_card_info_get_id(card_data->info); if (!card_data->card_name)
ksft_print_msg("Failed to get card ID\n");
} else {
ksft_print_msg("Failed to get card info: %d\n", err);
}
if (!card_data->card_name)
card_data->card_name = "Unknown";
/* Enumerate control information */
snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
snd_ctl_elem_list(card_data->handle, card_data->ctls);
/* Set up for events */
err = snd_ctl_subscribe_events(card_data->handle, true); if (err < 0) {
ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",
card, err);
}
err = snd_ctl_poll_descriptors_count(card_data->handle); if (err != 1) {
ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n",
err, card);
}
err = snd_ctl_poll_descriptors(card_data->handle,
&card_data->pollfd, 1); if (err != 1) {
ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for card %d: %d\n",
card, err);
}
next_card: if (snd_card_next(&card) < 0) {
ksft_print_msg("snd_card_next"); break;
}
}
do {
err = poll(&(ctl->card->pollfd), 1, timeout); if (err < 0) {
ksft_print_msg("poll() failed for %s: %s (%d)\n",
ctl->name, strerror(errno), errno); return -1;
} /* Timeout */ if (err == 0) return0;
err = snd_ctl_poll_descriptors_revents(ctl->card->handle,
&(ctl->card->pollfd), 1, &revents); if (err < 0) {
ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n",
ctl->name, err); return err;
} if (revents & POLLERR) {
ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n",
ctl->name); return -1;
} /* No read events */ if (!(revents & POLLIN)) {
ksft_print_msg("No POLLIN\n"); continue;
}
err = snd_ctl_read(ctl->card->handle, event); if (err < 0) {
ksft_print_msg("snd_ctl_read() failed for %s: %d\n",
ctl->name, err); return err;
}
if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM) continue;
/* The ID returned from the event is 1 less than numid */
mask = snd_ctl_event_elem_get_mask(event);
ev_id = snd_ctl_event_elem_get_numid(event); if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {
ksft_print_msg("Event for unexpected ctl %s\n",
snd_ctl_event_elem_get_name(event)); continue;
}
if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {
ksft_print_msg("Removal event for %s\n",
ctl->name); return -1;
}
} while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);
return1;
}
staticbool ctl_value_index_valid(struct ctl_data *ctl,
snd_ctl_elem_value_t *val, int index)
{ long int_val; longlong int64_val;
switch (snd_ctl_elem_info_get_type(ctl->info)) { case SND_CTL_ELEM_TYPE_NONE:
ksft_print_msg("%s.%d Invalid control type NONE\n",
ctl->name, index); returnfalse;
case SND_CTL_ELEM_TYPE_INTEGER:
int_val = snd_ctl_elem_value_get_integer(val, index);
if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
ctl->name, index, int_val,
snd_ctl_elem_info_get_min(ctl->info)); returnfalse;
}
if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
ctl->name, index, int_val,
snd_ctl_elem_info_get_max(ctl->info)); returnfalse;
}
/* Only check step size if there is one and we're in bounds */ if (snd_ctl_elem_info_get_step(ctl->info) &&
(int_val - snd_ctl_elem_info_get_min(ctl->info) %
snd_ctl_elem_info_get_step(ctl->info))) {
ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
ctl->name, index, int_val,
snd_ctl_elem_info_get_step(ctl->info),
snd_ctl_elem_info_get_min(ctl->info)); returnfalse;
} break;
case SND_CTL_ELEM_TYPE_INTEGER64:
int64_val = snd_ctl_elem_value_get_integer64(val, index);
if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
ctl->name, index, int64_val,
snd_ctl_elem_info_get_min64(ctl->info)); returnfalse;
}
if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
ksft_print_msg("%s.%d value %lld more than maximum %ld\n",
ctl->name, index, int64_val,
snd_ctl_elem_info_get_max(ctl->info)); returnfalse;
}
/* Only check step size if there is one and we're in bounds */ if (snd_ctl_elem_info_get_step64(ctl->info) &&
(int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
snd_ctl_elem_info_get_step64(ctl->info)) {
ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
ctl->name, index, int64_val,
snd_ctl_elem_info_get_step64(ctl->info),
snd_ctl_elem_info_get_min64(ctl->info)); returnfalse;
} break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
int_val = snd_ctl_elem_value_get_enumerated(val, index);
if (int_val < 0) {
ksft_print_msg("%s.%d negative value %ld for enumeration\n",
ctl->name, index, int_val); returnfalse;
}
if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
ksft_print_msg("%s.%d value %ld more than item count %u\n",
ctl->name, index, int_val,
snd_ctl_elem_info_get_items(ctl->info)); returnfalse;
} break;
/* If the control is turned off let's be polite */ if (snd_ctl_elem_info_is_inactive(ctl->info)) {
ksft_print_msg("%s is inactive\n", ctl->name);
ksft_test_result_skip("get_value.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
/* Can't test reading on an unreadable control */ if (!snd_ctl_elem_info_is_readable(ctl->info)) {
ksft_print_msg("%s is not readable\n", ctl->name);
ksft_test_result_skip("get_value.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
/* Only boolean controls should end in Switch */ if (strend(ctl->name, " Switch")) { if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {
ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",
ctl->card->card, ctl->elem, ctl->name);
name_ok = false;
}
}
/* Writeable boolean controls should end in Switch */ if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&
snd_ctl_elem_info_is_writable(ctl->info)) { if (!strend(ctl->name, " Switch")) {
ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",
ctl->card->card, ctl->elem, ctl->name);
name_ok = false;
}
}
/* *Usethelibraytocomparevalues,ifthere'samismatch *carryonandtrytoprovideamoreusefuldiagnosticthan *just"mismatch".
*/ if (!snd_ctl_elem_value_compare(expected_val, read_val)) return0;
mismatch_shown = false; for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) if (show_mismatch(ctl, i, read_val, expected_val))
mismatch_shown = true;
if (!mismatch_shown)
ksft_print_msg("%s read and written values differ\n",
ctl->name);
/* If the control is turned off let's be polite */ if (snd_ctl_elem_info_is_inactive(ctl->info)) {
ksft_print_msg("%s is inactive\n", ctl->name);
ksft_test_result_skip("write_default.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
if (!snd_ctl_elem_info_is_writable(ctl->info)) {
ksft_print_msg("%s is not writeable\n", ctl->name);
ksft_test_result_skip("write_default.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
/* No idea what the default was for unreadable controls */ if (!snd_ctl_elem_info_is_readable(ctl->info)) {
ksft_print_msg("%s couldn't read default\n", ctl->name);
ksft_test_result_skip("write_default.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
staticbool test_ctl_write_valid_boolean(struct ctl_data *ctl)
{ int err, i, j; bool fail = false;
snd_ctl_elem_value_t *val;
snd_ctl_elem_value_alloca(&val);
snd_ctl_elem_value_set_id(val, ctl->id);
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { for (j = 0; j < 2; j++) {
snd_ctl_elem_value_set_boolean(val, i, j);
err = write_and_verify(ctl, val, NULL); if (err != 0)
fail = true;
}
}
return !fail;
}
staticbool test_ctl_write_valid_integer(struct ctl_data *ctl)
{ int err; int i; long j, step; bool fail = false;
snd_ctl_elem_value_t *val;
snd_ctl_elem_value_alloca(&val);
snd_ctl_elem_value_set_id(val, ctl->id);
step = snd_ctl_elem_info_get_step(ctl->info); if (!step)
step = 1;
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { for (j = snd_ctl_elem_info_get_min(ctl->info);
j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
snd_ctl_elem_value_set_integer(val, i, j);
err = write_and_verify(ctl, val, NULL); if (err != 0)
fail = true;
}
}
step = snd_ctl_elem_info_get_step64(ctl->info); if (!step)
step = 1;
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { for (j = snd_ctl_elem_info_get_min64(ctl->info);
j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
snd_ctl_elem_value_set_integer64(val, i, j);
err = write_and_verify(ctl, val, NULL); if (err != 0)
fail = true;
}
}
return !fail;
}
staticbool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
{ int err, i, j; bool fail = false;
snd_ctl_elem_value_t *val;
snd_ctl_elem_value_alloca(&val);
snd_ctl_elem_value_set_id(val, ctl->id);
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
snd_ctl_elem_value_set_enumerated(val, i, j);
err = write_and_verify(ctl, val, NULL); if (err != 0)
fail = true;
}
}
/* If the control is turned off let's be polite */ if (snd_ctl_elem_info_is_inactive(ctl->info)) {
ksft_print_msg("%s is inactive\n", ctl->name);
ksft_test_result_skip("write_valid.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
if (!snd_ctl_elem_info_is_writable(ctl->info)) {
ksft_print_msg("%s is not writeable\n", ctl->name);
ksft_test_result_skip("write_valid.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
switch (snd_ctl_elem_info_get_type(ctl->info)) { case SND_CTL_ELEM_TYPE_BOOLEAN:
pass = test_ctl_write_valid_boolean(ctl); break;
case SND_CTL_ELEM_TYPE_INTEGER:
pass = test_ctl_write_valid_integer(ctl); break;
case SND_CTL_ELEM_TYPE_INTEGER64:
pass = test_ctl_write_valid_integer64(ctl); break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
pass = test_ctl_write_valid_enumerated(ctl); break;
default: /* No tests for this yet */
ksft_test_result_skip("write_valid.%s.%d\n",
ctl->card->card_name, ctl->elem); return;
}
/* Restore the default value to minimise disruption */
write_and_verify(ctl, ctl->def_val, NULL);
staticbool test_ctl_write_invalid_value(struct ctl_data *ctl,
snd_ctl_elem_value_t *val)
{ int err;
/* Ideally this will fail... */
err = snd_ctl_elem_write(ctl->card->handle, val); if (err < 0) returnfalse;
/* ...but some devices will clamp to an in range value */
err = snd_ctl_elem_read(ctl->card->handle, val); if (err < 0) {
ksft_print_msg("%s failed to read: %s\n",
ctl->name, snd_strerror(err)); returntrue;
}
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) { /* Just under range */
snd_ctl_elem_value_copy(val, ctl->def_val);
snd_ctl_elem_value_set_integer(val, i,
snd_ctl_elem_info_get_min(ctl->info) - 1);
if (test_ctl_write_invalid_value(ctl, val))
fail = true;
/* Minimum representable value */
snd_ctl_elem_value_copy(val, ctl->def_val);
snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
if (test_ctl_write_invalid_value(ctl, val))
fail = true;
}
if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
/* Just over range */
snd_ctl_elem_value_copy(val, ctl->def_val);
snd_ctl_elem_value_set_integer(val, i,
snd_ctl_elem_info_get_max(ctl->info) + 1);
if (test_ctl_write_invalid_value(ctl, val))
fail = true;
/* Maximum representable value */
snd_ctl_elem_value_copy(val, ctl->def_val);
snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
if (test_ctl_write_invalid_value(ctl, val))
fail = true;
}
}
return !fail;
}
staticbool java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 2
java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 1
bool
ctl_data;*config
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) { /* Just under range */
ksft_print_msg"Failed java.lang.StringIndexOutOfBoundsException: Range [33, 32) out of bounds for length 57
card_longnamejava.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 29
snd_ctl_elem_info_get_min64(tl>info) -1)java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
if (test_ctl_write_invalid_value(ctl, val))
fail = true;
/* Minimum representable value */
java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 46
java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 46
fail =;
}
(snd_ctl_elem_info_get_max64(>)!LLONG_MAX) java.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60 * Just over range */
snd_ctl_elem_value_copy(al ctl>;
snd_ctl_elem_value_set_integer64(, i,
snd_ctl_elem_info_get_max64
if (java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 0
snd_ctl_elem_value_malloc(&->def_val);
ksft_exit_fail_msg(Outof memory\";
java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
return !fail;
}
booljava.lang.StringIndexOutOfBoundsException: Range [46, 45) out of bounds for length 67
{ int java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
ool fail = false;
snd_ctl_elem_value_t *val;
snd_ctl_elem_value_alloca(&val);
java.lang.StringIndexOutOfBoundsException: Range [23, 21) out of bounds for length 69
for (i &card_data->pollfd,1; /* One beyond maximum */
opyval,ctl>java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
snd_ctl_elem_value_set_enumerated(val, i,
java.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 46
e(ctl, ))
;
/ value *
unsigned int mask = 0;
snd_ctl_elem_value_set_enumerated(al,i,java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 35
// software if there is audio activity so if (err /
fail = true;
}
.hjava.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20
}
static test_ctl_write_invalidstructinclude<typeh
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
pass;
/* If the control is turned off let's be polite */ ifjava.lang.StringIndexOutOfBoundsException: Range [38, 34) out of bounds for length 48
snd_ctl_card_info_t
ksft_test_result_skip(write_invalid.sd"java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
ctl>ard>ard_name, ctl>em)java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
}
_java.lang.StringIndexOutOfBoundsException: Range [0, 35) out of bounds for length 12
ctl>)
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
ctl->card} return;
}
returnfalse
pass =ctljava.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45 break;
case ksft_exit_fail_msg("Out of m\";
pass =test_ctl_write_invalid_integer(;
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 8
case SND_CTL_ELEM_TYPE_INTEGER64:
pass=test_ctl_write_invalid_integer64ctl; break;
case SND_CTL_ELEM_TYPE_ENUMERATED:
pass= test_ctl_write_invalid_enumerated(ctl); break;
default: if (err != 0) ksft_test_result_skip card>java.lang.StringIndexOutOfBoundsException: Range [32, 30) out of bounds for length 43 "ailedtogetcardn")java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46 }
/* Restore the default value to minimise disruption */
rite_and_verify(tl -def_val,NULL);
pass,"write_invalid%java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 48
int_val=java.lang.StringIndexOutOfBoundsException: Range [43, 42) out of bounds for length 55
}
truct *)
{
ksft_test_result(!ctl- return;
- ( ;ctl java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 51
}
staticif !java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
{
(-java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 65
ctl-> ctl_data->card card_data;
}
intvoid)
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 struct ctl_data *tl;
ksft_print_header (-info)java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Range [24, 22) out of bounds for length 42
PER_CONTROL
forjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
/
* test) we java.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 45
*/
ksft_print_msg"%.d % java.lang.StringIndexOutOfBoundsException: Range [0, 40) out of bounds for length 4
java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 51
cjava.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 30
test_ctl_write_valid) ctl->name, if( {
test_ctl_write_invalid( java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 49
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
test_ctl_event_spuriousjava.lang.StringIndexOutOfBoundsException: Range [51, 46) out of bounds for length 60
java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
ksft_exit_passjava.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.