1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // kselftest for the ALSA mixer API 4 // 5 // Original author: Mark Brown <broonie@kernel.org> 6 // Copyright (c) 2021-2 Arm Limited 7 8 // This test will iterate over all cards detected in the system, exercising 9 // every mixer control it can find. This may conflict with other system 10 // software if there is audio activity so is best run on a system with a 11 // minimal active userspace. 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <stdbool.h> 16 #include <limits.h> 17 #include <string.h> 18 #include <getopt.h> 19 #include <stdarg.h> 20 #include <ctype.h> 21 #include <math.h> 22 #include <errno.h> 23 #include <assert.h> 24 #include <alsa/asoundlib.h> 25 #include <poll.h> 26 #include <stdint.h> 27 28 #include "kselftest.h" 29 #include "alsa-local.h" 30 31 #define TESTS_PER_CONTROL 8 32 33 /* Suffixes of the SNDRV_CTL_NAME_IEC958() names, not exported to userspace */ 34 #define IEC958_DEFAULT "Default" 35 #define IEC958_CON_MASK "Con Mask" 36 #define IEC958_PRO_MASK "Pro Mask" 37 #define IEC958_MASK "Mask" 38 39 struct card_data { 40 snd_ctl_t *handle; 41 int card; 42 snd_ctl_card_info_t *info; 43 const char *card_name; 44 struct pollfd pollfd; 45 int num_ctls; 46 snd_ctl_elem_list_t *ctls; 47 struct card_data *next; 48 }; 49 50 struct ctl_data { 51 const char *name; 52 snd_ctl_elem_id_t *id; 53 snd_ctl_elem_info_t *info; 54 snd_ctl_elem_value_t *def_val; 55 snd_ctl_elem_value_t *snapshot; 56 bool snapshot_valid; 57 bool moved; 58 int elem; 59 int event_missing; 60 int event_spurious; 61 int side_effects; 62 unsigned int ev_mask; 63 struct card_data *card; 64 struct ctl_data *next; 65 }; 66 67 int num_cards; 68 int num_controls; 69 struct card_data *card_list; 70 struct ctl_data *ctl_list; 71 72 static void find_controls(void) 73 { 74 char name[32]; 75 int card, ctl, err; 76 struct card_data *card_data; 77 struct ctl_data *ctl_data; 78 snd_config_t *config; 79 char *card_name, *card_longname; 80 81 card = -1; 82 if (snd_card_next(&card) < 0 || card < 0) 83 return; 84 85 config = get_alsalib_config(); 86 87 while (card >= 0) { 88 sprintf(name, "hw:%d", card); 89 90 card_data = malloc(sizeof(*card_data)); 91 if (!card_data) 92 ksft_exit_fail_msg("Out of memory\n"); 93 94 err = snd_ctl_open_lconf(&card_data->handle, name, 0, config); 95 if (err < 0) { 96 ksft_print_msg("Failed to get hctl for card %d: %s\n", 97 card, snd_strerror(err)); 98 free(card_data); 99 goto next_card; 100 } 101 102 err = snd_card_get_name(card, &card_name); 103 if (err != 0) 104 card_name = "Unknown"; 105 err = snd_card_get_longname(card, &card_longname); 106 if (err != 0) 107 card_longname = "Unknown"; 108 109 err = snd_ctl_card_info_malloc(&card_data->info); 110 if (err != 0) 111 ksft_exit_fail_msg("Failed to allocate card info: %d\n", 112 err); 113 114 err = snd_ctl_card_info(card_data->handle, card_data->info); 115 if (err == 0) { 116 card_data->card_name = snd_ctl_card_info_get_id(card_data->info); 117 if (!card_data->card_name) 118 ksft_print_msg("Failed to get card ID\n"); 119 } else { 120 ksft_print_msg("Failed to get card info: %d\n", err); 121 } 122 123 if (!card_data->card_name) 124 card_data->card_name = "Unknown"; 125 126 ksft_print_msg("Card %d/%s - %s (%s)\n", card, 127 card_data->card_name, card_name, card_longname); 128 129 /* Count controls */ 130 snd_ctl_elem_list_malloc(&card_data->ctls); 131 snd_ctl_elem_list(card_data->handle, card_data->ctls); 132 card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls); 133 134 /* Enumerate control information */ 135 snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls); 136 snd_ctl_elem_list(card_data->handle, card_data->ctls); 137 138 card_data->card = num_cards++; 139 card_data->next = card_list; 140 card_list = card_data; 141 142 num_controls += card_data->num_ctls; 143 144 for (ctl = 0; ctl < card_data->num_ctls; ctl++) { 145 ctl_data = malloc(sizeof(*ctl_data)); 146 if (!ctl_data) 147 ksft_exit_fail_msg("Out of memory\n"); 148 149 memset(ctl_data, 0, sizeof(*ctl_data)); 150 ctl_data->card = card_data; 151 ctl_data->elem = ctl; 152 ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls, 153 ctl); 154 155 err = snd_ctl_elem_id_malloc(&ctl_data->id); 156 if (err < 0) 157 ksft_exit_fail_msg("Out of memory\n"); 158 159 err = snd_ctl_elem_info_malloc(&ctl_data->info); 160 if (err < 0) 161 ksft_exit_fail_msg("Out of memory\n"); 162 163 err = snd_ctl_elem_value_malloc(&ctl_data->def_val); 164 if (err < 0) 165 ksft_exit_fail_msg("Out of memory\n"); 166 167 err = snd_ctl_elem_value_malloc(&ctl_data->snapshot); 168 if (err < 0) 169 ksft_exit_fail_msg("Out of memory\n"); 170 171 snd_ctl_elem_list_get_id(card_data->ctls, ctl, 172 ctl_data->id); 173 snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id); 174 err = snd_ctl_elem_info(card_data->handle, 175 ctl_data->info); 176 if (err < 0) { 177 ksft_print_msg("%s getting info for %s\n", 178 snd_strerror(err), 179 ctl_data->name); 180 } 181 182 snd_ctl_elem_value_set_id(ctl_data->def_val, 183 ctl_data->id); 184 185 ctl_data->next = ctl_list; 186 ctl_list = ctl_data; 187 } 188 189 /* Set up for events */ 190 err = snd_ctl_subscribe_events(card_data->handle, true); 191 if (err < 0) { 192 ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n", 193 card, err); 194 } 195 196 err = snd_ctl_poll_descriptors_count(card_data->handle); 197 if (err != 1) { 198 ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n", 199 err, card); 200 } 201 202 err = snd_ctl_poll_descriptors(card_data->handle, 203 &card_data->pollfd, 1); 204 if (err != 1) { 205 ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for card %d: %d\n", 206 card, err); 207 } 208 209 next_card: 210 if (snd_card_next(&card) < 0) { 211 ksft_print_msg("snd_card_next"); 212 break; 213 } 214 } 215 216 snd_config_delete(config); 217 } 218 219 /* The control on the same card that an event's numid refers to */ 220 static struct ctl_data *find_ctl_by_numid(struct card_data *card, 221 unsigned int numid) 222 { 223 struct ctl_data *ctl; 224 225 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) 226 if (ctl->card == card && 227 snd_ctl_elem_info_get_numid(ctl->info) == numid) 228 return ctl; 229 230 return NULL; 231 } 232 233 /* 234 * Block for up to timeout ms for an event, returns a negative value 235 * on error, 0 for no event and 1 for an event. 236 */ 237 static int wait_for_event(struct ctl_data *ctl, int timeout) 238 { 239 unsigned short revents; 240 snd_ctl_event_t *event; 241 int err; 242 unsigned int mask = 0; 243 unsigned int ev_id; 244 245 snd_ctl_event_alloca(&event); 246 247 do { 248 err = poll(&(ctl->card->pollfd), 1, timeout); 249 if (err < 0) { 250 ksft_print_msg("poll() failed for %s: %s (%d)\n", 251 ctl->name, strerror(errno), errno); 252 return -1; 253 } 254 /* Timeout */ 255 if (err == 0) 256 return 0; 257 258 err = snd_ctl_poll_descriptors_revents(ctl->card->handle, 259 &(ctl->card->pollfd), 260 1, &revents); 261 if (err < 0) { 262 ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n", 263 ctl->name, err); 264 return err; 265 } 266 if (revents & POLLERR) { 267 ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n", 268 ctl->name); 269 return -1; 270 } 271 /* No read events */ 272 if (!(revents & POLLIN)) { 273 ksft_print_msg("No POLLIN\n"); 274 continue; 275 } 276 277 err = snd_ctl_read(ctl->card->handle, event); 278 if (err < 0) { 279 ksft_print_msg("snd_ctl_read() failed for %s: %d\n", 280 ctl->name, err); 281 return err; 282 } 283 284 if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM) 285 continue; 286 287 /* The ID returned from the event is 1 less than numid */ 288 mask = snd_ctl_event_elem_get_mask(event); 289 ev_id = snd_ctl_event_elem_get_numid(event); 290 if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) { 291 struct ctl_data *other = find_ctl_by_numid(ctl->card, 292 ev_id); 293 294 /* 295 * Remember that the driver announced this one. 296 * test_ctl_write_side_effects() uses that to tell a 297 * deliberate link from a silent register collision. 298 */ 299 if (other) 300 other->ev_mask |= mask; 301 302 ksft_print_msg("Event for unexpected ctl %s\n", 303 snd_ctl_event_elem_get_name(event)); 304 /* The loop condition must not see the other control's mask */ 305 mask = 0; 306 continue; 307 } 308 309 if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) { 310 ksft_print_msg("Removal event for %s\n", 311 ctl->name); 312 return -1; 313 } 314 } while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE); 315 316 return 1; 317 } 318 319 static bool ctl_value_index_valid(struct ctl_data *ctl, 320 snd_ctl_elem_value_t *val, 321 int index) 322 { 323 long int_val; 324 long long int64_val; 325 326 switch (snd_ctl_elem_info_get_type(ctl->info)) { 327 case SND_CTL_ELEM_TYPE_NONE: 328 ksft_print_msg("%s.%d Invalid control type NONE\n", 329 ctl->name, index); 330 return false; 331 332 case SND_CTL_ELEM_TYPE_BOOLEAN: 333 int_val = snd_ctl_elem_value_get_boolean(val, index); 334 switch (int_val) { 335 case 0: 336 case 1: 337 break; 338 default: 339 ksft_print_msg("%s.%d Invalid boolean value %ld\n", 340 ctl->name, index, int_val); 341 return false; 342 } 343 break; 344 345 case SND_CTL_ELEM_TYPE_INTEGER: 346 int_val = snd_ctl_elem_value_get_integer(val, index); 347 348 if (int_val < snd_ctl_elem_info_get_min(ctl->info)) { 349 ksft_print_msg("%s.%d value %ld less than minimum %ld\n", 350 ctl->name, index, int_val, 351 snd_ctl_elem_info_get_min(ctl->info)); 352 return false; 353 } 354 355 if (int_val > snd_ctl_elem_info_get_max(ctl->info)) { 356 ksft_print_msg("%s.%d value %ld more than maximum %ld\n", 357 ctl->name, index, int_val, 358 snd_ctl_elem_info_get_max(ctl->info)); 359 return false; 360 } 361 362 /* Only check step size if there is one and we're in bounds */ 363 if (snd_ctl_elem_info_get_step(ctl->info) && 364 (int_val - snd_ctl_elem_info_get_min(ctl->info)) % 365 snd_ctl_elem_info_get_step(ctl->info)) { 366 ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n", 367 ctl->name, index, int_val, 368 snd_ctl_elem_info_get_step(ctl->info), 369 snd_ctl_elem_info_get_min(ctl->info)); 370 return false; 371 } 372 break; 373 374 case SND_CTL_ELEM_TYPE_INTEGER64: 375 int64_val = snd_ctl_elem_value_get_integer64(val, index); 376 377 if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) { 378 ksft_print_msg("%s.%d value %lld less than minimum %lld\n", 379 ctl->name, index, int64_val, 380 snd_ctl_elem_info_get_min64(ctl->info)); 381 return false; 382 } 383 384 if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) { 385 ksft_print_msg("%s.%d value %lld more than maximum %lld\n", 386 ctl->name, index, int64_val, 387 snd_ctl_elem_info_get_max64(ctl->info)); 388 return false; 389 } 390 391 /* Only check step size if there is one and we're in bounds */ 392 if (snd_ctl_elem_info_get_step64(ctl->info) && 393 (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) % 394 snd_ctl_elem_info_get_step64(ctl->info)) { 395 ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n", 396 ctl->name, index, int64_val, 397 snd_ctl_elem_info_get_step64(ctl->info), 398 snd_ctl_elem_info_get_min64(ctl->info)); 399 return false; 400 } 401 break; 402 403 case SND_CTL_ELEM_TYPE_ENUMERATED: 404 int_val = snd_ctl_elem_value_get_enumerated(val, index); 405 406 if (int_val < 0) { 407 ksft_print_msg("%s.%d negative value %ld for enumeration\n", 408 ctl->name, index, int_val); 409 return false; 410 } 411 412 if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) { 413 ksft_print_msg("%s.%d value %ld more than item count %u\n", 414 ctl->name, index, int_val, 415 snd_ctl_elem_info_get_items(ctl->info)); 416 return false; 417 } 418 break; 419 420 default: 421 /* No tests for other types */ 422 break; 423 } 424 425 return true; 426 } 427 428 /* 429 * Check that the provided value meets the constraints for the 430 * provided control. 431 */ 432 static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val) 433 { 434 int i; 435 bool valid = true; 436 437 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) 438 if (!ctl_value_index_valid(ctl, val, i)) 439 valid = false; 440 441 return valid; 442 } 443 444 /* 445 * Check that we can read the default value and it is valid. Write 446 * tests use the read value to restore the default. 447 */ 448 static void test_ctl_get_value(struct ctl_data *ctl) 449 { 450 int err; 451 452 /* If the control is turned off let's be polite */ 453 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 454 ksft_print_msg("%s is inactive\n", ctl->name); 455 ksft_test_result_skip("get_value.%s.%d\n", 456 ctl->card->card_name, ctl->elem); 457 return; 458 } 459 460 /* Can't test reading on an unreadable control */ 461 if (!snd_ctl_elem_info_is_readable(ctl->info)) { 462 ksft_print_msg("%s is not readable\n", ctl->name); 463 ksft_test_result_skip("get_value.%s.%d\n", 464 ctl->card->card_name, ctl->elem); 465 return; 466 } 467 468 err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val); 469 if (err < 0) { 470 ksft_print_msg("snd_ctl_elem_read() failed: %s\n", 471 snd_strerror(err)); 472 goto out; 473 } 474 475 if (!ctl_value_valid(ctl, ctl->def_val)) 476 err = -EINVAL; 477 478 out: 479 ksft_test_result(err >= 0, "get_value.%s.%d\n", 480 ctl->card->card_name, ctl->elem); 481 } 482 483 static bool strend(const char *haystack, const char *needle) 484 { 485 size_t haystack_len = strlen(haystack); 486 size_t needle_len = strlen(needle); 487 488 if (needle_len > haystack_len) 489 return false; 490 return strcmp(haystack + haystack_len - needle_len, needle) == 0; 491 } 492 493 static void test_ctl_name(struct ctl_data *ctl) 494 { 495 bool name_ok = true; 496 497 ksft_print_msg("%s.%d %s\n", ctl->card->card_name, ctl->elem, 498 ctl->name); 499 500 /* Only boolean controls should end in Switch */ 501 if (strend(ctl->name, " Switch")) { 502 if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) { 503 ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n", 504 ctl->card->card, ctl->elem, ctl->name); 505 name_ok = false; 506 } 507 } 508 509 /* Writeable boolean controls should end in Switch */ 510 if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN && 511 snd_ctl_elem_info_is_writable(ctl->info)) { 512 if (!strend(ctl->name, " Switch")) { 513 ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n", 514 ctl->card->card, ctl->elem, ctl->name); 515 name_ok = false; 516 } 517 } 518 519 ksft_test_result(name_ok, "name.%s.%d\n", 520 ctl->card->card_name, ctl->elem); 521 } 522 523 static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val, 524 snd_ctl_elem_value_t *read_val) 525 { 526 long long orig_int, read_int; 527 int i; 528 529 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 530 switch (snd_ctl_elem_info_get_type(ctl->info)) { 531 case SND_CTL_ELEM_TYPE_BOOLEAN: 532 orig_int = snd_ctl_elem_value_get_boolean(orig_val, i); 533 read_int = snd_ctl_elem_value_get_boolean(read_val, i); 534 break; 535 536 case SND_CTL_ELEM_TYPE_INTEGER: 537 orig_int = snd_ctl_elem_value_get_integer(orig_val, i); 538 read_int = snd_ctl_elem_value_get_integer(read_val, i); 539 break; 540 541 case SND_CTL_ELEM_TYPE_INTEGER64: 542 orig_int = snd_ctl_elem_value_get_integer64(orig_val, 543 i); 544 read_int = snd_ctl_elem_value_get_integer64(read_val, 545 i); 546 break; 547 548 case SND_CTL_ELEM_TYPE_ENUMERATED: 549 orig_int = snd_ctl_elem_value_get_enumerated(orig_val, 550 i); 551 read_int = snd_ctl_elem_value_get_enumerated(read_val, 552 i); 553 break; 554 555 default: 556 return; 557 } 558 559 ksft_print_msg("%s.%d orig %lld read %lld, is_volatile %d\n", 560 ctl->name, i, orig_int, read_int, 561 snd_ctl_elem_info_is_volatile(ctl->info)); 562 } 563 } 564 565 static bool show_mismatch(struct ctl_data *ctl, int index, 566 snd_ctl_elem_value_t *read_val, 567 snd_ctl_elem_value_t *expected_val) 568 { 569 long long expected_int, read_int; 570 571 /* 572 * We factor out the code to compare values representable as 573 * integers, ensure that check doesn't log otherwise. 574 */ 575 expected_int = 0; 576 read_int = 0; 577 578 switch (snd_ctl_elem_info_get_type(ctl->info)) { 579 case SND_CTL_ELEM_TYPE_BOOLEAN: 580 expected_int = snd_ctl_elem_value_get_boolean(expected_val, 581 index); 582 read_int = snd_ctl_elem_value_get_boolean(read_val, index); 583 break; 584 585 case SND_CTL_ELEM_TYPE_INTEGER: 586 expected_int = snd_ctl_elem_value_get_integer(expected_val, 587 index); 588 read_int = snd_ctl_elem_value_get_integer(read_val, index); 589 break; 590 591 case SND_CTL_ELEM_TYPE_INTEGER64: 592 expected_int = snd_ctl_elem_value_get_integer64(expected_val, 593 index); 594 read_int = snd_ctl_elem_value_get_integer64(read_val, 595 index); 596 break; 597 598 case SND_CTL_ELEM_TYPE_ENUMERATED: 599 expected_int = snd_ctl_elem_value_get_enumerated(expected_val, 600 index); 601 read_int = snd_ctl_elem_value_get_enumerated(read_val, 602 index); 603 break; 604 605 default: 606 break; 607 } 608 609 if (expected_int != read_int) { 610 /* 611 * NOTE: The volatile attribute means that the hardware 612 * can voluntarily change the state of control element 613 * independent of any operation by software. 614 */ 615 bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info); 616 ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n", 617 ctl->name, index, expected_int, read_int, is_volatile); 618 return !is_volatile; 619 } else { 620 return false; 621 } 622 } 623 624 /* 625 * Write a value then if possible verify that we get the expected 626 * result. An optional expected value can be provided if we expect 627 * the write to fail, for verifying that invalid writes don't corrupt 628 * anything. 629 */ 630 static int write_and_verify(struct ctl_data *ctl, 631 snd_ctl_elem_value_t *write_val, 632 snd_ctl_elem_value_t *expected_val) 633 { 634 int err, i; 635 bool error_expected, mismatch_shown; 636 snd_ctl_elem_value_t *initial_val, *read_val, *w_val; 637 snd_ctl_elem_value_alloca(&initial_val); 638 snd_ctl_elem_value_alloca(&read_val); 639 snd_ctl_elem_value_alloca(&w_val); 640 641 /* 642 * We need to copy the write value since writing can modify 643 * the value which causes surprises, and allocate an expected 644 * value if we expect to read back what we wrote. 645 */ 646 snd_ctl_elem_value_copy(w_val, write_val); 647 if (expected_val) { 648 error_expected = true; 649 } else { 650 error_expected = false; 651 snd_ctl_elem_value_alloca(&expected_val); 652 snd_ctl_elem_value_copy(expected_val, write_val); 653 } 654 655 /* Store the value before we write */ 656 if (snd_ctl_elem_info_is_readable(ctl->info)) { 657 snd_ctl_elem_value_set_id(initial_val, ctl->id); 658 659 err = snd_ctl_elem_read(ctl->card->handle, initial_val); 660 if (err < 0) { 661 ksft_print_msg("snd_ctl_elem_read() failed: %s\n", 662 snd_strerror(err)); 663 return err; 664 } 665 } 666 667 /* 668 * Do the write, if we have an expected value ignore the error 669 * and carry on to validate the expected value. 670 */ 671 err = snd_ctl_elem_write(ctl->card->handle, w_val); 672 if (err < 0 && !error_expected) { 673 ksft_print_msg("snd_ctl_elem_write() failed: %s\n", 674 snd_strerror(err)); 675 return err; 676 } 677 678 /* Can we do the verification part? */ 679 if (!snd_ctl_elem_info_is_readable(ctl->info)) 680 return err; 681 682 snd_ctl_elem_value_set_id(read_val, ctl->id); 683 684 err = snd_ctl_elem_read(ctl->card->handle, read_val); 685 if (err < 0) { 686 ksft_print_msg("snd_ctl_elem_read() failed: %s\n", 687 snd_strerror(err)); 688 return err; 689 } 690 691 /* 692 * We can't verify any specific value for volatile controls 693 * but we should still check that whatever we read is a valid 694 * vale for the control. 695 */ 696 if (snd_ctl_elem_info_is_volatile(ctl->info)) { 697 if (!ctl_value_valid(ctl, read_val)) { 698 ksft_print_msg("Volatile control %s has invalid value\n", 699 ctl->name); 700 return -EINVAL; 701 } 702 703 return 0; 704 } 705 706 /* 707 * Check for an event if the value changed, or confirm that 708 * there was none if it didn't. We rely on the kernel 709 * generating the notification before it returns from the 710 * write, this is currently true, should that ever change this 711 * will most likely break and need updating. 712 */ 713 err = wait_for_event(ctl, 0); 714 if (snd_ctl_elem_value_compare(initial_val, read_val)) { 715 if (err < 1) { 716 ksft_print_msg("No event generated for %s\n", 717 ctl->name); 718 show_values(ctl, initial_val, read_val); 719 ctl->event_missing++; 720 } 721 } else { 722 if (err != 0) { 723 ksft_print_msg("Spurious event generated for %s\n", 724 ctl->name); 725 show_values(ctl, initial_val, read_val); 726 ctl->event_spurious++; 727 } 728 } 729 730 /* 731 * Use the libray to compare values, if there's a mismatch 732 * carry on and try to provide a more useful diagnostic than 733 * just "mismatch". 734 */ 735 if (!snd_ctl_elem_value_compare(expected_val, read_val)) 736 return 0; 737 738 mismatch_shown = false; 739 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) 740 if (show_mismatch(ctl, i, read_val, expected_val)) 741 mismatch_shown = true; 742 743 if (!mismatch_shown) 744 ksft_print_msg("%s read and written values differ\n", 745 ctl->name); 746 747 return -1; 748 } 749 750 /* 751 * Make sure we can write the default value back to the control, this 752 * should validate that at least some write works. 753 */ 754 static void test_ctl_write_default(struct ctl_data *ctl) 755 { 756 int err; 757 758 /* If the control is turned off let's be polite */ 759 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 760 ksft_print_msg("%s is inactive\n", ctl->name); 761 ksft_test_result_skip("write_default.%s.%d\n", 762 ctl->card->card_name, ctl->elem); 763 return; 764 } 765 766 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 767 ksft_print_msg("%s is not writeable\n", ctl->name); 768 ksft_test_result_skip("write_default.%s.%d\n", 769 ctl->card->card_name, ctl->elem); 770 return; 771 } 772 773 /* No idea what the default was for unreadable controls */ 774 if (!snd_ctl_elem_info_is_readable(ctl->info)) { 775 ksft_print_msg("%s couldn't read default\n", ctl->name); 776 ksft_test_result_skip("write_default.%s.%d\n", 777 ctl->card->card_name, ctl->elem); 778 return; 779 } 780 781 err = write_and_verify(ctl, ctl->def_val, NULL); 782 783 ksft_test_result(err >= 0, "write_default.%s.%d\n", 784 ctl->card->card_name, ctl->elem); 785 } 786 787 static bool test_ctl_write_valid_boolean(struct ctl_data *ctl) 788 { 789 int err, i, j; 790 bool fail = false; 791 snd_ctl_elem_value_t *val; 792 snd_ctl_elem_value_alloca(&val); 793 794 snd_ctl_elem_value_set_id(val, ctl->id); 795 796 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 797 for (j = 0; j < 2; j++) { 798 snd_ctl_elem_value_set_boolean(val, i, j); 799 err = write_and_verify(ctl, val, NULL); 800 if (err != 0) 801 fail = true; 802 } 803 } 804 805 return !fail; 806 } 807 808 static bool test_ctl_write_valid_integer(struct ctl_data *ctl) 809 { 810 int err; 811 int i; 812 long j, step; 813 bool fail = false; 814 snd_ctl_elem_value_t *val; 815 snd_ctl_elem_value_alloca(&val); 816 817 snd_ctl_elem_value_set_id(val, ctl->id); 818 819 step = snd_ctl_elem_info_get_step(ctl->info); 820 if (!step) 821 step = 1; 822 823 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 824 for (j = snd_ctl_elem_info_get_min(ctl->info); 825 j <= snd_ctl_elem_info_get_max(ctl->info); j += step) { 826 827 snd_ctl_elem_value_set_integer(val, i, j); 828 err = write_and_verify(ctl, val, NULL); 829 if (err != 0) 830 fail = true; 831 } 832 } 833 834 835 return !fail; 836 } 837 838 static bool test_ctl_write_valid_integer64(struct ctl_data *ctl) 839 { 840 int err, i; 841 long long j, step; 842 bool fail = false; 843 snd_ctl_elem_value_t *val; 844 snd_ctl_elem_value_alloca(&val); 845 846 snd_ctl_elem_value_set_id(val, ctl->id); 847 848 step = snd_ctl_elem_info_get_step64(ctl->info); 849 if (!step) 850 step = 1; 851 852 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 853 for (j = snd_ctl_elem_info_get_min64(ctl->info); 854 j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) { 855 856 snd_ctl_elem_value_set_integer64(val, i, j); 857 err = write_and_verify(ctl, val, NULL); 858 if (err != 0) 859 fail = true; 860 } 861 } 862 863 return !fail; 864 } 865 866 static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl) 867 { 868 int err, i, j; 869 bool fail = false; 870 snd_ctl_elem_value_t *val; 871 snd_ctl_elem_value_alloca(&val); 872 873 snd_ctl_elem_value_set_id(val, ctl->id); 874 875 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 876 for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) { 877 snd_ctl_elem_value_set_enumerated(val, i, j); 878 err = write_and_verify(ctl, val, NULL); 879 if (err != 0) 880 fail = true; 881 } 882 } 883 884 return !fail; 885 } 886 887 /* 888 * Find the read only mask control for an IEC958 value control. The two 889 * share device, subdevice and index but not always the interface. 890 */ 891 static struct ctl_data *find_iec958_mask_ctl(struct ctl_data *ctl, 892 const char *suffix) 893 { 894 char name[64]; 895 int stem; 896 struct ctl_data *mask; 897 898 stem = strlen(ctl->name) - strlen(IEC958_DEFAULT); 899 if (snprintf(name, sizeof(name), "%.*s%s", stem, ctl->name, suffix) >= 900 (int)sizeof(name)) 901 return NULL; 902 903 for (mask = ctl_list; mask != NULL; mask = mask->next) { 904 if (mask->card != ctl->card) 905 continue; 906 if (snd_ctl_elem_info_get_type(mask->info) != 907 SND_CTL_ELEM_TYPE_IEC958) 908 continue; 909 if (snd_ctl_elem_info_is_inactive(mask->info)) 910 continue; 911 if (!snd_ctl_elem_info_is_readable(mask->info)) 912 continue; 913 if (snd_ctl_elem_id_get_device(mask->id) != 914 snd_ctl_elem_id_get_device(ctl->id)) 915 continue; 916 if (snd_ctl_elem_id_get_subdevice(mask->id) != 917 snd_ctl_elem_id_get_subdevice(ctl->id)) 918 continue; 919 if (snd_ctl_elem_id_get_index(mask->id) != 920 snd_ctl_elem_id_get_index(ctl->id)) 921 continue; 922 if (strcmp(mask->name, name) == 0) 923 return mask; 924 } 925 926 return NULL; 927 } 928 929 /* 930 * Read the bits the device says it implements. Bit 0 of the first status 931 * byte picks which mask applies, some devices publish only a plain Mask. 932 */ 933 static bool read_iec958_mask(struct ctl_data *ctl, 934 const snd_aes_iec958_t *cur, 935 snd_aes_iec958_t *mask) 936 { 937 int err; 938 struct ctl_data *mask_ctl; 939 snd_ctl_elem_value_t *val; 940 941 if (!strend(ctl->name, IEC958_DEFAULT)) 942 return false; 943 944 if (cur->status[0] & IEC958_AES0_PROFESSIONAL) 945 mask_ctl = find_iec958_mask_ctl(ctl, IEC958_PRO_MASK); 946 else 947 mask_ctl = find_iec958_mask_ctl(ctl, IEC958_CON_MASK); 948 if (!mask_ctl) 949 mask_ctl = find_iec958_mask_ctl(ctl, IEC958_MASK); 950 if (!mask_ctl) 951 return false; 952 953 snd_ctl_elem_value_alloca(&val); 954 snd_ctl_elem_value_set_id(val, mask_ctl->id); 955 956 err = snd_ctl_elem_read(mask_ctl->card->handle, val); 957 if (err < 0) { 958 ksft_print_msg("snd_ctl_elem_read() failed for %s: %s\n", 959 mask_ctl->name, snd_strerror(err)); 960 return false; 961 } 962 963 snd_ctl_elem_value_get_iec958(val, mask); 964 965 return true; 966 } 967 968 /* 969 * Throw away the events from a write we are not checking, one left behind 970 * would make a missing notification look like a notification we got. 971 */ 972 static int drop_events(struct ctl_data *ctl) 973 { 974 int err; 975 976 do { 977 err = wait_for_event(ctl, 0); 978 } while (err > 0); 979 980 return err; 981 } 982 983 /* 984 * Toggle every bit the device advertises, one at a time. Each one starts 985 * from the value we read since a driver can look at the rest of the block 986 * when it stores a bit, and bit 0 of the first status byte selects the 987 * layout the others are read in so leave that one alone. 988 */ 989 static bool test_ctl_write_valid_iec958_mask(struct ctl_data *ctl, 990 snd_ctl_elem_value_t *orig_val, 991 const snd_aes_iec958_t *mask) 992 { 993 int err, j; 994 unsigned int byte; 995 unsigned char bit; 996 bool fail = false, found = false; 997 snd_aes_iec958_t iec958; 998 snd_ctl_elem_value_t *val; 999 snd_ctl_elem_value_alloca(&val); 1000 1001 snd_ctl_elem_value_copy(val, orig_val); 1002 1003 for (byte = 0; byte < sizeof(mask->status); byte++) { 1004 for (j = 0; j < 8; j++) { 1005 bit = 1 << j; 1006 1007 if (byte == 0 && bit == IEC958_AES0_PROFESSIONAL) 1008 continue; 1009 if (!(mask->status[byte] & bit)) 1010 continue; 1011 found = true; 1012 1013 snd_ctl_elem_value_get_iec958(orig_val, &iec958); 1014 iec958.status[byte] ^= bit; 1015 snd_ctl_elem_value_set_iec958(val, &iec958); 1016 1017 err = write_and_verify(ctl, val, NULL); 1018 if (err != 0) { 1019 ksft_print_msg("%s failed to set advertised status[%u] 0x%02x\n", 1020 ctl->name, byte, bit); 1021 fail = true; 1022 } 1023 } 1024 } 1025 1026 if (!found) 1027 ksft_print_msg("%s implements no settable status bits\n", 1028 ctl->name); 1029 1030 return !fail; 1031 } 1032 1033 /* 1034 * With nothing advertised all we can do is try non audio, and the device 1035 * need not implement even that. Write it once and ignore the result, then 1036 * compare against what came back so the value cannot fail, which leaves the 1037 * notification counted by event_missing. 1038 */ 1039 static bool test_ctl_write_valid_iec958_guess(struct ctl_data *ctl, 1040 snd_ctl_elem_value_t *orig_val) 1041 { 1042 int err; 1043 snd_aes_iec958_t iec958; 1044 snd_ctl_elem_value_t *val, *read_val, *w_val; 1045 snd_ctl_elem_value_alloca(&val); 1046 snd_ctl_elem_value_alloca(&read_val); 1047 snd_ctl_elem_value_alloca(&w_val); 1048 1049 snd_ctl_elem_value_get_iec958(orig_val, &iec958); 1050 iec958.status[0] ^= IEC958_AES0_NONAUDIO; 1051 snd_ctl_elem_value_copy(val, orig_val); 1052 snd_ctl_elem_value_set_iec958(val, &iec958); 1053 1054 /* Writing can modify the value so keep a copy to write from */ 1055 snd_ctl_elem_value_copy(w_val, val); 1056 err = snd_ctl_elem_write(ctl->card->handle, w_val); 1057 if (err < 0) { 1058 ksft_print_msg("snd_ctl_elem_write() failed: %s\n", 1059 snd_strerror(err)); 1060 return false; 1061 } 1062 1063 snd_ctl_elem_value_set_id(read_val, ctl->id); 1064 err = snd_ctl_elem_read(ctl->card->handle, read_val); 1065 if (err < 0) { 1066 ksft_print_msg("snd_ctl_elem_read() failed: %s\n", 1067 snd_strerror(err)); 1068 return false; 1069 } 1070 1071 /* Put it back where we found it, then drop the events from both */ 1072 snd_ctl_elem_value_copy(w_val, orig_val); 1073 err = snd_ctl_elem_write(ctl->card->handle, w_val); 1074 if (err < 0) { 1075 ksft_print_msg("snd_ctl_elem_write() failed: %s\n", 1076 snd_strerror(err)); 1077 return false; 1078 } 1079 1080 err = drop_events(ctl); 1081 if (err < 0) { 1082 ksft_print_msg("drop_events() failed for %s: %d\n", 1083 ctl->name, err); 1084 return false; 1085 } 1086 1087 if (snd_ctl_elem_value_compare(val, read_val)) { 1088 ksft_print_msg("%s does not implement status[0] 0x%02x\n", 1089 ctl->name, IEC958_AES0_NONAUDIO); 1090 1091 /* Expect what came back, the notification is still checked */ 1092 return write_and_verify(ctl, val, read_val) == 0; 1093 } 1094 1095 return write_and_verify(ctl, val, NULL) == 0; 1096 } 1097 1098 /* 1099 * Write the channel status bits and check that userspace is told about it. 1100 * Where a mask was found every bit in it has to stick, otherwise we fall 1101 * back to one guessed bit the device is free to ignore. 1102 */ 1103 static bool test_ctl_write_valid_iec958(struct ctl_data *ctl) 1104 { 1105 int err; 1106 snd_aes_iec958_t iec958, mask; 1107 snd_ctl_elem_value_t *orig_val; 1108 snd_ctl_elem_value_alloca(&orig_val); 1109 1110 /* 1111 * The bytes past the ones a driver implements are compared too, 1112 * so start from a read rather than building a value here. 1113 */ 1114 snd_ctl_elem_value_set_id(orig_val, ctl->id); 1115 err = snd_ctl_elem_read(ctl->card->handle, orig_val); 1116 if (err < 0) { 1117 ksft_print_msg("snd_ctl_elem_read() failed: %s\n", 1118 snd_strerror(err)); 1119 return false; 1120 } 1121 1122 snd_ctl_elem_value_get_iec958(orig_val, &iec958); 1123 1124 if (!read_iec958_mask(ctl, &iec958, &mask)) { 1125 ksft_print_msg("%s falling back to non audio\n", ctl->name); 1126 return test_ctl_write_valid_iec958_guess(ctl, orig_val); 1127 } 1128 1129 return test_ctl_write_valid_iec958_mask(ctl, orig_val, &mask); 1130 } 1131 1132 static void test_ctl_write_valid(struct ctl_data *ctl) 1133 { 1134 bool pass; 1135 1136 /* If the control is turned off let's be polite */ 1137 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 1138 ksft_print_msg("%s is inactive\n", ctl->name); 1139 ksft_test_result_skip("write_valid.%s.%d\n", 1140 ctl->card->card_name, ctl->elem); 1141 return; 1142 } 1143 1144 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 1145 ksft_print_msg("%s is not writeable\n", ctl->name); 1146 ksft_test_result_skip("write_valid.%s.%d\n", 1147 ctl->card->card_name, ctl->elem); 1148 return; 1149 } 1150 1151 switch (snd_ctl_elem_info_get_type(ctl->info)) { 1152 case SND_CTL_ELEM_TYPE_BOOLEAN: 1153 pass = test_ctl_write_valid_boolean(ctl); 1154 break; 1155 1156 case SND_CTL_ELEM_TYPE_INTEGER: 1157 pass = test_ctl_write_valid_integer(ctl); 1158 break; 1159 1160 case SND_CTL_ELEM_TYPE_INTEGER64: 1161 pass = test_ctl_write_valid_integer64(ctl); 1162 break; 1163 1164 case SND_CTL_ELEM_TYPE_ENUMERATED: 1165 pass = test_ctl_write_valid_enumerated(ctl); 1166 break; 1167 1168 case SND_CTL_ELEM_TYPE_IEC958: 1169 pass = test_ctl_write_valid_iec958(ctl); 1170 break; 1171 1172 default: 1173 /* No tests for this yet */ 1174 ksft_test_result_skip("write_valid.%s.%d\n", 1175 ctl->card->card_name, ctl->elem); 1176 return; 1177 } 1178 1179 /* Restore the default value to minimise disruption */ 1180 write_and_verify(ctl, ctl->def_val, NULL); 1181 1182 ksft_test_result(pass, "write_valid.%s.%d\n", 1183 ctl->card->card_name, ctl->elem); 1184 } 1185 1186 /* 1187 * Build the smallest or the largest value the control offers. The smallest 1188 * clears the control's register field and the largest sets its top bit, which 1189 * is the bit a mask one bit too wide puts in its neighbour. 1190 */ 1191 static bool set_limit_value(struct ctl_data *ctl, snd_ctl_elem_value_t *val, 1192 bool max) 1193 { 1194 int i, count = snd_ctl_elem_info_get_count(ctl->info); 1195 1196 snd_ctl_elem_value_set_id(val, ctl->id); 1197 1198 switch (snd_ctl_elem_info_get_type(ctl->info)) { 1199 case SND_CTL_ELEM_TYPE_BOOLEAN: 1200 for (i = 0; i < count; i++) 1201 snd_ctl_elem_value_set_boolean(val, i, max); 1202 return true; 1203 1204 case SND_CTL_ELEM_TYPE_INTEGER: 1205 for (i = 0; i < count; i++) 1206 snd_ctl_elem_value_set_integer(val, i, max ? 1207 snd_ctl_elem_info_get_max(ctl->info) : 1208 snd_ctl_elem_info_get_min(ctl->info)); 1209 return true; 1210 1211 case SND_CTL_ELEM_TYPE_INTEGER64: 1212 for (i = 0; i < count; i++) 1213 snd_ctl_elem_value_set_integer64(val, i, max ? 1214 snd_ctl_elem_info_get_max64(ctl->info) : 1215 snd_ctl_elem_info_get_min64(ctl->info)); 1216 return true; 1217 1218 case SND_CTL_ELEM_TYPE_ENUMERATED: 1219 for (i = 0; i < count; i++) 1220 snd_ctl_elem_value_set_enumerated(val, i, max ? 1221 snd_ctl_elem_info_get_items(ctl->info) - 1 : 0); 1222 return true; 1223 1224 default: 1225 /* Nothing sensible to write for the rest */ 1226 return false; 1227 } 1228 } 1229 1230 /* Note every control on the card that no longer reads as it did */ 1231 static void find_moved_ctls(struct ctl_data *ctl, snd_ctl_elem_value_t *val) 1232 { 1233 struct ctl_data *other; 1234 int err; 1235 1236 for (other = ctl_list; other != NULL; other = other->next) { 1237 if (!other->snapshot_valid) 1238 continue; 1239 1240 /* 1241 * The buffer is shared and compare() looks at all of it, so 1242 * clear what the last control left in the slots this one 1243 * does not use. 1244 */ 1245 snd_ctl_elem_value_clear(val); 1246 snd_ctl_elem_value_set_id(val, other->id); 1247 err = snd_ctl_elem_read(ctl->card->handle, val); 1248 if (err < 0) { 1249 ksft_print_msg("snd_ctl_elem_read() failed for %s: %s\n", 1250 other->name, snd_strerror(err)); 1251 continue; 1252 } 1253 1254 if (snd_ctl_elem_value_compare(other->snapshot, val)) 1255 other->moved = true; 1256 } 1257 } 1258 1259 /* 1260 * Write one control and look for others on the same card that moved with it. 1261 * A driver that links two controls on purpose tells userspace about both, so 1262 * only an unannounced change is counted. That is what a control whose 1263 * register mask covers bits belonging to its neighbour looks like from here. 1264 */ 1265 static void test_ctl_write_side_effects(struct ctl_data *ctl) 1266 { 1267 struct ctl_data *other; 1268 snd_ctl_elem_value_t *min_val, *max_val, *read_val; 1269 int err; 1270 1271 snd_ctl_elem_value_alloca(&min_val); 1272 snd_ctl_elem_value_alloca(&max_val); 1273 snd_ctl_elem_value_alloca(&read_val); 1274 1275 /* Without a readable default there is nothing to put back */ 1276 if (snd_ctl_elem_info_is_inactive(ctl->info) || 1277 !snd_ctl_elem_info_is_writable(ctl->info) || 1278 !snd_ctl_elem_info_is_readable(ctl->info) || 1279 !set_limit_value(ctl, min_val, false) || 1280 !set_limit_value(ctl, max_val, true)) { 1281 ksft_test_result_skip("write_side_effects.%s.%d\n", 1282 ctl->card->card_name, ctl->elem); 1283 return; 1284 } 1285 1286 /* Drain first, a stale event would look like the driver announced it */ 1287 drop_events(ctl); 1288 1289 /* 1290 * Record what the rest of the card reads as. A volatile control can 1291 * move on its own so there is nothing to compare it against. 1292 */ 1293 for (other = ctl_list; other != NULL; other = other->next) { 1294 other->snapshot_valid = false; 1295 other->moved = false; 1296 other->ev_mask = 0; 1297 1298 if (other == ctl || other->card != ctl->card) 1299 continue; 1300 if (!snd_ctl_elem_info_is_readable(other->info) || 1301 snd_ctl_elem_info_is_volatile(other->info)) 1302 continue; 1303 1304 snd_ctl_elem_value_clear(other->snapshot); 1305 snd_ctl_elem_value_set_id(other->snapshot, other->id); 1306 err = snd_ctl_elem_read(ctl->card->handle, other->snapshot); 1307 if (err < 0) { 1308 ksft_print_msg("snd_ctl_elem_read() failed for %s: %s\n", 1309 other->name, snd_strerror(err)); 1310 continue; 1311 } 1312 1313 other->snapshot_valid = true; 1314 } 1315 1316 /* 1317 * Compare against the snapshot after each write, before anything is 1318 * put back. Restoring the control we wrote goes through the same 1319 * mask, so doing it first would hide the change we are looking for. 1320 */ 1321 err = snd_ctl_elem_write(ctl->card->handle, min_val); 1322 if (err >= 0) { 1323 drop_events(ctl); 1324 find_moved_ctls(ctl, read_val); 1325 err = snd_ctl_elem_write(ctl->card->handle, max_val); 1326 } 1327 if (err < 0) { 1328 ksft_print_msg("snd_ctl_elem_write() failed for %s: %s\n", 1329 ctl->name, snd_strerror(err)); 1330 } else { 1331 drop_events(ctl); 1332 find_moved_ctls(ctl, read_val); 1333 } 1334 1335 for (other = ctl_list; other != NULL; other = other->next) { 1336 if (!other->moved) 1337 continue; 1338 1339 if (other->ev_mask & SND_CTL_EVENT_MASK_VALUE) { 1340 ksft_print_msg("Writing %s changed %s, the driver said so\n", 1341 ctl->name, other->name); 1342 } else { 1343 ksft_print_msg("Writing %s silently changed %s\n", 1344 ctl->name, other->name); 1345 ctl->side_effects++; 1346 } 1347 } 1348 1349 /* 1350 * The control we wrote goes back first so its mask stops moving the 1351 * rest. A plain write keeps this out of the event counters, they 1352 * belong to the tests that check them. 1353 */ 1354 snd_ctl_elem_write(ctl->card->handle, ctl->def_val); 1355 1356 for (other = ctl_list; other != NULL; other = other->next) { 1357 if (!other->snapshot_valid || 1358 !snd_ctl_elem_info_is_writable(other->info)) 1359 continue; 1360 1361 snd_ctl_elem_value_clear(read_val); 1362 snd_ctl_elem_value_set_id(read_val, other->id); 1363 if (snd_ctl_elem_read(ctl->card->handle, read_val) < 0) 1364 continue; 1365 1366 if (snd_ctl_elem_value_compare(other->snapshot, read_val)) 1367 snd_ctl_elem_write(ctl->card->handle, other->snapshot); 1368 } 1369 1370 /* Our own restores queue events, the next test must not see them */ 1371 drop_events(ctl); 1372 1373 if (err < 0) 1374 ksft_test_result_skip("write_side_effects.%s.%d\n", 1375 ctl->card->card_name, ctl->elem); 1376 else 1377 ksft_test_result(!ctl->side_effects, 1378 "write_side_effects.%s.%d\n", 1379 ctl->card->card_name, ctl->elem); 1380 } 1381 1382 static bool test_ctl_write_invalid_value(struct ctl_data *ctl, 1383 snd_ctl_elem_value_t *val) 1384 { 1385 int err; 1386 1387 /* Ideally this will fail... */ 1388 err = snd_ctl_elem_write(ctl->card->handle, val); 1389 if (err < 0) 1390 return false; 1391 1392 /* ...but some devices will clamp to an in range value */ 1393 err = snd_ctl_elem_read(ctl->card->handle, val); 1394 if (err < 0) { 1395 ksft_print_msg("%s failed to read: %s\n", 1396 ctl->name, snd_strerror(err)); 1397 return true; 1398 } 1399 1400 return !ctl_value_valid(ctl, val); 1401 } 1402 1403 static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl) 1404 { 1405 int i; 1406 bool fail = false; 1407 snd_ctl_elem_value_t *val; 1408 snd_ctl_elem_value_alloca(&val); 1409 1410 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 1411 snd_ctl_elem_value_copy(val, ctl->def_val); 1412 snd_ctl_elem_value_set_boolean(val, i, 2); 1413 1414 if (test_ctl_write_invalid_value(ctl, val)) 1415 fail = true; 1416 } 1417 1418 return !fail; 1419 } 1420 1421 static bool test_ctl_write_invalid_integer(struct ctl_data *ctl) 1422 { 1423 int i; 1424 bool fail = false; 1425 snd_ctl_elem_value_t *val; 1426 snd_ctl_elem_value_alloca(&val); 1427 1428 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 1429 if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) { 1430 /* Just under range */ 1431 snd_ctl_elem_value_copy(val, ctl->def_val); 1432 snd_ctl_elem_value_set_integer(val, i, 1433 snd_ctl_elem_info_get_min(ctl->info) - 1); 1434 1435 if (test_ctl_write_invalid_value(ctl, val)) 1436 fail = true; 1437 1438 /* Minimum representable value */ 1439 snd_ctl_elem_value_copy(val, ctl->def_val); 1440 snd_ctl_elem_value_set_integer(val, i, LONG_MIN); 1441 1442 if (test_ctl_write_invalid_value(ctl, val)) 1443 fail = true; 1444 } 1445 1446 if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) { 1447 /* Just over range */ 1448 snd_ctl_elem_value_copy(val, ctl->def_val); 1449 snd_ctl_elem_value_set_integer(val, i, 1450 snd_ctl_elem_info_get_max(ctl->info) + 1); 1451 1452 if (test_ctl_write_invalid_value(ctl, val)) 1453 fail = true; 1454 1455 /* Maximum representable value */ 1456 snd_ctl_elem_value_copy(val, ctl->def_val); 1457 snd_ctl_elem_value_set_integer(val, i, LONG_MAX); 1458 1459 if (test_ctl_write_invalid_value(ctl, val)) 1460 fail = true; 1461 } 1462 } 1463 1464 return !fail; 1465 } 1466 1467 static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl) 1468 { 1469 int i; 1470 bool fail = false; 1471 snd_ctl_elem_value_t *val; 1472 snd_ctl_elem_value_alloca(&val); 1473 1474 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 1475 if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) { 1476 /* Just under range */ 1477 snd_ctl_elem_value_copy(val, ctl->def_val); 1478 snd_ctl_elem_value_set_integer64(val, i, 1479 snd_ctl_elem_info_get_min64(ctl->info) - 1); 1480 1481 if (test_ctl_write_invalid_value(ctl, val)) 1482 fail = true; 1483 1484 /* Minimum representable value */ 1485 snd_ctl_elem_value_copy(val, ctl->def_val); 1486 snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN); 1487 1488 if (test_ctl_write_invalid_value(ctl, val)) 1489 fail = true; 1490 } 1491 1492 if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) { 1493 /* Just over range */ 1494 snd_ctl_elem_value_copy(val, ctl->def_val); 1495 snd_ctl_elem_value_set_integer64(val, i, 1496 snd_ctl_elem_info_get_max64(ctl->info) + 1); 1497 1498 if (test_ctl_write_invalid_value(ctl, val)) 1499 fail = true; 1500 1501 /* Maximum representable value */ 1502 snd_ctl_elem_value_copy(val, ctl->def_val); 1503 snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX); 1504 1505 if (test_ctl_write_invalid_value(ctl, val)) 1506 fail = true; 1507 } 1508 } 1509 1510 return !fail; 1511 } 1512 1513 static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl) 1514 { 1515 int i; 1516 bool fail = false; 1517 snd_ctl_elem_value_t *val; 1518 snd_ctl_elem_value_alloca(&val); 1519 1520 snd_ctl_elem_value_set_id(val, ctl->id); 1521 1522 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) { 1523 /* One beyond maximum */ 1524 snd_ctl_elem_value_copy(val, ctl->def_val); 1525 snd_ctl_elem_value_set_enumerated(val, i, 1526 snd_ctl_elem_info_get_items(ctl->info)); 1527 1528 if (test_ctl_write_invalid_value(ctl, val)) 1529 fail = true; 1530 1531 /* Maximum representable value */ 1532 snd_ctl_elem_value_copy(val, ctl->def_val); 1533 snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX); 1534 1535 if (test_ctl_write_invalid_value(ctl, val)) 1536 fail = true; 1537 1538 } 1539 1540 return !fail; 1541 } 1542 1543 1544 static void test_ctl_write_invalid(struct ctl_data *ctl) 1545 { 1546 bool pass; 1547 1548 /* If the control is turned off let's be polite */ 1549 if (snd_ctl_elem_info_is_inactive(ctl->info)) { 1550 ksft_print_msg("%s is inactive\n", ctl->name); 1551 ksft_test_result_skip("write_invalid.%s.%d\n", 1552 ctl->card->card_name, ctl->elem); 1553 return; 1554 } 1555 1556 if (!snd_ctl_elem_info_is_writable(ctl->info)) { 1557 ksft_print_msg("%s is not writeable\n", ctl->name); 1558 ksft_test_result_skip("write_invalid.%s.%d\n", 1559 ctl->card->card_name, ctl->elem); 1560 return; 1561 } 1562 1563 switch (snd_ctl_elem_info_get_type(ctl->info)) { 1564 case SND_CTL_ELEM_TYPE_BOOLEAN: 1565 pass = test_ctl_write_invalid_boolean(ctl); 1566 break; 1567 1568 case SND_CTL_ELEM_TYPE_INTEGER: 1569 pass = test_ctl_write_invalid_integer(ctl); 1570 break; 1571 1572 case SND_CTL_ELEM_TYPE_INTEGER64: 1573 pass = test_ctl_write_invalid_integer64(ctl); 1574 break; 1575 1576 case SND_CTL_ELEM_TYPE_ENUMERATED: 1577 pass = test_ctl_write_invalid_enumerated(ctl); 1578 break; 1579 1580 default: 1581 /* No tests for this yet */ 1582 ksft_test_result_skip("write_invalid.%s.%d\n", 1583 ctl->card->card_name, ctl->elem); 1584 return; 1585 } 1586 1587 /* Restore the default value to minimise disruption */ 1588 write_and_verify(ctl, ctl->def_val, NULL); 1589 1590 ksft_test_result(pass, "write_invalid.%s.%d\n", 1591 ctl->card->card_name, ctl->elem); 1592 } 1593 1594 static void test_ctl_event_missing(struct ctl_data *ctl) 1595 { 1596 ksft_test_result(!ctl->event_missing, "event_missing.%s.%d\n", 1597 ctl->card->card_name, ctl->elem); 1598 } 1599 1600 static void test_ctl_event_spurious(struct ctl_data *ctl) 1601 { 1602 ksft_test_result(!ctl->event_spurious, "event_spurious.%s.%d\n", 1603 ctl->card->card_name, ctl->elem); 1604 } 1605 1606 int main(void) 1607 { 1608 struct ctl_data *ctl; 1609 1610 ksft_print_header(); 1611 1612 find_controls(); 1613 1614 ksft_set_plan(num_controls * TESTS_PER_CONTROL); 1615 1616 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) { 1617 /* 1618 * Must test get_value() before we write anything, the 1619 * test stores the default value for later cleanup. 1620 */ 1621 test_ctl_get_value(ctl); 1622 test_ctl_name(ctl); 1623 test_ctl_write_default(ctl); 1624 test_ctl_write_valid(ctl); 1625 test_ctl_write_side_effects(ctl); 1626 test_ctl_write_invalid(ctl); 1627 test_ctl_event_missing(ctl); 1628 test_ctl_event_spurious(ctl); 1629 } 1630 1631 ksft_exit_pass(); 1632 1633 return 0; 1634 } 1635