mixer-test.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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. // This test will iterate over all cards detected in the system, exercising
  8. // every mixer control it can find. This may conflict with other system
  9. // software if there is audio activity so is best run on a system with a
  10. // minimal active userspace.
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdbool.h>
  14. #include <limits.h>
  15. #include <string.h>
  16. #include <getopt.h>
  17. #include <stdarg.h>
  18. #include <ctype.h>
  19. #include <math.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <alsa/asoundlib.h>
  23. #include <poll.h>
  24. #include <stdint.h>
  25. #include "kselftest.h"
  26. #include "alsa-local.h"
  27. #define TESTS_PER_CONTROL 7
  28. struct card_data {
  29. snd_ctl_t *handle;
  30. int card;
  31. snd_ctl_card_info_t *info;
  32. const char *card_name;
  33. struct pollfd pollfd;
  34. int num_ctls;
  35. snd_ctl_elem_list_t *ctls;
  36. struct card_data *next;
  37. };
  38. struct ctl_data {
  39. const char *name;
  40. snd_ctl_elem_id_t *id;
  41. snd_ctl_elem_info_t *info;
  42. snd_ctl_elem_value_t *def_val;
  43. int elem;
  44. int event_missing;
  45. int event_spurious;
  46. struct card_data *card;
  47. struct ctl_data *next;
  48. };
  49. int num_cards;
  50. int num_controls;
  51. struct card_data *card_list;
  52. struct ctl_data *ctl_list;
  53. static void find_controls(void)
  54. {
  55. char name[32];
  56. int card, ctl, err;
  57. struct card_data *card_data;
  58. struct ctl_data *ctl_data;
  59. snd_config_t *config;
  60. char *card_name, *card_longname;
  61. card = -1;
  62. if (snd_card_next(&card) < 0 || card < 0)
  63. return;
  64. config = get_alsalib_config();
  65. while (card >= 0) {
  66. sprintf(name, "hw:%d", card);
  67. card_data = malloc(sizeof(*card_data));
  68. if (!card_data)
  69. ksft_exit_fail_msg("Out of memory\n");
  70. err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
  71. if (err < 0) {
  72. ksft_print_msg("Failed to get hctl for card %d: %s\n",
  73. card, snd_strerror(err));
  74. goto next_card;
  75. }
  76. err = snd_card_get_name(card, &card_name);
  77. if (err != 0)
  78. card_name = "Unknown";
  79. err = snd_card_get_longname(card, &card_longname);
  80. if (err != 0)
  81. card_longname = "Unknown";
  82. err = snd_ctl_card_info_malloc(&card_data->info);
  83. if (err != 0)
  84. ksft_exit_fail_msg("Failed to allocate card info: %d\n",
  85. err);
  86. err = snd_ctl_card_info(card_data->handle, card_data->info);
  87. if (err == 0) {
  88. card_data->card_name = snd_ctl_card_info_get_id(card_data->info);
  89. if (!card_data->card_name)
  90. ksft_print_msg("Failed to get card ID\n");
  91. } else {
  92. ksft_print_msg("Failed to get card info: %d\n", err);
  93. }
  94. if (!card_data->card_name)
  95. card_data->card_name = "Unknown";
  96. ksft_print_msg("Card %d/%s - %s (%s)\n", card,
  97. card_data->card_name, card_name, card_longname);
  98. /* Count controls */
  99. snd_ctl_elem_list_malloc(&card_data->ctls);
  100. snd_ctl_elem_list(card_data->handle, card_data->ctls);
  101. card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);
  102. /* Enumerate control information */
  103. snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
  104. snd_ctl_elem_list(card_data->handle, card_data->ctls);
  105. card_data->card = num_cards++;
  106. card_data->next = card_list;
  107. card_list = card_data;
  108. num_controls += card_data->num_ctls;
  109. for (ctl = 0; ctl < card_data->num_ctls; ctl++) {
  110. ctl_data = malloc(sizeof(*ctl_data));
  111. if (!ctl_data)
  112. ksft_exit_fail_msg("Out of memory\n");
  113. memset(ctl_data, 0, sizeof(*ctl_data));
  114. ctl_data->card = card_data;
  115. ctl_data->elem = ctl;
  116. ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,
  117. ctl);
  118. err = snd_ctl_elem_id_malloc(&ctl_data->id);
  119. if (err < 0)
  120. ksft_exit_fail_msg("Out of memory\n");
  121. err = snd_ctl_elem_info_malloc(&ctl_data->info);
  122. if (err < 0)
  123. ksft_exit_fail_msg("Out of memory\n");
  124. err = snd_ctl_elem_value_malloc(&ctl_data->def_val);
  125. if (err < 0)
  126. ksft_exit_fail_msg("Out of memory\n");
  127. snd_ctl_elem_list_get_id(card_data->ctls, ctl,
  128. ctl_data->id);
  129. snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);
  130. err = snd_ctl_elem_info(card_data->handle,
  131. ctl_data->info);
  132. if (err < 0) {
  133. ksft_print_msg("%s getting info for %s\n",
  134. snd_strerror(err),
  135. ctl_data->name);
  136. }
  137. snd_ctl_elem_value_set_id(ctl_data->def_val,
  138. ctl_data->id);
  139. ctl_data->next = ctl_list;
  140. ctl_list = ctl_data;
  141. }
  142. /* Set up for events */
  143. err = snd_ctl_subscribe_events(card_data->handle, true);
  144. if (err < 0) {
  145. ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",
  146. card, err);
  147. }
  148. err = snd_ctl_poll_descriptors_count(card_data->handle);
  149. if (err != 1) {
  150. ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n",
  151. err, card);
  152. }
  153. err = snd_ctl_poll_descriptors(card_data->handle,
  154. &card_data->pollfd, 1);
  155. if (err != 1) {
  156. ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for card %d: %d\n",
  157. card, err);
  158. }
  159. next_card:
  160. if (snd_card_next(&card) < 0) {
  161. ksft_print_msg("snd_card_next");
  162. break;
  163. }
  164. }
  165. snd_config_delete(config);
  166. }
  167. /*
  168. * Block for up to timeout ms for an event, returns a negative value
  169. * on error, 0 for no event and 1 for an event.
  170. */
  171. static int wait_for_event(struct ctl_data *ctl, int timeout)
  172. {
  173. unsigned short revents;
  174. snd_ctl_event_t *event;
  175. int err;
  176. unsigned int mask = 0;
  177. unsigned int ev_id;
  178. snd_ctl_event_alloca(&event);
  179. do {
  180. err = poll(&(ctl->card->pollfd), 1, timeout);
  181. if (err < 0) {
  182. ksft_print_msg("poll() failed for %s: %s (%d)\n",
  183. ctl->name, strerror(errno), errno);
  184. return -1;
  185. }
  186. /* Timeout */
  187. if (err == 0)
  188. return 0;
  189. err = snd_ctl_poll_descriptors_revents(ctl->card->handle,
  190. &(ctl->card->pollfd),
  191. 1, &revents);
  192. if (err < 0) {
  193. ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n",
  194. ctl->name, err);
  195. return err;
  196. }
  197. if (revents & POLLERR) {
  198. ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n",
  199. ctl->name);
  200. return -1;
  201. }
  202. /* No read events */
  203. if (!(revents & POLLIN)) {
  204. ksft_print_msg("No POLLIN\n");
  205. continue;
  206. }
  207. err = snd_ctl_read(ctl->card->handle, event);
  208. if (err < 0) {
  209. ksft_print_msg("snd_ctl_read() failed for %s: %d\n",
  210. ctl->name, err);
  211. return err;
  212. }
  213. if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)
  214. continue;
  215. /* The ID returned from the event is 1 less than numid */
  216. mask = snd_ctl_event_elem_get_mask(event);
  217. ev_id = snd_ctl_event_elem_get_numid(event);
  218. if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {
  219. ksft_print_msg("Event for unexpected ctl %s\n",
  220. snd_ctl_event_elem_get_name(event));
  221. continue;
  222. }
  223. if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {
  224. ksft_print_msg("Removal event for %s\n",
  225. ctl->name);
  226. return -1;
  227. }
  228. } while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);
  229. return 1;
  230. }
  231. static bool ctl_value_index_valid(struct ctl_data *ctl,
  232. snd_ctl_elem_value_t *val,
  233. int index)
  234. {
  235. long int_val;
  236. long long int64_val;
  237. switch (snd_ctl_elem_info_get_type(ctl->info)) {
  238. case SND_CTL_ELEM_TYPE_NONE:
  239. ksft_print_msg("%s.%d Invalid control type NONE\n",
  240. ctl->name, index);
  241. return false;
  242. case SND_CTL_ELEM_TYPE_BOOLEAN:
  243. int_val = snd_ctl_elem_value_get_boolean(val, index);
  244. switch (int_val) {
  245. case 0:
  246. case 1:
  247. break;
  248. default:
  249. ksft_print_msg("%s.%d Invalid boolean value %ld\n",
  250. ctl->name, index, int_val);
  251. return false;
  252. }
  253. break;
  254. case SND_CTL_ELEM_TYPE_INTEGER:
  255. int_val = snd_ctl_elem_value_get_integer(val, index);
  256. if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
  257. ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
  258. ctl->name, index, int_val,
  259. snd_ctl_elem_info_get_min(ctl->info));
  260. return false;
  261. }
  262. if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
  263. ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
  264. ctl->name, index, int_val,
  265. snd_ctl_elem_info_get_max(ctl->info));
  266. return false;
  267. }
  268. /* Only check step size if there is one and we're in bounds */
  269. if (snd_ctl_elem_info_get_step(ctl->info) &&
  270. (int_val - snd_ctl_elem_info_get_min(ctl->info) %
  271. snd_ctl_elem_info_get_step(ctl->info))) {
  272. ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
  273. ctl->name, index, int_val,
  274. snd_ctl_elem_info_get_step(ctl->info),
  275. snd_ctl_elem_info_get_min(ctl->info));
  276. return false;
  277. }
  278. break;
  279. case SND_CTL_ELEM_TYPE_INTEGER64:
  280. int64_val = snd_ctl_elem_value_get_integer64(val, index);
  281. if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
  282. ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
  283. ctl->name, index, int64_val,
  284. snd_ctl_elem_info_get_min64(ctl->info));
  285. return false;
  286. }
  287. if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
  288. ksft_print_msg("%s.%d value %lld more than maximum %ld\n",
  289. ctl->name, index, int64_val,
  290. snd_ctl_elem_info_get_max(ctl->info));
  291. return false;
  292. }
  293. /* Only check step size if there is one and we're in bounds */
  294. if (snd_ctl_elem_info_get_step64(ctl->info) &&
  295. (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
  296. snd_ctl_elem_info_get_step64(ctl->info)) {
  297. ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
  298. ctl->name, index, int64_val,
  299. snd_ctl_elem_info_get_step64(ctl->info),
  300. snd_ctl_elem_info_get_min64(ctl->info));
  301. return false;
  302. }
  303. break;
  304. case SND_CTL_ELEM_TYPE_ENUMERATED:
  305. int_val = snd_ctl_elem_value_get_enumerated(val, index);
  306. if (int_val < 0) {
  307. ksft_print_msg("%s.%d negative value %ld for enumeration\n",
  308. ctl->name, index, int_val);
  309. return false;
  310. }
  311. if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
  312. ksft_print_msg("%s.%d value %ld more than item count %u\n",
  313. ctl->name, index, int_val,
  314. snd_ctl_elem_info_get_items(ctl->info));
  315. return false;
  316. }
  317. break;
  318. default:
  319. /* No tests for other types */
  320. break;
  321. }
  322. return true;
  323. }
  324. /*
  325. * Check that the provided value meets the constraints for the
  326. * provided control.
  327. */
  328. static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)
  329. {
  330. int i;
  331. bool valid = true;
  332. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
  333. if (!ctl_value_index_valid(ctl, val, i))
  334. valid = false;
  335. return valid;
  336. }
  337. /*
  338. * Check that we can read the default value and it is valid. Write
  339. * tests use the read value to restore the default.
  340. */
  341. static void test_ctl_get_value(struct ctl_data *ctl)
  342. {
  343. int err;
  344. /* If the control is turned off let's be polite */
  345. if (snd_ctl_elem_info_is_inactive(ctl->info)) {
  346. ksft_print_msg("%s is inactive\n", ctl->name);
  347. ksft_test_result_skip("get_value.%s.%d\n",
  348. ctl->card->card_name, ctl->elem);
  349. return;
  350. }
  351. /* Can't test reading on an unreadable control */
  352. if (!snd_ctl_elem_info_is_readable(ctl->info)) {
  353. ksft_print_msg("%s is not readable\n", ctl->name);
  354. ksft_test_result_skip("get_value.%s.%d\n",
  355. ctl->card->card_name, ctl->elem);
  356. return;
  357. }
  358. err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);
  359. if (err < 0) {
  360. ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
  361. snd_strerror(err));
  362. goto out;
  363. }
  364. if (!ctl_value_valid(ctl, ctl->def_val))
  365. err = -EINVAL;
  366. out:
  367. ksft_test_result(err >= 0, "get_value.%s.%d\n",
  368. ctl->card->card_name, ctl->elem);
  369. }
  370. static bool strend(const char *haystack, const char *needle)
  371. {
  372. size_t haystack_len = strlen(haystack);
  373. size_t needle_len = strlen(needle);
  374. if (needle_len > haystack_len)
  375. return false;
  376. return strcmp(haystack + haystack_len - needle_len, needle) == 0;
  377. }
  378. static void test_ctl_name(struct ctl_data *ctl)
  379. {
  380. bool name_ok = true;
  381. ksft_print_msg("%s.%d %s\n", ctl->card->card_name, ctl->elem,
  382. ctl->name);
  383. /* Only boolean controls should end in Switch */
  384. if (strend(ctl->name, " Switch")) {
  385. if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {
  386. ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",
  387. ctl->card->card, ctl->elem, ctl->name);
  388. name_ok = false;
  389. }
  390. }
  391. /* Writeable boolean controls should end in Switch */
  392. if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&
  393. snd_ctl_elem_info_is_writable(ctl->info)) {
  394. if (!strend(ctl->name, " Switch")) {
  395. ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",
  396. ctl->card->card, ctl->elem, ctl->name);
  397. name_ok = false;
  398. }
  399. }
  400. ksft_test_result(name_ok, "name.%s.%d\n",
  401. ctl->card->card_name, ctl->elem);
  402. }
  403. static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val,
  404. snd_ctl_elem_value_t *read_val)
  405. {
  406. long long orig_int, read_int;
  407. int i;
  408. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  409. switch (snd_ctl_elem_info_get_type(ctl->info)) {
  410. case SND_CTL_ELEM_TYPE_BOOLEAN:
  411. orig_int = snd_ctl_elem_value_get_boolean(orig_val, i);
  412. read_int = snd_ctl_elem_value_get_boolean(read_val, i);
  413. break;
  414. case SND_CTL_ELEM_TYPE_INTEGER:
  415. orig_int = snd_ctl_elem_value_get_integer(orig_val, i);
  416. read_int = snd_ctl_elem_value_get_integer(read_val, i);
  417. break;
  418. case SND_CTL_ELEM_TYPE_INTEGER64:
  419. orig_int = snd_ctl_elem_value_get_integer64(orig_val,
  420. i);
  421. read_int = snd_ctl_elem_value_get_integer64(read_val,
  422. i);
  423. break;
  424. case SND_CTL_ELEM_TYPE_ENUMERATED:
  425. orig_int = snd_ctl_elem_value_get_enumerated(orig_val,
  426. i);
  427. read_int = snd_ctl_elem_value_get_enumerated(read_val,
  428. i);
  429. break;
  430. default:
  431. return;
  432. }
  433. ksft_print_msg("%s.%d orig %lld read %lld, is_volatile %d\n",
  434. ctl->name, i, orig_int, read_int,
  435. snd_ctl_elem_info_is_volatile(ctl->info));
  436. }
  437. }
  438. static bool show_mismatch(struct ctl_data *ctl, int index,
  439. snd_ctl_elem_value_t *read_val,
  440. snd_ctl_elem_value_t *expected_val)
  441. {
  442. long long expected_int, read_int;
  443. /*
  444. * We factor out the code to compare values representable as
  445. * integers, ensure that check doesn't log otherwise.
  446. */
  447. expected_int = 0;
  448. read_int = 0;
  449. switch (snd_ctl_elem_info_get_type(ctl->info)) {
  450. case SND_CTL_ELEM_TYPE_BOOLEAN:
  451. expected_int = snd_ctl_elem_value_get_boolean(expected_val,
  452. index);
  453. read_int = snd_ctl_elem_value_get_boolean(read_val, index);
  454. break;
  455. case SND_CTL_ELEM_TYPE_INTEGER:
  456. expected_int = snd_ctl_elem_value_get_integer(expected_val,
  457. index);
  458. read_int = snd_ctl_elem_value_get_integer(read_val, index);
  459. break;
  460. case SND_CTL_ELEM_TYPE_INTEGER64:
  461. expected_int = snd_ctl_elem_value_get_integer64(expected_val,
  462. index);
  463. read_int = snd_ctl_elem_value_get_integer64(read_val,
  464. index);
  465. break;
  466. case SND_CTL_ELEM_TYPE_ENUMERATED:
  467. expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
  468. index);
  469. read_int = snd_ctl_elem_value_get_enumerated(read_val,
  470. index);
  471. break;
  472. default:
  473. break;
  474. }
  475. if (expected_int != read_int) {
  476. /*
  477. * NOTE: The volatile attribute means that the hardware
  478. * can voluntarily change the state of control element
  479. * independent of any operation by software.
  480. */
  481. bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
  482. ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
  483. ctl->name, index, expected_int, read_int, is_volatile);
  484. return !is_volatile;
  485. } else {
  486. return false;
  487. }
  488. }
  489. /*
  490. * Write a value then if possible verify that we get the expected
  491. * result. An optional expected value can be provided if we expect
  492. * the write to fail, for verifying that invalid writes don't corrupt
  493. * anything.
  494. */
  495. static int write_and_verify(struct ctl_data *ctl,
  496. snd_ctl_elem_value_t *write_val,
  497. snd_ctl_elem_value_t *expected_val)
  498. {
  499. int err, i;
  500. bool error_expected, mismatch_shown;
  501. snd_ctl_elem_value_t *initial_val, *read_val, *w_val;
  502. snd_ctl_elem_value_alloca(&initial_val);
  503. snd_ctl_elem_value_alloca(&read_val);
  504. snd_ctl_elem_value_alloca(&w_val);
  505. /*
  506. * We need to copy the write value since writing can modify
  507. * the value which causes surprises, and allocate an expected
  508. * value if we expect to read back what we wrote.
  509. */
  510. snd_ctl_elem_value_copy(w_val, write_val);
  511. if (expected_val) {
  512. error_expected = true;
  513. } else {
  514. error_expected = false;
  515. snd_ctl_elem_value_alloca(&expected_val);
  516. snd_ctl_elem_value_copy(expected_val, write_val);
  517. }
  518. /* Store the value before we write */
  519. if (snd_ctl_elem_info_is_readable(ctl->info)) {
  520. snd_ctl_elem_value_set_id(initial_val, ctl->id);
  521. err = snd_ctl_elem_read(ctl->card->handle, initial_val);
  522. if (err < 0) {
  523. ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
  524. snd_strerror(err));
  525. return err;
  526. }
  527. }
  528. /*
  529. * Do the write, if we have an expected value ignore the error
  530. * and carry on to validate the expected value.
  531. */
  532. err = snd_ctl_elem_write(ctl->card->handle, w_val);
  533. if (err < 0 && !error_expected) {
  534. ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
  535. snd_strerror(err));
  536. return err;
  537. }
  538. /* Can we do the verification part? */
  539. if (!snd_ctl_elem_info_is_readable(ctl->info))
  540. return err;
  541. snd_ctl_elem_value_set_id(read_val, ctl->id);
  542. err = snd_ctl_elem_read(ctl->card->handle, read_val);
  543. if (err < 0) {
  544. ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
  545. snd_strerror(err));
  546. return err;
  547. }
  548. /*
  549. * We can't verify any specific value for volatile controls
  550. * but we should still check that whatever we read is a valid
  551. * vale for the control.
  552. */
  553. if (snd_ctl_elem_info_is_volatile(ctl->info)) {
  554. if (!ctl_value_valid(ctl, read_val)) {
  555. ksft_print_msg("Volatile control %s has invalid value\n",
  556. ctl->name);
  557. return -EINVAL;
  558. }
  559. return 0;
  560. }
  561. /*
  562. * Check for an event if the value changed, or confirm that
  563. * there was none if it didn't. We rely on the kernel
  564. * generating the notification before it returns from the
  565. * write, this is currently true, should that ever change this
  566. * will most likely break and need updating.
  567. */
  568. err = wait_for_event(ctl, 0);
  569. if (snd_ctl_elem_value_compare(initial_val, read_val)) {
  570. if (err < 1) {
  571. ksft_print_msg("No event generated for %s\n",
  572. ctl->name);
  573. show_values(ctl, initial_val, read_val);
  574. ctl->event_missing++;
  575. }
  576. } else {
  577. if (err != 0) {
  578. ksft_print_msg("Spurious event generated for %s\n",
  579. ctl->name);
  580. show_values(ctl, initial_val, read_val);
  581. ctl->event_spurious++;
  582. }
  583. }
  584. /*
  585. * Use the libray to compare values, if there's a mismatch
  586. * carry on and try to provide a more useful diagnostic than
  587. * just "mismatch".
  588. */
  589. if (!snd_ctl_elem_value_compare(expected_val, read_val))
  590. return 0;
  591. mismatch_shown = false;
  592. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
  593. if (show_mismatch(ctl, i, read_val, expected_val))
  594. mismatch_shown = true;
  595. if (!mismatch_shown)
  596. ksft_print_msg("%s read and written values differ\n",
  597. ctl->name);
  598. return -1;
  599. }
  600. /*
  601. * Make sure we can write the default value back to the control, this
  602. * should validate that at least some write works.
  603. */
  604. static void test_ctl_write_default(struct ctl_data *ctl)
  605. {
  606. int err;
  607. /* If the control is turned off let's be polite */
  608. if (snd_ctl_elem_info_is_inactive(ctl->info)) {
  609. ksft_print_msg("%s is inactive\n", ctl->name);
  610. ksft_test_result_skip("write_default.%s.%d\n",
  611. ctl->card->card_name, ctl->elem);
  612. return;
  613. }
  614. if (!snd_ctl_elem_info_is_writable(ctl->info)) {
  615. ksft_print_msg("%s is not writeable\n", ctl->name);
  616. ksft_test_result_skip("write_default.%s.%d\n",
  617. ctl->card->card_name, ctl->elem);
  618. return;
  619. }
  620. /* No idea what the default was for unreadable controls */
  621. if (!snd_ctl_elem_info_is_readable(ctl->info)) {
  622. ksft_print_msg("%s couldn't read default\n", ctl->name);
  623. ksft_test_result_skip("write_default.%s.%d\n",
  624. ctl->card->card_name, ctl->elem);
  625. return;
  626. }
  627. err = write_and_verify(ctl, ctl->def_val, NULL);
  628. ksft_test_result(err >= 0, "write_default.%s.%d\n",
  629. ctl->card->card_name, ctl->elem);
  630. }
  631. static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
  632. {
  633. int err, i, j;
  634. bool fail = false;
  635. snd_ctl_elem_value_t *val;
  636. snd_ctl_elem_value_alloca(&val);
  637. snd_ctl_elem_value_set_id(val, ctl->id);
  638. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  639. for (j = 0; j < 2; j++) {
  640. snd_ctl_elem_value_set_boolean(val, i, j);
  641. err = write_and_verify(ctl, val, NULL);
  642. if (err != 0)
  643. fail = true;
  644. }
  645. }
  646. return !fail;
  647. }
  648. static bool test_ctl_write_valid_integer(struct ctl_data *ctl)
  649. {
  650. int err;
  651. int i;
  652. long j, step;
  653. bool fail = false;
  654. snd_ctl_elem_value_t *val;
  655. snd_ctl_elem_value_alloca(&val);
  656. snd_ctl_elem_value_set_id(val, ctl->id);
  657. step = snd_ctl_elem_info_get_step(ctl->info);
  658. if (!step)
  659. step = 1;
  660. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  661. for (j = snd_ctl_elem_info_get_min(ctl->info);
  662. j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
  663. snd_ctl_elem_value_set_integer(val, i, j);
  664. err = write_and_verify(ctl, val, NULL);
  665. if (err != 0)
  666. fail = true;
  667. }
  668. }
  669. return !fail;
  670. }
  671. static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
  672. {
  673. int err, i;
  674. long long j, step;
  675. bool fail = false;
  676. snd_ctl_elem_value_t *val;
  677. snd_ctl_elem_value_alloca(&val);
  678. snd_ctl_elem_value_set_id(val, ctl->id);
  679. step = snd_ctl_elem_info_get_step64(ctl->info);
  680. if (!step)
  681. step = 1;
  682. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  683. for (j = snd_ctl_elem_info_get_min64(ctl->info);
  684. j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
  685. snd_ctl_elem_value_set_integer64(val, i, j);
  686. err = write_and_verify(ctl, val, NULL);
  687. if (err != 0)
  688. fail = true;
  689. }
  690. }
  691. return !fail;
  692. }
  693. static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
  694. {
  695. int err, i, j;
  696. bool fail = false;
  697. snd_ctl_elem_value_t *val;
  698. snd_ctl_elem_value_alloca(&val);
  699. snd_ctl_elem_value_set_id(val, ctl->id);
  700. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  701. for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
  702. snd_ctl_elem_value_set_enumerated(val, i, j);
  703. err = write_and_verify(ctl, val, NULL);
  704. if (err != 0)
  705. fail = true;
  706. }
  707. }
  708. return !fail;
  709. }
  710. static void test_ctl_write_valid(struct ctl_data *ctl)
  711. {
  712. bool pass;
  713. /* If the control is turned off let's be polite */
  714. if (snd_ctl_elem_info_is_inactive(ctl->info)) {
  715. ksft_print_msg("%s is inactive\n", ctl->name);
  716. ksft_test_result_skip("write_valid.%s.%d\n",
  717. ctl->card->card_name, ctl->elem);
  718. return;
  719. }
  720. if (!snd_ctl_elem_info_is_writable(ctl->info)) {
  721. ksft_print_msg("%s is not writeable\n", ctl->name);
  722. ksft_test_result_skip("write_valid.%s.%d\n",
  723. ctl->card->card_name, ctl->elem);
  724. return;
  725. }
  726. switch (snd_ctl_elem_info_get_type(ctl->info)) {
  727. case SND_CTL_ELEM_TYPE_BOOLEAN:
  728. pass = test_ctl_write_valid_boolean(ctl);
  729. break;
  730. case SND_CTL_ELEM_TYPE_INTEGER:
  731. pass = test_ctl_write_valid_integer(ctl);
  732. break;
  733. case SND_CTL_ELEM_TYPE_INTEGER64:
  734. pass = test_ctl_write_valid_integer64(ctl);
  735. break;
  736. case SND_CTL_ELEM_TYPE_ENUMERATED:
  737. pass = test_ctl_write_valid_enumerated(ctl);
  738. break;
  739. default:
  740. /* No tests for this yet */
  741. ksft_test_result_skip("write_valid.%s.%d\n",
  742. ctl->card->card_name, ctl->elem);
  743. return;
  744. }
  745. /* Restore the default value to minimise disruption */
  746. write_and_verify(ctl, ctl->def_val, NULL);
  747. ksft_test_result(pass, "write_valid.%s.%d\n",
  748. ctl->card->card_name, ctl->elem);
  749. }
  750. static bool test_ctl_write_invalid_value(struct ctl_data *ctl,
  751. snd_ctl_elem_value_t *val)
  752. {
  753. int err;
  754. /* Ideally this will fail... */
  755. err = snd_ctl_elem_write(ctl->card->handle, val);
  756. if (err < 0)
  757. return false;
  758. /* ...but some devices will clamp to an in range value */
  759. err = snd_ctl_elem_read(ctl->card->handle, val);
  760. if (err < 0) {
  761. ksft_print_msg("%s failed to read: %s\n",
  762. ctl->name, snd_strerror(err));
  763. return true;
  764. }
  765. return !ctl_value_valid(ctl, val);
  766. }
  767. static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
  768. {
  769. int i;
  770. bool fail = false;
  771. snd_ctl_elem_value_t *val;
  772. snd_ctl_elem_value_alloca(&val);
  773. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  774. snd_ctl_elem_value_copy(val, ctl->def_val);
  775. snd_ctl_elem_value_set_boolean(val, i, 2);
  776. if (test_ctl_write_invalid_value(ctl, val))
  777. fail = true;
  778. }
  779. return !fail;
  780. }
  781. static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
  782. {
  783. int i;
  784. bool fail = false;
  785. snd_ctl_elem_value_t *val;
  786. snd_ctl_elem_value_alloca(&val);
  787. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  788. if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
  789. /* Just under range */
  790. snd_ctl_elem_value_copy(val, ctl->def_val);
  791. snd_ctl_elem_value_set_integer(val, i,
  792. snd_ctl_elem_info_get_min(ctl->info) - 1);
  793. if (test_ctl_write_invalid_value(ctl, val))
  794. fail = true;
  795. /* Minimum representable value */
  796. snd_ctl_elem_value_copy(val, ctl->def_val);
  797. snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
  798. if (test_ctl_write_invalid_value(ctl, val))
  799. fail = true;
  800. }
  801. if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
  802. /* Just over range */
  803. snd_ctl_elem_value_copy(val, ctl->def_val);
  804. snd_ctl_elem_value_set_integer(val, i,
  805. snd_ctl_elem_info_get_max(ctl->info) + 1);
  806. if (test_ctl_write_invalid_value(ctl, val))
  807. fail = true;
  808. /* Maximum representable value */
  809. snd_ctl_elem_value_copy(val, ctl->def_val);
  810. snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
  811. if (test_ctl_write_invalid_value(ctl, val))
  812. fail = true;
  813. }
  814. }
  815. return !fail;
  816. }
  817. static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
  818. {
  819. int i;
  820. bool fail = false;
  821. snd_ctl_elem_value_t *val;
  822. snd_ctl_elem_value_alloca(&val);
  823. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  824. if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
  825. /* Just under range */
  826. snd_ctl_elem_value_copy(val, ctl->def_val);
  827. snd_ctl_elem_value_set_integer64(val, i,
  828. snd_ctl_elem_info_get_min64(ctl->info) - 1);
  829. if (test_ctl_write_invalid_value(ctl, val))
  830. fail = true;
  831. /* Minimum representable value */
  832. snd_ctl_elem_value_copy(val, ctl->def_val);
  833. snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
  834. if (test_ctl_write_invalid_value(ctl, val))
  835. fail = true;
  836. }
  837. if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
  838. /* Just over range */
  839. snd_ctl_elem_value_copy(val, ctl->def_val);
  840. snd_ctl_elem_value_set_integer64(val, i,
  841. snd_ctl_elem_info_get_max64(ctl->info) + 1);
  842. if (test_ctl_write_invalid_value(ctl, val))
  843. fail = true;
  844. /* Maximum representable value */
  845. snd_ctl_elem_value_copy(val, ctl->def_val);
  846. snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
  847. if (test_ctl_write_invalid_value(ctl, val))
  848. fail = true;
  849. }
  850. }
  851. return !fail;
  852. }
  853. static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
  854. {
  855. int i;
  856. bool fail = false;
  857. snd_ctl_elem_value_t *val;
  858. snd_ctl_elem_value_alloca(&val);
  859. snd_ctl_elem_value_set_id(val, ctl->id);
  860. for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
  861. /* One beyond maximum */
  862. snd_ctl_elem_value_copy(val, ctl->def_val);
  863. snd_ctl_elem_value_set_enumerated(val, i,
  864. snd_ctl_elem_info_get_items(ctl->info));
  865. if (test_ctl_write_invalid_value(ctl, val))
  866. fail = true;
  867. /* Maximum representable value */
  868. snd_ctl_elem_value_copy(val, ctl->def_val);
  869. snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
  870. if (test_ctl_write_invalid_value(ctl, val))
  871. fail = true;
  872. }
  873. return !fail;
  874. }
  875. static void test_ctl_write_invalid(struct ctl_data *ctl)
  876. {
  877. bool pass;
  878. /* If the control is turned off let's be polite */
  879. if (snd_ctl_elem_info_is_inactive(ctl->info)) {
  880. ksft_print_msg("%s is inactive\n", ctl->name);
  881. ksft_test_result_skip("write_invalid.%s.%d\n",
  882. ctl->card->card_name, ctl->elem);
  883. return;
  884. }
  885. if (!snd_ctl_elem_info_is_writable(ctl->info)) {
  886. ksft_print_msg("%s is not writeable\n", ctl->name);
  887. ksft_test_result_skip("write_invalid.%s.%d\n",
  888. ctl->card->card_name, ctl->elem);
  889. return;
  890. }
  891. switch (snd_ctl_elem_info_get_type(ctl->info)) {
  892. case SND_CTL_ELEM_TYPE_BOOLEAN:
  893. pass = test_ctl_write_invalid_boolean(ctl);
  894. break;
  895. case SND_CTL_ELEM_TYPE_INTEGER:
  896. pass = test_ctl_write_invalid_integer(ctl);
  897. break;
  898. case SND_CTL_ELEM_TYPE_INTEGER64:
  899. pass = test_ctl_write_invalid_integer64(ctl);
  900. break;
  901. case SND_CTL_ELEM_TYPE_ENUMERATED:
  902. pass = test_ctl_write_invalid_enumerated(ctl);
  903. break;
  904. default:
  905. /* No tests for this yet */
  906. ksft_test_result_skip("write_invalid.%s.%d\n",
  907. ctl->card->card_name, ctl->elem);
  908. return;
  909. }
  910. /* Restore the default value to minimise disruption */
  911. write_and_verify(ctl, ctl->def_val, NULL);
  912. ksft_test_result(pass, "write_invalid.%s.%d\n",
  913. ctl->card->card_name, ctl->elem);
  914. }
  915. static void test_ctl_event_missing(struct ctl_data *ctl)
  916. {
  917. ksft_test_result(!ctl->event_missing, "event_missing.%s.%d\n",
  918. ctl->card->card_name, ctl->elem);
  919. }
  920. static void test_ctl_event_spurious(struct ctl_data *ctl)
  921. {
  922. ksft_test_result(!ctl->event_spurious, "event_spurious.%s.%d\n",
  923. ctl->card->card_name, ctl->elem);
  924. }
  925. int main(void)
  926. {
  927. struct ctl_data *ctl;
  928. ksft_print_header();
  929. find_controls();
  930. ksft_set_plan(num_controls * TESTS_PER_CONTROL);
  931. for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
  932. /*
  933. * Must test get_value() before we write anything, the
  934. * test stores the default value for later cleanup.
  935. */
  936. test_ctl_get_value(ctl);
  937. test_ctl_name(ctl);
  938. test_ctl_write_default(ctl);
  939. test_ctl_write_valid(ctl);
  940. test_ctl_write_invalid(ctl);
  941. test_ctl_event_missing(ctl);
  942. test_ctl_event_spurious(ctl);
  943. }
  944. ksft_exit_pass();
  945. return 0;
  946. }