1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2024 The FreeBSD Foundation 5 * 6 * This software was developed by Christos Margiolis <christos@FreeBSD.org> 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/linker.h> 33 #include <sys/nv.h> 34 #include <sys/sndstat.h> 35 #include <sys/soundcard.h> 36 37 #include <atf-c.h> 38 #include <errno.h> 39 #include <fcntl.h> 40 #include <stdlib.h> 41 #include <unistd.h> 42 43 static void 44 load_dummy(void) 45 { 46 if (kldload("snd_dummy.ko") < 0 && errno != EEXIST) 47 atf_tc_skip("snd_dummy.ko not found"); 48 } 49 50 ATF_TC(sndstat_nv); 51 ATF_TC_HEAD(sndstat_nv, tc) 52 { 53 atf_tc_set_md_var(tc, "descr", "/dev/sndstat nvlist test"); 54 } 55 56 ATF_TC_BODY(sndstat_nv, tc) 57 { 58 nvlist_t *nvl; 59 const nvlist_t * const *di; 60 const nvlist_t * const *cdi; 61 struct sndstioc_nv_arg arg; 62 size_t nitems, nchans, i, j; 63 int fd, rc, pchan, rchan; 64 65 load_dummy(); 66 67 if ((fd = open("/dev/sndstat", O_RDONLY)) < 0) 68 atf_tc_skip("/dev/sndstat not found, load sound(4)"); 69 70 rc = ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL); 71 ATF_REQUIRE_EQ(rc, 0); 72 73 arg.nbytes = 0; 74 arg.buf = NULL; 75 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 76 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#1) failed"); 77 78 arg.buf = malloc(arg.nbytes); 79 ATF_REQUIRE(arg.buf != NULL); 80 81 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 82 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#2) failed"); 83 84 nvl = nvlist_unpack(arg.buf, arg.nbytes, 0); 85 ATF_REQUIRE(nvl != NULL); 86 87 if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS)) 88 atf_tc_skip("no soundcards attached"); 89 90 di = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems); 91 for (i = 0; i < nitems; i++) { 92 #define NV(type, item) do { \ 93 ATF_REQUIRE_MSG(nvlist_exists(di[i], SNDST_DSPS_ ## item), \ 94 "SNDST_DSPS_" #item " does not exist"); \ 95 nvlist_get_ ## type (di[i], SNDST_DSPS_ ## item); \ 96 } while (0) 97 NV(string, NAMEUNIT); 98 NV(bool, FROM_USER); 99 NV(string, DEVNODE); 100 NV(string, DESC); 101 NV(string, PROVIDER); 102 NV(number, PCHAN); 103 NV(number, RCHAN); 104 #undef NV 105 106 /* Cannot asign using the macro. */ 107 pchan = nvlist_get_number(di[i], SNDST_DSPS_PCHAN); 108 rchan = nvlist_get_number(di[i], SNDST_DSPS_RCHAN); 109 110 if (pchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_PLAY)) 111 atf_tc_fail("playback channel list empty"); 112 if (rchan && !nvlist_exists(di[i], SNDST_DSPS_INFO_REC)) 113 atf_tc_fail("recording channel list empty"); 114 115 #define NV(type, mode, item) do { \ 116 ATF_REQUIRE_MSG(nvlist_exists(nvlist_get_nvlist(di[i], \ 117 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item), \ 118 "SNDST_DSPS_INFO_" #item " does not exist"); \ 119 nvlist_get_ ## type (nvlist_get_nvlist(di[i], \ 120 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item); \ 121 } while (0) 122 if (pchan) { 123 NV(number, PLAY, MIN_RATE); 124 NV(number, PLAY, MAX_RATE); 125 NV(number, PLAY, FORMATS); 126 NV(number, PLAY, MIN_CHN); 127 NV(number, PLAY, MAX_CHN); 128 } 129 if (rchan) { 130 NV(number, REC, MIN_RATE); 131 NV(number, REC, MAX_RATE); 132 NV(number, REC, FORMATS); 133 NV(number, REC, MIN_CHN); 134 NV(number, REC, MAX_CHN); 135 } 136 #undef NV 137 138 if (!nvlist_exists(di[i], SNDST_DSPS_PROVIDER_INFO)) 139 continue; 140 141 #define NV(type, item) do { \ 142 ATF_REQUIRE_MSG(nvlist_exists(nvlist_get_nvlist(di[i], \ 143 SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_ ## item), \ 144 "SNDST_DSPS_SOUND4_" #item " does not exist"); \ 145 nvlist_get_ ## type (nvlist_get_nvlist(di[i], \ 146 SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_ ## item); \ 147 } while (0) 148 NV(number, UNIT); 149 NV(string, STATUS); 150 NV(bool, BITPERFECT); 151 NV(number, PVCHAN); 152 NV(number, PVCHANRATE); 153 NV(number, PVCHANFORMAT); 154 NV(number, RVCHAN); 155 NV(number, PVCHANRATE); 156 NV(number, PVCHANFORMAT); 157 #undef NV 158 159 if (!nvlist_exists(nvlist_get_nvlist(di[i], 160 SNDST_DSPS_PROVIDER_INFO), SNDST_DSPS_SOUND4_CHAN_INFO)) 161 atf_tc_fail("channel info list empty"); 162 163 cdi = nvlist_get_nvlist_array( 164 nvlist_get_nvlist(di[i], SNDST_DSPS_PROVIDER_INFO), 165 SNDST_DSPS_SOUND4_CHAN_INFO, &nchans); 166 for (j = 0; j < nchans; j++) { 167 #define NV(type, item) do { \ 168 ATF_REQUIRE_MSG(nvlist_exists(cdi[j], SNDST_DSPS_SOUND4_CHAN_ ## item), \ 169 "SNDST_DSPS_SOUND4_CHAN_" #item " does not exist"); \ 170 nvlist_get_ ## type (cdi[j], SNDST_DSPS_SOUND4_CHAN_ ## item); \ 171 } while (0) 172 NV(string, NAME); 173 NV(string, PARENTCHAN); 174 NV(number, UNIT); 175 NV(number, CAPS); 176 NV(number, LATENCY); 177 NV(number, RATE); 178 NV(number, FORMAT); 179 NV(number, PID); 180 NV(string, COMM); 181 NV(number, INTR); 182 NV(number, XRUNS); 183 NV(number, FEEDCNT); 184 NV(number, LEFTVOL); 185 NV(number, RIGHTVOL); 186 NV(number, HWBUF_FORMAT); 187 NV(number, HWBUF_SIZE); 188 NV(number, HWBUF_BLKSZ); 189 NV(number, HWBUF_BLKCNT); 190 NV(number, HWBUF_FREE); 191 NV(number, HWBUF_READY); 192 NV(number, SWBUF_FORMAT); 193 NV(number, SWBUF_SIZE); 194 NV(number, SWBUF_BLKSZ); 195 NV(number, SWBUF_BLKCNT); 196 NV(number, SWBUF_FREE); 197 NV(number, SWBUF_READY); 198 NV(string, FEEDERCHAIN); 199 #undef NV 200 } 201 } 202 203 free(arg.buf); 204 nvlist_destroy(nvl); 205 close(fd); 206 } 207 208 #define UDEV_PROVIDER "sndstat_udev" 209 #define UDEV_NAMEUNIT "sndstat_udev" 210 #define UDEV_DEVNODE "sndstat_udev" 211 #define UDEV_DESC "Test Device" 212 #define UDEV_PCHAN 1 213 #define UDEV_RCHAN 1 214 #define UDEV_MIN_RATE 8000 215 #define UDEV_MAX_RATE 96000 216 #define UDEV_FORMATS (AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE) 217 #define UDEV_MIN_CHN 1 218 #define UDEV_MAX_CHN 2 219 220 ATF_TC(sndstat_udev); 221 ATF_TC_HEAD(sndstat_udev, tc) 222 { 223 atf_tc_set_md_var(tc, "descr", "/dev/sndstat userdev interface test"); 224 } 225 226 ATF_TC_BODY(sndstat_udev, tc) 227 { 228 nvlist_t *nvl, *di, *dichild; 229 const nvlist_t * const *rdi; 230 struct sndstioc_nv_arg arg; 231 const char *str; 232 size_t nitems, i; 233 int fd, rc, pchan, rchan, n; 234 235 load_dummy(); 236 237 if ((fd = open("/dev/sndstat", O_RDWR)) < 0) 238 atf_tc_skip("/dev/sndstat not found, load sound(4)"); 239 240 nvl = nvlist_create(0); 241 ATF_REQUIRE(nvl != NULL); 242 243 di = nvlist_create(0); 244 ATF_REQUIRE(di != NULL); 245 246 dichild = nvlist_create(0); 247 ATF_REQUIRE(dichild != NULL); 248 249 nvlist_add_string(di, SNDST_DSPS_PROVIDER, UDEV_PROVIDER); 250 nvlist_add_string(di, SNDST_DSPS_NAMEUNIT, UDEV_NAMEUNIT); 251 nvlist_add_string(di, SNDST_DSPS_DESC, UDEV_DESC); 252 nvlist_add_string(di, SNDST_DSPS_DEVNODE, UDEV_DEVNODE); 253 nvlist_add_number(di, SNDST_DSPS_PCHAN, UDEV_PCHAN); 254 nvlist_add_number(di, SNDST_DSPS_RCHAN, UDEV_RCHAN); 255 256 nvlist_add_number(dichild, SNDST_DSPS_INFO_MIN_RATE, UDEV_MIN_RATE); 257 nvlist_add_number(dichild, SNDST_DSPS_INFO_MAX_RATE, UDEV_MAX_RATE); 258 nvlist_add_number(dichild, SNDST_DSPS_INFO_FORMATS, UDEV_FORMATS); 259 nvlist_add_number(dichild, SNDST_DSPS_INFO_MIN_CHN, UDEV_MIN_CHN); 260 nvlist_add_number(dichild, SNDST_DSPS_INFO_MAX_CHN, UDEV_MAX_CHN); 261 262 nvlist_add_nvlist(di, SNDST_DSPS_INFO_PLAY, dichild); 263 nvlist_add_nvlist(di, SNDST_DSPS_INFO_REC, dichild); 264 265 nvlist_append_nvlist_array(nvl, SNDST_DSPS, di); 266 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 267 268 arg.buf = nvlist_pack(nvl, &arg.nbytes); 269 ATF_REQUIRE_MSG(arg.buf != NULL, "failed to pack nvlist"); 270 271 rc = ioctl(fd, SNDSTIOC_ADD_USER_DEVS, &arg); 272 free(arg.buf); 273 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_ADD_USER_DEVS) failed"); 274 275 nvlist_destroy(di); 276 nvlist_destroy(dichild); 277 nvlist_destroy(nvl); 278 279 /* Read back registered values. */ 280 rc = ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL); 281 ATF_REQUIRE_EQ(rc, 0); 282 283 arg.nbytes = 0; 284 arg.buf = NULL; 285 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 286 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#1) failed"); 287 288 arg.buf = malloc(arg.nbytes); 289 ATF_REQUIRE(arg.buf != NULL); 290 291 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 292 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#2) failed"); 293 294 nvl = nvlist_unpack(arg.buf, arg.nbytes, 0); 295 ATF_REQUIRE(nvl != NULL); 296 297 if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS)) 298 atf_tc_skip("no soundcards attached"); 299 300 rdi = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems); 301 for (i = 0; i < nitems; i++) { 302 #define NV(type, item, var) do { \ 303 ATF_REQUIRE_MSG(nvlist_exists(rdi[i], SNDST_DSPS_ ## item), \ 304 "SNDST_DSPS_" #item " does not exist"); \ 305 var = nvlist_get_ ## type (rdi[i], SNDST_DSPS_ ## item); \ 306 } while (0) 307 /* Search for our device. */ 308 NV(string, NAMEUNIT, str); 309 if (strcmp(str, UDEV_NAMEUNIT) == 0) 310 break; 311 } 312 if (i == nitems) 313 atf_tc_fail("userland device %s not found", UDEV_NAMEUNIT); 314 315 NV(string, NAMEUNIT, str); 316 ATF_CHECK(strcmp(str, UDEV_NAMEUNIT) == 0); 317 318 NV(bool, FROM_USER, n); 319 ATF_CHECK(n); 320 321 NV(string, DEVNODE, str); 322 ATF_CHECK(strcmp(str, UDEV_DEVNODE) == 0); 323 324 NV(string, DESC, str); 325 ATF_CHECK(strcmp(str, UDEV_DESC) == 0); 326 327 NV(string, PROVIDER, str); 328 ATF_CHECK(strcmp(str, UDEV_PROVIDER) == 0); 329 330 NV(number, PCHAN, pchan); 331 ATF_CHECK(pchan == UDEV_PCHAN); 332 if (pchan && !nvlist_exists(rdi[i], SNDST_DSPS_INFO_PLAY)) 333 atf_tc_fail("playback channel list empty"); 334 335 NV(number, RCHAN, rchan); 336 ATF_CHECK(rchan == UDEV_RCHAN); 337 if (rchan && !nvlist_exists(rdi[i], SNDST_DSPS_INFO_REC)) 338 atf_tc_fail("recording channel list empty"); 339 #undef NV 340 341 #define NV(type, mode, item, var) do { \ 342 ATF_REQUIRE_MSG(nvlist_exists(nvlist_get_nvlist(rdi[i], \ 343 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item), \ 344 "SNDST_DSPS_INFO_" #item " does not exist"); \ 345 var = nvlist_get_ ## type (nvlist_get_nvlist(rdi[i], \ 346 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item); \ 347 } while (0) 348 if (pchan) { 349 NV(number, PLAY, MIN_RATE, n); 350 ATF_CHECK(n == UDEV_MIN_RATE); 351 352 NV(number, PLAY, MAX_RATE, n); 353 ATF_CHECK(n == UDEV_MAX_RATE); 354 355 NV(number, PLAY, FORMATS, n); 356 ATF_CHECK(n == UDEV_FORMATS); 357 358 NV(number, PLAY, MIN_CHN, n); 359 ATF_CHECK(n == UDEV_MIN_CHN); 360 361 NV(number, PLAY, MAX_CHN, n); 362 ATF_CHECK(n == UDEV_MAX_CHN); 363 } 364 if (rchan) { 365 NV(number, REC, MIN_RATE, n); 366 ATF_CHECK(n == UDEV_MIN_RATE); 367 368 NV(number, REC, MAX_RATE, n); 369 ATF_CHECK(n == UDEV_MAX_RATE); 370 371 NV(number, REC, FORMATS, n); 372 ATF_CHECK(n == UDEV_FORMATS); 373 374 NV(number, REC, MIN_CHN, n); 375 ATF_CHECK(n == UDEV_MIN_CHN); 376 377 NV(number, REC, MAX_CHN, n); 378 ATF_CHECK(n == UDEV_MAX_CHN); 379 } 380 #undef NV 381 382 free(arg.buf); 383 nvlist_destroy(nvl); 384 close(fd); 385 } 386 387 ATF_TP_ADD_TCS(tp) 388 { 389 ATF_TP_ADD_TC(tp, sndstat_nv); 390 ATF_TP_ADD_TC(tp, sndstat_udev); 391 392 return (atf_no_error()); 393 } 394