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(bool, PVCHAN); 152 NV(number, PVCHANRATE); 153 NV(number, PVCHANFORMAT); 154 NV(bool, 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_RATE); 188 NV(number, HWBUF_SIZE); 189 NV(number, HWBUF_BLKSZ); 190 NV(number, HWBUF_BLKCNT); 191 NV(number, HWBUF_FREE); 192 NV(number, HWBUF_READY); 193 NV(number, SWBUF_FORMAT); 194 NV(number, SWBUF_RATE); 195 NV(number, SWBUF_SIZE); 196 NV(number, SWBUF_BLKSZ); 197 NV(number, SWBUF_BLKCNT); 198 NV(number, SWBUF_FREE); 199 NV(number, SWBUF_READY); 200 NV(string, FEEDERCHAIN); 201 #undef NV 202 } 203 } 204 205 free(arg.buf); 206 nvlist_destroy(nvl); 207 close(fd); 208 } 209 210 #define UDEV_PROVIDER "sndstat_udev" 211 #define UDEV_NAMEUNIT "sndstat_udev" 212 #define UDEV_DEVNODE "sndstat_udev" 213 #define UDEV_DESC "Test Device" 214 #define UDEV_PCHAN 1 215 #define UDEV_RCHAN 1 216 #define UDEV_MIN_RATE 8000 217 #define UDEV_MAX_RATE 96000 218 #define UDEV_FORMATS (AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE) 219 #define UDEV_MIN_CHN 1 220 #define UDEV_MAX_CHN 2 221 222 ATF_TC(sndstat_udev); 223 ATF_TC_HEAD(sndstat_udev, tc) 224 { 225 atf_tc_set_md_var(tc, "descr", "/dev/sndstat userdev interface test"); 226 } 227 228 ATF_TC_BODY(sndstat_udev, tc) 229 { 230 nvlist_t *nvl, *di, *dichild; 231 const nvlist_t * const *rdi; 232 struct sndstioc_nv_arg arg; 233 const char *str; 234 size_t nitems, i; 235 int fd, rc, pchan, rchan, n; 236 237 load_dummy(); 238 239 if ((fd = open("/dev/sndstat", O_RDWR)) < 0) 240 atf_tc_skip("/dev/sndstat not found, load sound(4)"); 241 242 nvl = nvlist_create(0); 243 ATF_REQUIRE(nvl != NULL); 244 245 di = nvlist_create(0); 246 ATF_REQUIRE(di != NULL); 247 248 dichild = nvlist_create(0); 249 ATF_REQUIRE(dichild != NULL); 250 251 nvlist_add_string(di, SNDST_DSPS_PROVIDER, UDEV_PROVIDER); 252 nvlist_add_string(di, SNDST_DSPS_NAMEUNIT, UDEV_NAMEUNIT); 253 nvlist_add_string(di, SNDST_DSPS_DESC, UDEV_DESC); 254 nvlist_add_string(di, SNDST_DSPS_DEVNODE, UDEV_DEVNODE); 255 nvlist_add_number(di, SNDST_DSPS_PCHAN, UDEV_PCHAN); 256 nvlist_add_number(di, SNDST_DSPS_RCHAN, UDEV_RCHAN); 257 258 nvlist_add_number(dichild, SNDST_DSPS_INFO_MIN_RATE, UDEV_MIN_RATE); 259 nvlist_add_number(dichild, SNDST_DSPS_INFO_MAX_RATE, UDEV_MAX_RATE); 260 nvlist_add_number(dichild, SNDST_DSPS_INFO_FORMATS, UDEV_FORMATS); 261 nvlist_add_number(dichild, SNDST_DSPS_INFO_MIN_CHN, UDEV_MIN_CHN); 262 nvlist_add_number(dichild, SNDST_DSPS_INFO_MAX_CHN, UDEV_MAX_CHN); 263 264 nvlist_add_nvlist(di, SNDST_DSPS_INFO_PLAY, dichild); 265 nvlist_add_nvlist(di, SNDST_DSPS_INFO_REC, dichild); 266 267 nvlist_append_nvlist_array(nvl, SNDST_DSPS, di); 268 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 269 270 arg.buf = nvlist_pack(nvl, &arg.nbytes); 271 ATF_REQUIRE_MSG(arg.buf != NULL, "failed to pack nvlist"); 272 273 rc = ioctl(fd, SNDSTIOC_ADD_USER_DEVS, &arg); 274 free(arg.buf); 275 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_ADD_USER_DEVS) failed"); 276 277 nvlist_destroy(di); 278 nvlist_destroy(dichild); 279 nvlist_destroy(nvl); 280 281 /* Read back registered values. */ 282 rc = ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL); 283 ATF_REQUIRE_EQ(rc, 0); 284 285 arg.nbytes = 0; 286 arg.buf = NULL; 287 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 288 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#1) failed"); 289 290 arg.buf = malloc(arg.nbytes); 291 ATF_REQUIRE(arg.buf != NULL); 292 293 rc = ioctl(fd, SNDSTIOC_GET_DEVS, &arg); 294 ATF_REQUIRE_EQ_MSG(rc, 0, "ioctl(SNDSTIOC_GET_DEVS#2) failed"); 295 296 nvl = nvlist_unpack(arg.buf, arg.nbytes, 0); 297 ATF_REQUIRE(nvl != NULL); 298 299 if (nvlist_empty(nvl) || !nvlist_exists(nvl, SNDST_DSPS)) 300 atf_tc_skip("no soundcards attached"); 301 302 rdi = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems); 303 for (i = 0; i < nitems; i++) { 304 #define NV(type, item, var) do { \ 305 ATF_REQUIRE_MSG(nvlist_exists(rdi[i], SNDST_DSPS_ ## item), \ 306 "SNDST_DSPS_" #item " does not exist"); \ 307 var = nvlist_get_ ## type (rdi[i], SNDST_DSPS_ ## item); \ 308 } while (0) 309 /* Search for our device. */ 310 NV(string, NAMEUNIT, str); 311 if (strcmp(str, UDEV_NAMEUNIT) == 0) 312 break; 313 } 314 if (i == nitems) 315 atf_tc_fail("userland device %s not found", UDEV_NAMEUNIT); 316 317 NV(string, NAMEUNIT, str); 318 ATF_CHECK(strcmp(str, UDEV_NAMEUNIT) == 0); 319 320 NV(bool, FROM_USER, n); 321 ATF_CHECK(n); 322 323 NV(string, DEVNODE, str); 324 ATF_CHECK(strcmp(str, UDEV_DEVNODE) == 0); 325 326 NV(string, DESC, str); 327 ATF_CHECK(strcmp(str, UDEV_DESC) == 0); 328 329 NV(string, PROVIDER, str); 330 ATF_CHECK(strcmp(str, UDEV_PROVIDER) == 0); 331 332 NV(number, PCHAN, pchan); 333 ATF_CHECK(pchan == UDEV_PCHAN); 334 if (pchan && !nvlist_exists(rdi[i], SNDST_DSPS_INFO_PLAY)) 335 atf_tc_fail("playback channel list empty"); 336 337 NV(number, RCHAN, rchan); 338 ATF_CHECK(rchan == UDEV_RCHAN); 339 if (rchan && !nvlist_exists(rdi[i], SNDST_DSPS_INFO_REC)) 340 atf_tc_fail("recording channel list empty"); 341 #undef NV 342 343 #define NV(type, mode, item, var) do { \ 344 ATF_REQUIRE_MSG(nvlist_exists(nvlist_get_nvlist(rdi[i], \ 345 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item), \ 346 "SNDST_DSPS_INFO_" #item " does not exist"); \ 347 var = nvlist_get_ ## type (nvlist_get_nvlist(rdi[i], \ 348 SNDST_DSPS_INFO_ ## mode), SNDST_DSPS_INFO_ ## item); \ 349 } while (0) 350 if (pchan) { 351 NV(number, PLAY, MIN_RATE, n); 352 ATF_CHECK(n == UDEV_MIN_RATE); 353 354 NV(number, PLAY, MAX_RATE, n); 355 ATF_CHECK(n == UDEV_MAX_RATE); 356 357 NV(number, PLAY, FORMATS, n); 358 ATF_CHECK(n == UDEV_FORMATS); 359 360 NV(number, PLAY, MIN_CHN, n); 361 ATF_CHECK(n == UDEV_MIN_CHN); 362 363 NV(number, PLAY, MAX_CHN, n); 364 ATF_CHECK(n == UDEV_MAX_CHN); 365 } 366 if (rchan) { 367 NV(number, REC, MIN_RATE, n); 368 ATF_CHECK(n == UDEV_MIN_RATE); 369 370 NV(number, REC, MAX_RATE, n); 371 ATF_CHECK(n == UDEV_MAX_RATE); 372 373 NV(number, REC, FORMATS, n); 374 ATF_CHECK(n == UDEV_FORMATS); 375 376 NV(number, REC, MIN_CHN, n); 377 ATF_CHECK(n == UDEV_MIN_CHN); 378 379 NV(number, REC, MAX_CHN, n); 380 ATF_CHECK(n == UDEV_MAX_CHN); 381 } 382 #undef NV 383 384 free(arg.buf); 385 nvlist_destroy(nvl); 386 close(fd); 387 } 388 389 ATF_TP_ADD_TCS(tp) 390 { 391 ATF_TP_ADD_TC(tp, sndstat_nv); 392 ATF_TP_ADD_TC(tp, sndstat_udev); 393 394 return (atf_no_error()); 395 } 396