audiotest.c revision 1.10       1 /*	$NetBSD: audiotest.c,v 1.10 2020/03/26 13:43:10 isaki Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2019 Tetsuya Isaki. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __RCSID("$NetBSD: audiotest.c,v 1.10 2020/03/26 13:43:10 isaki Exp $");
     30 
     31 #include <errno.h>
     32 #include <fcntl.h>
     33 #define __STDC_FORMAT_MACROS	/* for PRIx64 */
     34 #include <inttypes.h>
     35 #include <pthread.h>
     36 #include <stdarg.h>
     37 #include <stdbool.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 #include <util.h>
     43 #include <sys/audioio.h>
     44 #include <sys/event.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/mman.h>
     47 #include <sys/poll.h>
     48 #include <sys/sysctl.h>
     49 #include <sys/time.h>
     50 #include <sys/wait.h>
     51 #if !defined(NO_RUMP)
     52 #include <rump/rump.h>
     53 #include <rump/rump_syscalls.h>
     54 #endif
     55 
     56 #if !defined(AUDIO_ENCODING_SLINEAR_NE)
     57 #if BYTE_ORDER == LITTLE_ENDIAN
     58 #define AUDIO_ENCODING_SLINEAR_NE AUDIO_ENCODING_SLINEAR_LE
     59 #define AUDIO_ENCODING_ULINEAR_NE AUDIO_ENCODING_ULINEAR_LE
     60 #define AUDIO_ENCODING_SLINEAR_OE AUDIO_ENCODING_SLINEAR_BE
     61 #define AUDIO_ENCODING_ULINEAR_OE AUDIO_ENCODING_ULINEAR_BE
     62 #else
     63 #define AUDIO_ENCODING_SLINEAR_NE AUDIO_ENCODING_SLINEAR_BE
     64 #define AUDIO_ENCODING_ULINEAR_NE AUDIO_ENCODING_ULINEAR_BE
     65 #define AUDIO_ENCODING_SLINEAR_OE AUDIO_ENCODING_SLINEAR_LE
     66 #define AUDIO_ENCODING_ULINEAR_OE AUDIO_ENCODING_ULINEAR_LE
     67 #endif
     68 #endif
     69 
     70 struct testentry {
     71 	const char *name;
     72 	void (*func)(void);
     73 };
     74 
     75 void usage(void) __dead;
     76 void xp_err(int, int, const char *, ...) __printflike(3, 4) __dead;
     77 void xp_errx(int, int, const char *, ...) __printflike(3, 4) __dead;
     78 bool match(const char *, const char *);
     79 void xxx_close_wait(void);
     80 int mixer_get_outputs_master(int);
     81 void do_test(int);
     82 int rump_or_open(const char *, int);
     83 int rump_or_write(int, const void *, size_t);
     84 int rump_or_read(int, void *, size_t);
     85 int rump_or_ioctl(int, u_long, void *);
     86 int rump_or_close(int);
     87 int rump_or_fcntl(int, int, ...);
     88 int rump_or_poll(struct pollfd *, nfds_t, int);
     89 int rump_or_kqueue(void);
     90 int rump_or_kevent(int, const struct kevent *, size_t,
     91 	struct kevent *, size_t, const struct timespec *);
     92 int hw_canplay(void);
     93 int hw_canrec(void);
     94 int hw_bidir(void);
     95 int hw_fulldup(void);
     96 void init(int);
     97 void *consumer_thread(void *);
     98 void cleanup_audiofd(void);
     99 void TEST(const char *, ...) __printflike(1, 2);
    100 bool xp_fail(int, const char *, ...) __printflike(2, 3);
    101 void xp_skip(int, const char *, ...) __printflike(2, 3);
    102 bool xp_eq(int, int, int, const char *);
    103 bool xp_eq_str(int, const char *, const char *, const char *);
    104 bool xp_ne(int, int, int, const char *);
    105 bool xp_if(int, bool, const char *);
    106 bool xp_sys_eq(int, int, int, const char *);
    107 bool xp_sys_ok(int, int, const char *);
    108 bool xp_sys_ng(int, int, int, const char *);
    109 bool xp_sys_ptr(int, int, void *, const char *);
    110 int debug_open(int, const char *, int);
    111 int debug_write(int, int, const void *, size_t);
    112 int debug_read(int, int, void *, size_t);
    113 int debug_ioctl(int, int, u_long, const char *, void *, const char *, ...)
    114 	__printflike(6, 7);
    115 int debug_fcntl(int, int, int, const char *, ...) __printflike(4, 5);
    116 int debug_close(int, int);
    117 void *debug_mmap(int, void *, size_t, int, int, int, off_t);
    118 int debug_munmap(int, void *, int);
    119 const char *event_tostr(int);
    120 int debug_poll(int, struct pollfd *, int, int);
    121 int debug_kqueue(int);
    122 int debug_kevent_set(int, int, const struct kevent *, size_t);
    123 int debug_kevent_poll(int, int, struct kevent *, size_t,
    124 	const struct timespec *);
    125 void debug_kev(int, const char *, const struct kevent *);
    126 uid_t debug_getuid(int);
    127 int debug_seteuid(int, uid_t);
    128 int debug_sysctlbyname(int, const char *, void *, size_t *, const void *,
    129 	size_t);
    130 
    131 int openable_mode(void);
    132 int mode2aumode(int);
    133 int mode2play(int);
    134 int mode2rec(int);
    135 void reset_after_mmap(void);
    136 
    137 /* from audio.c */
    138 static const char *encoding_names[] __unused = {
    139 	"none",
    140 	AudioEmulaw,
    141 	AudioEalaw,
    142 	"pcm16",
    143 	"pcm8",
    144 	AudioEadpcm,
    145 	AudioEslinear_le,
    146 	AudioEslinear_be,
    147 	AudioEulinear_le,
    148 	AudioEulinear_be,
    149 	AudioEslinear,
    150 	AudioEulinear,
    151 	AudioEmpeg_l1_stream,
    152 	AudioEmpeg_l1_packets,
    153 	AudioEmpeg_l1_system,
    154 	AudioEmpeg_l2_stream,
    155 	AudioEmpeg_l2_packets,
    156 	AudioEmpeg_l2_system,
    157 	AudioEac3,
    158 };
    159 
    160 int debug;
    161 int props;
    162 int hwfull;
    163 int netbsd;
    164 bool opt_atf;
    165 char testname[64];
    166 int testcount;
    167 int failcount;
    168 int skipcount;
    169 int unit;
    170 bool use_rump;
    171 bool use_pad;
    172 bool exact_match;
    173 int padfd;
    174 int maxfd;
    175 pthread_t th;
    176 char devicename[16];	/* "audioN" */
    177 char devaudio[16];	/* "/dev/audioN" */
    178 char devsound[16];	/* "/dev/soundN" */
    179 char devaudioctl[16];	/* "/dev/audioctlN" */
    180 char devmixer[16];	/* "/dev/mixerN" */
    181 extern struct testentry testtable[];
    182 
    183 void
    184 usage(void)
    185 {
    186 	fprintf(stderr, "usage:\t%s [<options>] [<testname>...]\n",
    187 	    getprogname());
    188 	fprintf(stderr, "\t-A        : make output suitable for ATF\n");
    189 	fprintf(stderr, "\t-a        : Test all\n");
    190 	fprintf(stderr, "\t-d        : Increase debug level\n");
    191 	fprintf(stderr, "\t-e        : Use exact match for testnames "
    192 	    "(default is forward match)\n");
    193 	fprintf(stderr, "\t-l        : List all tests\n");
    194 	fprintf(stderr, "\t-p        : Open pad\n");
    195 #if !defined(NO_RUMP)
    196 	fprintf(stderr, "\t-R        : Use rump (implies -p)\n");
    197 #endif
    198 	fprintf(stderr, "\t-u <unit> : Use audio<unit> (default:0)\n");
    199 	exit(1);
    200 }
    201 
    202 /* Customized err(3) */
    203 void
    204 xp_err(int code, int line, const char *fmt, ...)
    205 {
    206 	va_list ap;
    207 	int backup_errno;
    208 
    209 	backup_errno = errno;
    210 	printf("%s %d: ", (opt_atf ? "Line" : " ERROR:"), line);
    211 	va_start(ap, fmt);
    212 	vprintf(fmt, ap);
    213 	va_end(ap);
    214 	printf(": %s\n", strerror(backup_errno));
    215 
    216 	exit(code);
    217 }
    218 
    219 /* Customized errx(3) */
    220 void
    221 xp_errx(int code, int line, const char *fmt, ...)
    222 {
    223 	va_list ap;
    224 
    225 	printf("%s %d: ", (opt_atf ? "Line" : " ERROR:"), line);
    226 	va_start(ap, fmt);
    227 	vprintf(fmt, ap);
    228 	va_end(ap);
    229 	printf("\n");
    230 
    231 	exit(code);
    232 }
    233 
    234 int
    235 main(int argc, char *argv[])
    236 {
    237 	int i;
    238 	int j;
    239 	int c;
    240 	enum {
    241 		CMD_TEST,
    242 		CMD_ALL,
    243 		CMD_LIST,
    244 	} cmd;
    245 	bool found;
    246 
    247 	props = -1;
    248 	hwfull = 0;
    249 	unit = 0;
    250 	cmd = CMD_TEST;
    251 	use_pad = false;
    252 	padfd = -1;
    253 	exact_match = false;
    254 
    255 	while ((c = getopt(argc, argv, "AadelpRu:")) != -1) {
    256 		switch (c) {
    257 		case 'A':
    258 			opt_atf = true;
    259 			break;
    260 		case 'a':
    261 			cmd = CMD_ALL;
    262 			break;
    263 		case 'd':
    264 			debug++;
    265 			break;
    266 		case 'e':
    267 			exact_match = true;
    268 			break;
    269 		case 'l':
    270 			cmd = CMD_LIST;
    271 			break;
    272 		case 'p':
    273 			use_pad = true;
    274 			break;
    275 		case 'R':
    276 #if !defined(NO_RUMP)
    277 			use_rump = true;
    278 			use_pad = true;
    279 #else
    280 			usage();
    281 #endif
    282 			break;
    283 		case 'u':
    284 			unit = atoi(optarg);
    285 			break;
    286 		default:
    287 			usage();
    288 		}
    289 	}
    290 	argc -= optind;
    291 	argv += optind;
    292 
    293 	if (cmd == CMD_LIST) {
    294 		/* List all */
    295 		for (i = 0; testtable[i].name != NULL; i++)
    296 			printf("%s\n", testtable[i].name);
    297 		return 0;
    298 	}
    299 
    300 	init(unit);
    301 
    302 	if (cmd == CMD_ALL) {
    303 		/* Test all */
    304 		if (argc > 0)
    305 			usage();
    306 		for (i = 0; testtable[i].name != NULL; i++)
    307 			do_test(i);
    308 	} else {
    309 		/* Test only matched */
    310 		if (argc == 0)
    311 			usage();
    312 
    313 		found = false;
    314 		for (j = 0; j < argc; j++) {
    315 			for (i = 0; testtable[i].name != NULL; i++) {
    316 				if (match(argv[j], testtable[i].name)) {
    317 					do_test(i);
    318 					found = true;
    319 				}
    320 			}
    321 		}
    322 		if (!found) {
    323 			printf("test not found\n");
    324 			exit(1);
    325 		}
    326 	}
    327 
    328 	if (opt_atf == false) {
    329 		printf("Result: %d tests, %d success",
    330 		    testcount,
    331 		    testcount - failcount - skipcount);
    332 		if (failcount > 0)
    333 			printf(", %d failed", failcount);
    334 		if (skipcount > 0)
    335 			printf(", %d skipped", skipcount);
    336 		printf("\n");
    337 	}
    338 
    339 	if (skipcount > 0)
    340 		return 2;
    341 	if (failcount > 0)
    342 		return 1;
    343 
    344 	return 0;
    345 }
    346 
    347 bool
    348 match(const char *arg, const char *name)
    349 {
    350 	if (exact_match) {
    351 		/* Exact match */
    352 		if (strcmp(arg, name) == 0)
    353 			return true;
    354 	} else {
    355 		/* Forward match */
    356 		if (strncmp(arg, name, strlen(arg)) == 0)
    357 			return true;
    358 	}
    359 	return false;
    360 }
    361 
    362 /*
    363  * XXX
    364  * Some hardware drivers (e.g. hdafg(4)) require a little "rest" between
    365  * close(2) and re-open(2).
    366  * audio(4) uses hw_if->close() to tell the hardware to close.  However,
    367  * there is no agreement to wait for completion between MI and MD layer.
    368  * audio(4) immediately shifts the "closed" state, and that is, the next
    369  * open() will be acceptable immediately in audio layer.  But the real
    370  * hardware may not have been closed actually at that point.
    371  * It's troublesome issue but should be fixed...
    372  *
    373  * However, the most frequently used pad(4) (for ATF tests) doesn't have
    374  * such problem, so avoids it to reduce time.
    375  */
    376 void
    377 xxx_close_wait(void)
    378 {
    379 
    380 	if (!use_pad)
    381 		usleep(500 * 1000);
    382 }
    383 
    384 void
    385 do_test(int testnumber)
    386 {
    387 	/* Sentinel */
    388 	strlcpy(testname, "<NoName>", sizeof(testname));
    389 	/* Do test */
    390 	testtable[testnumber].func();
    391 
    392 	cleanup_audiofd();
    393 	xxx_close_wait();
    394 }
    395 
    396 /*
    397  * system call wrappers for rump.
    398  */
    399 
    400 /* open(2) or rump_sys_open(3) */
    401 int
    402 rump_or_open(const char *filename, int flag)
    403 {
    404 	int r;
    405 
    406 #if !defined(NO_RUMP)
    407 	if (use_rump)
    408 		r = rump_sys_open(filename, flag);
    409 	else
    410 #endif
    411 		r = open(filename, flag);
    412 
    413 	if (r > maxfd)
    414 		maxfd = r;
    415 	return r;
    416 }
    417 
    418 /* write(2) or rump_sys_write(3) */
    419 int
    420 rump_or_write(int fd, const void *buf, size_t len)
    421 {
    422 	int r;
    423 
    424 #if !defined(NO_RUMP)
    425 	if (use_rump)
    426 		r = rump_sys_write(fd, buf, len);
    427 	else
    428 #endif
    429 		r = write(fd, buf, len);
    430 	return r;
    431 }
    432 
    433 /* read(2) or rump_sys_read(3) */
    434 int
    435 rump_or_read(int fd, void *buf, size_t len)
    436 {
    437 	int r;
    438 
    439 #if !defined(NO_RUMP)
    440 	if (use_rump)
    441 		r = rump_sys_read(fd, buf, len);
    442 	else
    443 #endif
    444 		r = read(fd, buf, len);
    445 	return r;
    446 }
    447 
    448 /* ioctl(2) or rump_sys_ioctl(3) */
    449 int
    450 rump_or_ioctl(int fd, u_long cmd, void *arg)
    451 {
    452 	int r;
    453 
    454 #if !defined(NO_RUMP)
    455 	if (use_rump)
    456 		r = rump_sys_ioctl(fd, cmd, arg);
    457 	else
    458 #endif
    459 		r = ioctl(fd, cmd, arg);
    460 	return r;
    461 }
    462 
    463 /* close(2) or rump_sys_close(3) */
    464 int
    465 rump_or_close(int fd)
    466 {
    467 	int r;
    468 
    469 #if !defined(NO_RUMP)
    470 	if (use_rump)
    471 		r = rump_sys_close(fd);
    472 	else
    473 #endif
    474 		r = close(fd);
    475 
    476 	/* maxfd-1 may not valid fd but no matter */
    477 	if (fd == maxfd)
    478 		maxfd--;
    479 	return r;
    480 }
    481 
    482 /* fcntl(2) or rump_sys_fcntl(3) */
    483 /* XXX Supported only with no arguments for now */
    484 int
    485 rump_or_fcntl(int fd, int cmd, ...)
    486 {
    487 	int r;
    488 
    489 #if !defined(NO_RUMP)
    490 	if (use_rump)
    491 		r = rump_sys_fcntl(fd, cmd);
    492 	else
    493 #endif
    494 		r = fcntl(fd, cmd);
    495 	return r;
    496 }
    497 
    498 /* poll(2) or rump_sys_poll(3) */
    499 int
    500 rump_or_poll(struct pollfd *fds, nfds_t nfds, int timeout)
    501 {
    502 	int r;
    503 
    504 #if !defined(NO_RUMP)
    505 	if (use_rump)
    506 		r = rump_sys_poll(fds, nfds, timeout);
    507 	else
    508 #endif
    509 		r = poll(fds, nfds, timeout);
    510 	return r;
    511 }
    512 
    513 /* kqueue(2) or rump_sys_kqueue(3) */
    514 int
    515 rump_or_kqueue(void)
    516 {
    517 	int r;
    518 
    519 #if !defined(NO_RUMP)
    520 	if (use_rump)
    521 		r = rump_sys_kqueue();
    522 	else
    523 #endif
    524 		r = kqueue();
    525 	return r;
    526 }
    527 
    528 /* kevent(2) or rump_sys_kevent(3) */
    529 int
    530 rump_or_kevent(int kq, const struct kevent *chlist, size_t nch,
    531 	struct kevent *evlist, size_t nev,
    532 	const struct timespec *timeout)
    533 {
    534 	int r;
    535 
    536 #if !defined(NO_RUMP)
    537 	if (use_rump)
    538 		r = rump_sys_kevent(kq, chlist, nch, evlist, nev, timeout);
    539 	else
    540 #endif
    541 		r = kevent(kq, chlist, nch, evlist, nev, timeout);
    542 	return r;
    543 }
    544 
    545 int
    546 hw_canplay(void)
    547 {
    548 	return (props & AUDIO_PROP_PLAYBACK) ? 1 : 0;
    549 }
    550 
    551 int
    552 hw_canrec(void)
    553 {
    554 	return (props & AUDIO_PROP_CAPTURE) ? 1 : 0;
    555 }
    556 
    557 int
    558 hw_bidir(void)
    559 {
    560 	return hw_canplay() & hw_canrec();
    561 }
    562 
    563 int
    564 hw_fulldup(void)
    565 {
    566 	return (props & AUDIO_PROP_FULLDUPLEX) ? 1 : 0;
    567 }
    568 
    569 #define DPRINTF(fmt...) do {	\
    570 	if (debug)		\
    571 		printf(fmt);	\
    572 } while (0)
    573 
    574 #define DPRINTFF(line, fmt...) do {		\
    575 	if (debug) {				\
    576 		printf("  > %d: ", line);	\
    577 		DPRINTF(fmt);			\
    578 		fflush(stdout);			\
    579 	}					\
    580 } while (0)
    581 
    582 #define DRESULT(r) do {				\
    583 	int backup_errno = errno;		\
    584 	if (r == -1) {				\
    585 		DPRINTF(" = %d, err#%d %s\n",	\
    586 		    r, backup_errno,		\
    587 		    strerror(backup_errno));	\
    588 	} else {				\
    589 		DPRINTF(" = %d\n", r);		\
    590 	}					\
    591 	errno = backup_errno;			\
    592 	return r;				\
    593 } while (0)
    594 
    595 /* pointer variants for mmap */
    596 #define DRESULT_PTR(r) do {			\
    597 	int backup_errno = errno;		\
    598 	if (r == (void *)-1) {			\
    599 		DPRINTF(" = -1, err#%d %s\n",	\
    600 		    backup_errno,		\
    601 		    strerror(backup_errno));	\
    602 	} else {				\
    603 		DPRINTF(" = %p\n", r);		\
    604 	}					\
    605 	errno = backup_errno;			\
    606 	return r;				\
    607 } while (0)
    608 
    609 
    610 /*
    611  * requnit <  0: Use auto by pad (not implemented).
    612  * requnit >= 0: Use audio<requnit>.
    613  */
    614 void
    615 init(int requnit)
    616 {
    617 	struct audio_device devinfo;
    618 	size_t len;
    619 	int rel;
    620 	int fd;
    621 	int r;
    622 
    623 	/* XXX */
    624 	atexit(cleanup_audiofd);
    625 
    626 	if (requnit < 0) {
    627 		xp_errx(1, __LINE__, "requnit < 0 not implemented.");
    628 	} else {
    629 		unit = requnit;
    630 	}
    631 
    632 	/* Set device name */
    633 	snprintf(devicename, sizeof(devicename), "audio%d", unit);
    634 	snprintf(devaudio, sizeof(devaudio), "/dev/audio%d", unit);
    635 	snprintf(devsound, sizeof(devsound), "/dev/sound%d", unit);
    636 	snprintf(devaudioctl, sizeof(devaudioctl), "/dev/audioctl%d", unit);
    637 	snprintf(devmixer, sizeof(devmixer), "/dev/mixer%d", unit);
    638 
    639 	/*
    640 	 * version
    641 	 * audio2 is merged in 8.99.39.
    642 	 */
    643 	len = sizeof(rel);
    644 	r = sysctlbyname("kern.osrevision", &rel, &len, NULL, 0);
    645 	if (r == -1)
    646 		xp_err(1, __LINE__, "sysctl kern.osrevision");
    647 	netbsd = rel / 100000000;
    648 	if (rel >=  899003900)
    649 		netbsd = 9;
    650 
    651 #if !defined(NO_RUMP)
    652 	if (use_rump) {
    653 		DPRINTF("  use rump\n");
    654 		rump_init();
    655 	}
    656 #endif
    657 
    658 	/*
    659 	 * Open pad device before all accesses (including /dev/audioctl).
    660 	 */
    661 	if (use_pad) {
    662 		padfd = rump_or_open("/dev/pad0", O_RDONLY);
    663 		if (padfd == -1)
    664 			xp_err(1, __LINE__, "rump_or_open");
    665 
    666 		/* Create consumer thread */
    667 		pthread_create(&th, NULL, consumer_thread, NULL);
    668 		/* Set this thread's name */
    669 		pthread_setname_np(pthread_self(), "main", NULL);
    670 	}
    671 
    672 	/*
    673 	 * Get device properties, etc.
    674 	 */
    675 	fd = rump_or_open(devaudioctl, O_RDONLY);
    676 	if (fd == -1)
    677 		xp_err(1, __LINE__, "open %s", devaudioctl);
    678 	r = rump_or_ioctl(fd, AUDIO_GETPROPS, &props);
    679 	if (r == -1)
    680 		xp_err(1, __LINE__, "AUDIO_GETPROPS");
    681 	r = rump_or_ioctl(fd, AUDIO_GETDEV, &devinfo);
    682 	if (r == -1)
    683 		xp_err(1, __LINE__, "AUDIO_GETDEV");
    684 	rump_or_close(fd);
    685 
    686 	if (debug) {
    687 		printf("  device = %s, %s, %s\n",
    688 		    devinfo.name, devinfo.version, devinfo.config);
    689 		printf("  hw props =");
    690 		if (hw_canplay())
    691 			printf(" playback");
    692 		if (hw_canrec())
    693 			printf(" capture");
    694 		if (hw_fulldup())
    695 			printf(" fullduplex");
    696 		printf("\n");
    697 	}
    698 
    699 }
    700 
    701 /* Consumer thread used by pad */
    702 void *
    703 consumer_thread(void *arg)
    704 {
    705 	char buf[1024];
    706 	int r;
    707 
    708 	pthread_setname_np(pthread_self(), "consumer", NULL);
    709 	pthread_detach(pthread_self());
    710 
    711 	/* throw away data anyway */
    712 	for (;;) {
    713 		r = read(padfd, buf, sizeof(buf));
    714 		if (r < 1)
    715 			break;
    716 	}
    717 
    718 	pthread_exit(NULL);
    719 }
    720 
    721 /*
    722  * XXX
    723  * Closing pad descriptor before audio descriptor causes panic (kern/54427).
    724  * To avoid this, close non-pad descriptor first using atexit(3) for now.
    725  * This is just a workaround and this function should be removed.
    726  */
    727 void cleanup_audiofd()
    728 {
    729 	int fd;
    730 
    731 	for (fd = 3; fd <= maxfd; fd++) {
    732 		if (fd != padfd)
    733 			close(fd);
    734 	}
    735 	maxfd = 3;
    736 }
    737 
    738 /*
    739  * Support functions
    740  */
    741 
    742 /* Set testname */
    743 void
    744 TEST(const char *name, ...)
    745 {
    746 	va_list ap;
    747 
    748 	va_start(ap, name);
    749 	vsnprintf(testname, sizeof(testname), name, ap);
    750 	va_end(ap);
    751 	if (opt_atf == false) {
    752 		printf("%s\n", testname);
    753 		fflush(stdout);
    754 	}
    755 }
    756 
    757 /*
    758  * XP_FAIL() should be called when this test fails.
    759  * If caller already count up testcount, call xp_fail() instead.
    760  */
    761 #define XP_FAIL(fmt...)	do {	\
    762 	testcount++;	\
    763 	xp_fail(__LINE__, fmt);	\
    764 } while (0)
    765 bool xp_fail(int line, const char *fmt, ...)
    766 {
    767 	va_list ap;
    768 
    769 	printf("%s %d: ", (opt_atf ? "Line" : " FAIL:"), line);
    770 	va_start(ap, fmt);
    771 	vprintf(fmt, ap);
    772 	va_end(ap);
    773 	printf("\n");
    774 	fflush(stdout);
    775 	failcount++;
    776 
    777 	return false;
    778 }
    779 
    780 /*
    781  * XP_SKIP() should be called when you want to skip this test.
    782  * If caller already count up testcount, call xp_skip() instead.
    783  */
    784 #define XP_SKIP(fmt...)	do { \
    785 	testcount++;	\
    786 	xp_skip(__LINE__, fmt);	\
    787 } while (0)
    788 void xp_skip(int line, const char *fmt, ...)
    789 {
    790 	va_list ap;
    791 
    792 	printf("%s %d: ", (opt_atf ? "Line" : " SKIP:"), line);
    793 	va_start(ap, fmt);
    794 	vprintf(fmt, ap);
    795 	va_end(ap);
    796 	printf("\n");
    797 	fflush(stdout);
    798 	skipcount++;
    799 }
    800 
    801 #define XP_EQ(exp, act)	xp_eq(__LINE__, exp, act, #act)
    802 bool xp_eq(int line, int exp, int act, const char *varname)
    803 {
    804 	bool r = true;
    805 
    806 	testcount++;
    807 	if (exp != act) {
    808 		r = xp_fail(line, "%s expects %d but %d", varname, exp, act);
    809 	}
    810 	return r;
    811 }
    812 #define XP_EQ_STR(exp, act) xp_eq_str(__LINE__, exp, act, #act)
    813 bool xp_eq_str(int line, const char *exp, const char *act, const char *varname)
    814 {
    815 	bool r = true;
    816 
    817 	testcount++;
    818 	if (strcmp(exp, act) != 0) {
    819 		r = xp_fail(line, "%s expects \"%s\" but \"%s\"",
    820 		    varname, exp, act);
    821 	}
    822 	return r;
    823 }
    824 
    825 #define XP_NE(exp, act)	xp_ne(__LINE__, exp, act, #act)
    826 bool xp_ne(int line, int exp, int act, const char *varname)
    827 {
    828 	bool r = true;
    829 
    830 	testcount++;
    831 	if (exp == act) {
    832 		r = xp_fail(line, "%s expects != %d but %d", varname, exp, act);
    833 	}
    834 	return r;
    835 }
    836 
    837 /* This expects that result is expressed in expr. */
    838 /* GCC extension */
    839 #define XP_IF(expr) xp_if(__LINE__, (expr), #expr)
    840 bool xp_if(int line, bool expr, const char *exprname)
    841 {
    842 	bool r = true;
    843 	testcount++;
    844 	if (!expr) {
    845 		r = xp_fail(__LINE__, "(%s) is expected but not met", exprname);
    846 	}
    847 	return r;
    848 }
    849 
    850 /* This expects that the system call returns 'exp'. */
    851 #define XP_SYS_EQ(exp, act)	xp_sys_eq(__LINE__, exp, act, #act)
    852 bool xp_sys_eq(int line, int exp, int act, const char *varname)
    853 {
    854 	bool r = true;
    855 
    856 	testcount++;
    857 	if (act == -1) {
    858 		r = xp_fail(line, "%s expects %d but -1,err#%d(%s)",
    859 		    varname, exp, errno, strerror(errno));
    860 	} else {
    861 		r = xp_eq(line, exp, act, varname);
    862 	}
    863 	return r;
    864 }
    865 
    866 /*
    867  * This expects that system call succeeds.
    868  * This is useful when you expect the system call succeeds but don't know
    869  * the expected return value, such as open(2).
    870  */
    871 #define XP_SYS_OK(act)	xp_sys_ok(__LINE__, act, #act)
    872 bool xp_sys_ok(int line, int act, const char *varname)
    873 {
    874 	bool r = true;
    875 
    876 	testcount++;
    877 	if (act == -1) {
    878 		r = xp_fail(line, "%s expects success but -1,err#%d(%s)",
    879 		    varname, errno, strerror(errno));
    880 	}
    881 	return r;
    882 }
    883 
    884 /* This expects that the system call fails with 'experrno'. */
    885 #define XP_SYS_NG(experrno, act) xp_sys_ng(__LINE__, experrno, act, #act)
    886 bool xp_sys_ng(int line, int experrno, int act, const char *varname)
    887 {
    888 	bool r = true;
    889 
    890 	testcount++;
    891 	if (act != -1) {
    892 		r = xp_fail(line, "%s expects -1,err#%d but %d",
    893 		    varname, experrno, act);
    894 	} else if (experrno != errno) {
    895 		char acterrbuf[100];
    896 		int acterrno = errno;
    897 		strlcpy(acterrbuf, strerror(acterrno), sizeof(acterrbuf));
    898 		r = xp_fail(line, "%s expects -1,err#%d(%s) but -1,err#%d(%s)",
    899 		    varname, experrno, strerror(experrno),
    900 		    acterrno, acterrbuf);
    901 	}
    902 	return r;
    903 }
    904 
    905 /*
    906  * When exp == 0, this expects that the system call succeeds with returned
    907  * pointer is not -1.
    908  * When exp != 0, this expects that the system call fails with returned
    909  * pointer is -1 and its errno is exp.
    910  * It's only for mmap().
    911  */
    912 #define XP_SYS_PTR(exp, act) xp_sys_ptr(__LINE__, exp, act, #act)
    913 bool xp_sys_ptr(int line, int exp, void *act, const char *varname)
    914 {
    915 	char errbuf[256];
    916 	int actual_errno;
    917 	bool r = true;
    918 
    919 	testcount++;
    920 	if (exp == 0) {
    921 		/* expects to succeed */
    922 		if (act == (void *)-1) {
    923 			r = xp_fail(line,
    924 			    "%s expects success but -1,err#%d(%s)",
    925 			    varname, errno, strerror(errno));
    926 		}
    927 	} else {
    928 		/* expects to fail */
    929 		if (act != (void *)-1) {
    930 			r = xp_fail(line,
    931 			    "%s expects -1,err#%d(%s) but success",
    932 			    varname, exp, strerror(exp));
    933 		} else if (exp != errno) {
    934 			actual_errno = errno;
    935 			strerror_r(actual_errno, errbuf, sizeof(errbuf));
    936 			r = xp_fail(line,
    937 			    "%s expects -1,err#%d(%s) but -1,err#%d(%s)",
    938 			    varname, exp, strerror(exp), actual_errno, errbuf);
    939 		}
    940 	}
    941 	return r;
    942 }
    943 
    944 
    945 /*
    946  * REQUIRED_* return immediately if condition does not meet.
    947  */
    948 #define REQUIRED_EQ(e, a) do { if (!XP_EQ(e, a)) return; } while (0)
    949 #define REQUIRED_NE(e, a) do { if (!XP_NE(e, a)) return; } while (0)
    950 #define REQUIRED_IF(expr) do { if (!XP_IF(expr)) return; } while (0)
    951 #define REQUIRED_SYS_EQ(e, a) do { if (!XP_SYS_EQ(e, a)) return; } while (0)
    952 #define REQUIRED_SYS_OK(a)    do { if (!XP_SYS_OK(a))    return; } while (0)
    953 
    954 
    955 static const char *openmode_str[] = {
    956 	"O_RDONLY",
    957 	"O_WRONLY",
    958 	"O_RDWR",
    959 };
    960 
    961 
    962 /*
    963  * All system calls in following tests should be called with these macros.
    964  */
    965 
    966 #define OPEN(name, mode)	\
    967 	debug_open(__LINE__, name, mode)
    968 int debug_open(int line, const char *name, int mode)
    969 {
    970 	char modestr[32];
    971 	int n;
    972 
    973 	if ((mode & 3) != 3) {
    974 		n = snprintf(modestr, sizeof(modestr), "%s",
    975 		    openmode_str[mode & 3]);
    976 	} else {
    977 		n = snprintf(modestr, sizeof(modestr), "%d", mode & 3);
    978 	}
    979 	if ((mode & O_NONBLOCK))
    980 		n += snprintf(modestr + n, sizeof(modestr) - n, "|O_NONBLOCK");
    981 
    982 	DPRINTFF(line, "open(\"%s\", %s)", name, modestr);
    983 	int r = rump_or_open(name, mode);
    984 	DRESULT(r);
    985 }
    986 
    987 #define WRITE(fd, addr, len)	\
    988 	debug_write(__LINE__, fd, addr, len)
    989 int debug_write(int line, int fd, const void *addr, size_t len)
    990 {
    991 	DPRINTFF(line, "write(%d, %p, %zd)", fd, addr, len);
    992 	int r = rump_or_write(fd, addr, len);
    993 	DRESULT(r);
    994 }
    995 
    996 #define READ(fd, addr, len)	\
    997 	debug_read(__LINE__, fd, addr, len)
    998 int debug_read(int line, int fd, void *addr, size_t len)
    999 {
   1000 	DPRINTFF(line, "read(%d, %p, %zd)", fd, addr, len);
   1001 	int r = rump_or_read(fd, addr, len);
   1002 	DRESULT(r);
   1003 }
   1004 
   1005 /*
   1006  * addrstr is the comment for debug message.
   1007  *   int onoff = 0;
   1008  *   ioctl(fd, SWITCH, onoff); -> IOCTL(fd, SWITCH, onoff, "off");
   1009  */
   1010 #define IOCTL(fd, name, addr, addrfmt...)	\
   1011 	debug_ioctl(__LINE__, fd, name, #name, addr, addrfmt)
   1012 int debug_ioctl(int line, int fd, u_long name, const char *namestr,
   1013 	void *addr, const char *addrfmt, ...)
   1014 {
   1015 	char addrbuf[100];
   1016 	va_list ap;
   1017 
   1018 	va_start(ap, addrfmt);
   1019 	vsnprintf(addrbuf, sizeof(addrbuf), addrfmt, ap);
   1020 	va_end(ap);
   1021 	DPRINTFF(line, "ioctl(%d, %s, %s)", fd, namestr, addrbuf);
   1022 	int r = rump_or_ioctl(fd, name, addr);
   1023 	DRESULT(r);
   1024 }
   1025 
   1026 #define FCNTL(fd, name...)	\
   1027 	debug_fcntl(__LINE__, fd, name, #name)
   1028 int debug_fcntl(int line, int fd, int name, const char *namestr, ...)
   1029 {
   1030 	int r;
   1031 
   1032 	switch (name) {
   1033 	 case F_GETFL:	/* no arguments */
   1034 		DPRINTFF(line, "fcntl(%d, %s)", fd, namestr);
   1035 		r = rump_or_fcntl(fd, name);
   1036 		break;
   1037 	 default:
   1038 		__unreachable();
   1039 	}
   1040 	DRESULT(r);
   1041 	return r;
   1042 }
   1043 
   1044 #define CLOSE(fd)	\
   1045 	debug_close(__LINE__, fd)
   1046 int debug_close(int line, int fd)
   1047 {
   1048 	DPRINTFF(line, "close(%d)", fd);
   1049 	int r = rump_or_close(fd);
   1050 	DRESULT(r);
   1051 }
   1052 
   1053 #define MMAP(ptr, len, prot, flags, fd, offset)	\
   1054 	debug_mmap(__LINE__, ptr, len, prot, flags, fd, offset)
   1055 void *debug_mmap(int line, void *ptr, size_t len, int prot, int flags, int fd,
   1056 	off_t offset)
   1057 {
   1058 	char protbuf[256];
   1059 	char flagbuf[256];
   1060 	int n;
   1061 
   1062 #define ADDFLAG(buf, var, name)	do {				\
   1063 	if (((var) & (name)))					\
   1064 		n = strlcat(buf, "|" #name, sizeof(buf));	\
   1065 		var &= ~(name);					\
   1066 } while (0)
   1067 
   1068 	n = 0;
   1069 	protbuf[n] = '\0';
   1070 	if (prot == 0) {
   1071 		strlcpy(protbuf, "|PROT_NONE", sizeof(protbuf));
   1072 	} else {
   1073 		ADDFLAG(protbuf, prot, PROT_EXEC);
   1074 		ADDFLAG(protbuf, prot, PROT_WRITE);
   1075 		ADDFLAG(protbuf, prot, PROT_READ);
   1076 		if (prot != 0) {
   1077 			snprintf(protbuf + n, sizeof(protbuf) - n,
   1078 			    "|prot=0x%x", prot);
   1079 		}
   1080 	}
   1081 
   1082 	n = 0;
   1083 	flagbuf[n] = '\0';
   1084 	if (flags == 0) {
   1085 		strlcpy(flagbuf, "|MAP_FILE", sizeof(flagbuf));
   1086 	} else {
   1087 		ADDFLAG(flagbuf, flags, MAP_SHARED);
   1088 		ADDFLAG(flagbuf, flags, MAP_PRIVATE);
   1089 		ADDFLAG(flagbuf, flags, MAP_FIXED);
   1090 		ADDFLAG(flagbuf, flags, MAP_INHERIT);
   1091 		ADDFLAG(flagbuf, flags, MAP_HASSEMAPHORE);
   1092 		ADDFLAG(flagbuf, flags, MAP_TRYFIXED);
   1093 		ADDFLAG(flagbuf, flags, MAP_WIRED);
   1094 		ADDFLAG(flagbuf, flags, MAP_ANON);
   1095 		if (flags != 0) {
   1096 			n += snprintf(flagbuf + n, sizeof(flagbuf) - n,
   1097 			    "|flag=0x%x", flags);
   1098 		}
   1099 	}
   1100 
   1101 	DPRINTFF(line, "mmap(%p, %zd, %s, %s, %d, %jd)",
   1102 	    ptr, len, protbuf + 1, flagbuf + 1, fd, offset);
   1103 	void *r = mmap(ptr, len, prot, flags, fd, offset);
   1104 	DRESULT_PTR(r);
   1105 }
   1106 
   1107 #define MUNMAP(ptr, len)	\
   1108 	debug_munmap(__LINE__, ptr, len)
   1109 int debug_munmap(int line, void *ptr, int len)
   1110 {
   1111 #if !defined(NO_RUMP)
   1112 	if (use_rump)
   1113 		xp_errx(1, __LINE__, "rump doesn't support munmap");
   1114 #endif
   1115 	DPRINTFF(line, "munmap(%p, %d)", ptr, len);
   1116 	int r = munmap(ptr, len);
   1117 	DRESULT(r);
   1118 }
   1119 
   1120 const char *
   1121 event_tostr(int events)
   1122 {
   1123 	static char buf[64];
   1124 
   1125 	snprintb(buf, sizeof(buf),
   1126 	    "\177\020" \
   1127 	    "b\10WRBAND\0" \
   1128 	    "b\7RDBAND\0" "b\6RDNORM\0" "b\5NVAL\0" "b\4HUP\0" \
   1129 	    "b\3ERR\0" "b\2OUT\0" "b\1PRI\0" "b\0IN\0",
   1130 	    events);
   1131 	return buf;
   1132 }
   1133 
   1134 #define POLL(pfd, nfd, timeout)	\
   1135 	debug_poll(__LINE__, pfd, nfd, timeout)
   1136 int debug_poll(int line, struct pollfd *pfd, int nfd, int timeout)
   1137 {
   1138 	char buf[256];
   1139 	int n = 0;
   1140 	buf[n] = '\0';
   1141 	for (int i = 0; i < nfd; i++) {
   1142 		n += snprintf(buf + n, sizeof(buf) - n, "{fd=%d,events=%s}",
   1143 		    pfd[i].fd, event_tostr(pfd[i].events));
   1144 	}
   1145 	DPRINTFF(line, "poll(%s, %d, %d)", buf, nfd, timeout);
   1146 	int r = rump_or_poll(pfd, nfd, timeout);
   1147 	DRESULT(r);
   1148 }
   1149 
   1150 #define KQUEUE()	\
   1151 	debug_kqueue(__LINE__)
   1152 int debug_kqueue(int line)
   1153 {
   1154 	DPRINTFF(line, "kqueue()");
   1155 	int r = rump_or_kqueue();
   1156 	DRESULT(r);
   1157 }
   1158 
   1159 #define KEVENT_SET(kq, kev, nev)	\
   1160 	debug_kevent_set(__LINE__, kq, kev, nev)
   1161 int debug_kevent_set(int line, int kq, const struct kevent *kev, size_t nev)
   1162 {
   1163 	DPRINTFF(line, "kevent_set(%d, %p, %zd)", kq, kev, nev);
   1164 	int r = rump_or_kevent(kq, kev, nev, NULL, 0, NULL);
   1165 	DRESULT(r);
   1166 }
   1167 
   1168 #define KEVENT_POLL(kq, kev, nev, ts) \
   1169 	debug_kevent_poll(__LINE__, kq, kev, nev, ts)
   1170 int debug_kevent_poll(int line, int kq, struct kevent *kev, size_t nev,
   1171 	const struct timespec *ts)
   1172 {
   1173 	char tsbuf[32];
   1174 
   1175 	if (ts == NULL) {
   1176 		snprintf(tsbuf, sizeof(tsbuf), "NULL");
   1177 	} else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
   1178 		snprintf(tsbuf, sizeof(tsbuf), "0.0");
   1179 	} else {
   1180 		snprintf(tsbuf, sizeof(tsbuf), "%d.%09ld",
   1181 			(int)ts->tv_sec, ts->tv_nsec);
   1182 	}
   1183 	DPRINTFF(line, "kevent_poll(%d, %p, %zd, %s)", kq, kev, nev, tsbuf);
   1184 	int r = rump_or_kevent(kq, NULL, 0, kev, nev, ts);
   1185 	DRESULT(r);
   1186 }
   1187 
   1188 #define DEBUG_KEV(name, kev)	\
   1189 	debug_kev(__LINE__, name, kev)
   1190 void debug_kev(int line, const char *name, const struct kevent *kev)
   1191 {
   1192 	char flagbuf[256];
   1193 	const char *filterbuf;
   1194 	uint32_t v;
   1195 	int n;
   1196 
   1197 	n = 0;
   1198 	flagbuf[n] = '\0';
   1199 	if (kev->flags == 0) {
   1200 		strcpy(flagbuf, "|0?");
   1201 	} else {
   1202 		v = kev->flags;
   1203 		ADDFLAG(flagbuf, v, EV_ADD);
   1204 		if (v != 0)
   1205 			snprintf(flagbuf + n, sizeof(flagbuf)-n, "|0x%x", v);
   1206 	}
   1207 
   1208 	switch (kev->filter) {
   1209 	 case EVFILT_READ:	filterbuf = "EVFILT_READ";	break;
   1210 	 case EVFILT_WRITE:	filterbuf = "EVFILT_WRITE";	break;
   1211 	 default:		filterbuf = "EVFILT_?";		break;
   1212 	}
   1213 
   1214 	DPRINTFF(line,
   1215 	    "%s={id:%d,%s,%s,fflags:0x%x,data:0x%" PRIx64 ",udata:0x%x}\n",
   1216 	    name,
   1217 	    (int)kev->ident,
   1218 	    flagbuf + 1,
   1219 	    filterbuf,
   1220 	    kev->fflags,
   1221 	    kev->data,
   1222 	    (int)(intptr_t)kev->udata);
   1223 }
   1224 
   1225 /* XXX rump? */
   1226 #define GETUID()	\
   1227 	debug_getuid(__LINE__)
   1228 uid_t debug_getuid(int line)
   1229 {
   1230 	DPRINTFF(line, "getuid");
   1231 	uid_t r = getuid();
   1232 	/* getuid() never fails */
   1233 	DPRINTF(" = %u\n", r);
   1234 	return r;
   1235 }
   1236 
   1237 /* XXX rump? */
   1238 #define SETEUID(id)	\
   1239 	debug_seteuid(__LINE__, id)
   1240 int debug_seteuid(int line, uid_t id)
   1241 {
   1242 	DPRINTFF(line, "seteuid(%d)", (int)id);
   1243 	int r = seteuid(id);
   1244 	DRESULT(r);
   1245 }
   1246 
   1247 #define SYSCTLBYNAME(name, oldp, oldlenp, newp, newlen)	\
   1248 	debug_sysctlbyname(__LINE__, name, oldp, oldlenp, newp, newlen)
   1249 int debug_sysctlbyname(int line, const char *name, void *oldp, size_t *oldlenp,
   1250 	const void *newp, size_t newlen)
   1251 {
   1252 	DPRINTFF(line, "sysctlbyname(\"%s\")", name);
   1253 	int r = sysctlbyname(name, oldp, oldlenp, newp, newlen);
   1254 	DRESULT(r);
   1255 }
   1256 
   1257 
   1258 /* Return openable mode on this hardware property */
   1259 int
   1260 openable_mode(void)
   1261 {
   1262 	if (hw_bidir())
   1263 		return O_RDWR;
   1264 	if (hw_canplay())
   1265 		return O_WRONLY;
   1266 	else
   1267 		return O_RDONLY;
   1268 }
   1269 
   1270 int mode2aumode_full[] = {
   1271 	                                AUMODE_RECORD,	/* O_RDONLY */
   1272 	AUMODE_PLAY | AUMODE_PLAY_ALL,			/* O_WRONLY */
   1273 	AUMODE_PLAY | AUMODE_PLAY_ALL | AUMODE_RECORD,	/* O_RDWR   */
   1274 };
   1275 
   1276 /* Convert openmode(O_*) to AUMODE_*, with hardware property */
   1277 int
   1278 mode2aumode(int mode)
   1279 {
   1280 	int aumode;
   1281 
   1282 	aumode = mode2aumode_full[mode];
   1283 	if (hw_canplay() == 0)
   1284 		aumode &= ~(AUMODE_PLAY | AUMODE_PLAY_ALL);
   1285 	if (hw_canrec() == 0)
   1286 		aumode &= ~AUMODE_RECORD;
   1287 
   1288 	if (netbsd >= 9) {
   1289 		/* half-duplex treats O_RDWR as O_WRONLY */
   1290 		if (mode == O_RDWR && hw_bidir() && hw_fulldup() == 0)
   1291 			aumode &= ~AUMODE_RECORD;
   1292 	}
   1293 
   1294 	return aumode;
   1295 }
   1296 
   1297 /* Is this mode + hardware playable? */
   1298 int
   1299 mode2play(int mode)
   1300 {
   1301 	int aumode;
   1302 
   1303 	aumode = mode2aumode(mode);
   1304 	return ((aumode & AUMODE_PLAY)) ? 1 : 0;
   1305 }
   1306 
   1307 /* Is this mode + hardware recordable? */
   1308 int
   1309 mode2rec(int mode)
   1310 {
   1311 	int aumode;
   1312 
   1313 	aumode = mode2aumode(mode);
   1314 	return ((aumode & AUMODE_RECORD)) ? 1 : 0;
   1315 }
   1316 
   1317 /*
   1318  * On NetBSD7, open() after-closing-mmap fails due to a bug.
   1319  * It happens once every two times like flip-flop, so the workaround is
   1320  * to open it again.
   1321  */
   1322 void
   1323 reset_after_mmap(void)
   1324 {
   1325 	int fd;
   1326 
   1327 	if (netbsd < 8) {
   1328 		fd = OPEN(devaudio, O_WRONLY);
   1329 		if (fd != -1)
   1330 			CLOSE(fd);
   1331 	}
   1332 }
   1333 
   1334 /*
   1335  * Lookup "outputs.master" and return its mixer device index.
   1336  * It may not be strict but I'm not sure.
   1337  */
   1338 int
   1339 mixer_get_outputs_master(int mixerfd)
   1340 {
   1341 	const char * const typename[] = { "CLASS", "ENUM", "SET", "VALUE" };
   1342 	mixer_devinfo_t di;
   1343 	int class_outputs;
   1344 	int i;
   1345 	int r;
   1346 
   1347 	class_outputs = -1;
   1348 	for (i = 0; ; i++) {
   1349 		memset(&di, 0, sizeof(di));
   1350 		di.index = i;
   1351 		r = IOCTL(mixerfd, AUDIO_MIXER_DEVINFO, &di, "index=%d", i);
   1352 		if (r < 0)
   1353 			break;
   1354 		DPRINTF("  > type=%s(%d) mixer_class=%d name=%s\n",
   1355 		    (0 <= di.type && di.type <= 3) ? typename[di.type] : "",
   1356 		    di.type, di.mixer_class, di.label.name);
   1357 		if (di.type == AUDIO_MIXER_CLASS &&
   1358 		    strcmp(di.label.name, "outputs") == 0) {
   1359 			class_outputs = di.mixer_class;
   1360 			DPRINTF("  > class_output=%d\n", class_outputs);
   1361 			continue;
   1362 		}
   1363 		if (di.type == AUDIO_MIXER_VALUE &&
   1364 		    di.mixer_class == class_outputs &&
   1365 		    strcmp(di.label.name, "master") == 0) {
   1366 			return i;
   1367 		}
   1368 	}
   1369 	/* Not found */
   1370 	return -1;
   1371 }
   1372 
   1373 /*
   1374  * Tests
   1375  */
   1376 
   1377 void test_open_mode(int);
   1378 void test_open(const char *, int);
   1379 void test_open_simul(int, int);
   1380 void try_open_multiuser(bool);
   1381 void test_open_multiuser(bool);
   1382 void test_rdwr_fallback(int, bool, bool);
   1383 void test_rdwr_two(int, int);
   1384 void test_mmap_mode(int, int);
   1385 void test_poll_mode(int, int, int);
   1386 void test_poll_in_open(const char *);
   1387 void test_kqueue_mode(int, int, int);
   1388 volatile int sigio_caught;
   1389 void signal_FIOASYNC(int);
   1390 void test_AUDIO_SETFD_xxONLY(int);
   1391 void test_AUDIO_SETINFO_mode(int, int, int, int);
   1392 void test_AUDIO_SETINFO_params_set(int, int, int);
   1393 void test_AUDIO_SETINFO_pause(int, int, int);
   1394 int getenc_make_table(int, int[][5]);
   1395 void xp_getenc(int[][5], int, int, int, struct audio_prinfo *);
   1396 void getenc_check_encodings(int, int[][5]);
   1397 void test_AUDIO_ERROR(int);
   1398 void test_audioctl_open_1(int, int);
   1399 void test_audioctl_open_2(int, int);
   1400 void try_audioctl_open_multiuser(const char *, const char *);
   1401 void test_audioctl_open_multiuser(bool, const char *, const char *);
   1402 void test_audioctl_rw(int);
   1403 
   1404 #define DEF(name) \
   1405 	void test__ ## name (void); \
   1406 	void test__ ## name (void)
   1407 
   1408 /*
   1409  * Whether it can be open()ed with specified mode.
   1410  */
   1411 void
   1412 test_open_mode(int mode)
   1413 {
   1414 	int fd;
   1415 	int r;
   1416 
   1417 	TEST("open_mode_%s", openmode_str[mode] + 2);
   1418 
   1419 	fd = OPEN(devaudio, mode);
   1420 	if (mode2aumode(mode) != 0) {
   1421 		XP_SYS_OK(fd);
   1422 	} else {
   1423 		XP_SYS_NG(ENXIO, fd);
   1424 	}
   1425 
   1426 	if (fd >= 0) {
   1427 		r = CLOSE(fd);
   1428 		XP_SYS_EQ(0, r);
   1429 	}
   1430 }
   1431 DEF(open_mode_RDONLY)	{ test_open_mode(O_RDONLY); }
   1432 DEF(open_mode_WRONLY)	{ test_open_mode(O_WRONLY); }
   1433 DEF(open_mode_RDWR)	{ test_open_mode(O_RDWR);   }
   1434 
   1435 /*
   1436  * Check the initial parameters and stickiness.
   1437  * /dev/audio
   1438  *	The initial parameters are always the same whenever you open.
   1439  * /dev/sound and /dev/audioctl
   1440  *	The initial parameters are inherited from the last /dev/sound or
   1441  *	/dev/audio.
   1442  */
   1443 void
   1444 test_open(const char *devname, int mode)
   1445 {
   1446 	struct audio_info ai;
   1447 	struct audio_info ai0;
   1448 	char devfile[16];
   1449 	int fd;
   1450 	int r;
   1451 	int can_play;
   1452 	int can_rec;
   1453 	int exp_mode;
   1454 	int exp_encoding;
   1455 	int exp_precision;
   1456 	int exp_channels;
   1457 	int exp_sample_rate;
   1458 	int exp_pause;
   1459 	int exp_popen;
   1460 	int exp_ropen;
   1461 
   1462 	TEST("open_%s_%s", devname, openmode_str[mode] + 2);
   1463 
   1464 	snprintf(devfile, sizeof(devfile), "/dev/%s%d", devname, unit);
   1465 	can_play = mode2play(mode);
   1466 	can_rec  = mode2rec(mode);
   1467 	if (strcmp(devname, "audioctl") != 0) {
   1468 		if (can_play + can_rec == 0) {
   1469 			/* Check whether it cannot be opened */
   1470 			fd = OPEN(devaudio, mode);
   1471 			XP_SYS_NG(ENXIO, fd);
   1472 			return;
   1473 		}
   1474 	}
   1475 
   1476 	/* /dev/audio is always initialized */
   1477 	if (strcmp(devname, "audio") == 0) {
   1478 		exp_encoding = AUDIO_ENCODING_ULAW;
   1479 		exp_precision = 8;
   1480 		exp_channels = 1;
   1481 		exp_sample_rate = 8000;
   1482 		exp_pause = 0;
   1483 	} else {
   1484 		exp_encoding = AUDIO_ENCODING_SLINEAR_LE;
   1485 		exp_precision = 16;
   1486 		exp_channels = 2;
   1487 		exp_sample_rate = 11025;
   1488 		exp_pause = 1;
   1489 	}
   1490 
   1491 	/* /dev/audioctl is always "not opened" */
   1492 	if (strcmp(devname, "audioctl") == 0) {
   1493 		exp_mode = 0;
   1494 		exp_popen = 0;
   1495 		exp_ropen = 0;
   1496 	} else {
   1497 		exp_mode = mode2aumode(mode);
   1498 		exp_popen = can_play;
   1499 		exp_ropen = can_rec;
   1500 	}
   1501 
   1502 
   1503 	/*
   1504 	 * At first, initialize the sticky parameters both of play and rec.
   1505 	 * This uses /dev/audio to verify /dev/audio.  It's not good way but
   1506 	 * I don't have better one...
   1507 	 */
   1508 	fd = OPEN(devaudio, openable_mode());
   1509 	REQUIRED_SYS_OK(fd);
   1510 	r = CLOSE(fd);
   1511 	REQUIRED_SYS_EQ(0, r);
   1512 
   1513 	/*
   1514 	 * Open target device and check the initial parameters
   1515 	 * At this moment, all devices are initialized by default.
   1516 	 */
   1517 	fd = OPEN(devfile, mode);
   1518 	REQUIRED_SYS_OK(fd);
   1519 	memset(&ai, 0, sizeof(ai));
   1520 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1521 	REQUIRED_SYS_EQ(0, r);
   1522 
   1523 	XP_NE(0, ai.blocksize);
   1524 		/* hiwat/lowat */
   1525 	XP_EQ(exp_mode, ai.mode);
   1526 	/* ai.play */
   1527 	XP_EQ(8000, ai.play.sample_rate);
   1528 	XP_EQ(1, ai.play.channels);
   1529 	XP_EQ(8, ai.play.precision);
   1530 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1531 		/* gain */
   1532 		/* port */
   1533 	XP_EQ(0, ai.play.seek);
   1534 		/* avail_ports */
   1535 	XP_NE(0, ai.play.buffer_size);
   1536 	XP_EQ(0, ai.play.samples);
   1537 	XP_EQ(0, ai.play.eof);
   1538 	XP_EQ(0, ai.play.pause);
   1539 	XP_EQ(0, ai.play.error);
   1540 	XP_EQ(0, ai.play.waiting);
   1541 		/* balance */
   1542 	XP_EQ(exp_popen, ai.play.open);
   1543 	XP_EQ(0, ai.play.active);
   1544 	/* ai.record */
   1545 	XP_EQ(8000, ai.record.sample_rate);
   1546 	XP_EQ(1, ai.record.channels);
   1547 	XP_EQ(8, ai.record.precision);
   1548 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1549 		/* gain */
   1550 		/* port */
   1551 	XP_EQ(0, ai.record.seek);
   1552 		/* avail_ports */
   1553 	XP_NE(0, ai.record.buffer_size);
   1554 	XP_EQ(0, ai.record.samples);
   1555 	XP_EQ(0, ai.record.eof);
   1556 	XP_EQ(0, ai.record.pause);
   1557 	XP_EQ(0, ai.record.error);
   1558 	XP_EQ(0, ai.record.waiting);
   1559 		/* balance */
   1560 	XP_EQ(exp_ropen, ai.record.open);
   1561 	if (netbsd < 9 && strcmp(devname, "sound") == 0) {
   1562 		/*
   1563 		 * On NetBSD7/8, it doesn't seem to start recording on open
   1564 		 * for /dev/sound.  It should be a bug.
   1565 		 */
   1566 		XP_EQ(0, ai.record.active);
   1567 	} else {
   1568 		XP_EQ(exp_ropen, ai.record.active);
   1569 	}
   1570 	/* Save it */
   1571 	ai0 = ai;
   1572 
   1573 	/*
   1574 	 * Change much as possible
   1575 	 */
   1576 	AUDIO_INITINFO(&ai);
   1577 	ai.mode = ai0.mode ^ AUMODE_PLAY_ALL;
   1578 	ai.play.sample_rate = 11025;
   1579 	ai.play.channels = 2;
   1580 	ai.play.precision = 16;
   1581 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1582 	ai.play.pause = 1;
   1583 	ai.record.sample_rate = 11025;
   1584 	ai.record.channels = 2;
   1585 	ai.record.precision = 16;
   1586 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1587 	ai.record.pause = 1;
   1588 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "ai");
   1589 	REQUIRED_SYS_EQ(0, r);
   1590 	r = CLOSE(fd);
   1591 	REQUIRED_SYS_EQ(0, r);
   1592 
   1593 	/*
   1594 	 * Open the same target device again and check
   1595 	 */
   1596 	fd = OPEN(devfile, mode);
   1597 	REQUIRED_SYS_OK(fd);
   1598 	memset(&ai, 0, sizeof(ai));
   1599 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1600 	REQUIRED_SYS_EQ(0, r);
   1601 
   1602 	XP_NE(0, ai.blocksize);
   1603 		/* hiwat/lowat */
   1604 	if (netbsd < 8) {
   1605 		/*
   1606 		 * On NetBSD7, the behavior when changing ai.mode on
   1607 		 * /dev/audioctl can not be explained yet but I won't
   1608 		 * verify it more over.
   1609 		 */
   1610 	} else {
   1611 		/* On NetBSD9, changing mode never affects other fds */
   1612 		XP_EQ(exp_mode, ai.mode);
   1613 	}
   1614 	/* ai.play */
   1615 	XP_EQ(exp_sample_rate, ai.play.sample_rate);
   1616 	XP_EQ(exp_channels, ai.play.channels);
   1617 	XP_EQ(exp_precision, ai.play.precision);
   1618 	XP_EQ(exp_encoding, ai.play.encoding);
   1619 		/* gain */
   1620 		/* port */
   1621 	XP_EQ(0, ai.play.seek);
   1622 		/* avail_ports */
   1623 	XP_NE(0, ai.play.buffer_size);
   1624 	XP_EQ(0, ai.play.samples);
   1625 	XP_EQ(0, ai.play.eof);
   1626 	XP_EQ(exp_pause, ai.play.pause);
   1627 	XP_EQ(0, ai.play.error);
   1628 	XP_EQ(0, ai.play.waiting);
   1629 		/* balance */
   1630 	XP_EQ(exp_popen, ai.play.open);
   1631 	XP_EQ(0, ai.play.active);
   1632 	/* ai.record */
   1633 	XP_EQ(exp_sample_rate, ai.record.sample_rate);
   1634 	XP_EQ(exp_channels, ai.record.channels);
   1635 	XP_EQ(exp_precision, ai.record.precision);
   1636 	XP_EQ(exp_encoding, ai.record.encoding);
   1637 		/* gain */
   1638 		/* port */
   1639 	XP_EQ(0, ai.record.seek);
   1640 		/* avail_ports */
   1641 	XP_NE(0, ai.record.buffer_size);
   1642 	XP_EQ(0, ai.record.samples);
   1643 	XP_EQ(0, ai.record.eof);
   1644 	XP_EQ(exp_pause, ai.record.pause);
   1645 	XP_EQ(0, ai.record.error);
   1646 	XP_EQ(0, ai.record.waiting);
   1647 		/* balance */
   1648 	XP_EQ(exp_ropen, ai.record.open);
   1649 	if (netbsd < 9 && strcmp(devname, "sound") == 0) {
   1650 		/*
   1651 		 * On NetBSD7/8, it doesn't seem to start recording on open
   1652 		 * for /dev/sound.  It should be a bug.
   1653 		 */
   1654 		XP_EQ(0, ai.record.active);
   1655 	} else {
   1656 		XP_EQ(exp_ropen, ai.record.active);
   1657 	}
   1658 
   1659 	r = CLOSE(fd);
   1660 	REQUIRED_SYS_EQ(0, r);
   1661 }
   1662 DEF(open_audio_RDONLY)		{ test_open("audio", O_RDONLY); }
   1663 DEF(open_audio_WRONLY)		{ test_open("audio", O_WRONLY); }
   1664 DEF(open_audio_RDWR)		{ test_open("audio", O_RDWR);   }
   1665 DEF(open_sound_RDONLY)		{ test_open("sound", O_RDONLY); }
   1666 DEF(open_sound_WRONLY)		{ test_open("sound", O_WRONLY); }
   1667 DEF(open_sound_RDWR)		{ test_open("sound", O_RDWR);   }
   1668 DEF(open_audioctl_RDONLY)	{ test_open("audioctl", O_RDONLY); }
   1669 DEF(open_audioctl_WRONLY)	{ test_open("audioctl", O_WRONLY); }
   1670 DEF(open_audioctl_RDWR)		{ test_open("audioctl", O_RDWR);   }
   1671 
   1672 /*
   1673  * Open (1) /dev/sound -> (2) /dev/audio -> (3) /dev/sound,
   1674  * Both of /dev/audio and /dev/sound share the sticky parameters,
   1675  * /dev/sound inherits and use it but /dev/audio initialize and use it.
   1676  * So 2nd audio descriptor affects 3rd sound descriptor.
   1677  */
   1678 DEF(open_sound_sticky)
   1679 {
   1680 	struct audio_info ai;
   1681 	int fd;
   1682 	int r;
   1683 	int openmode;
   1684 
   1685 	TEST("open_sound_sticky");
   1686 
   1687 	openmode = openable_mode();
   1688 
   1689 	/* First, open /dev/sound and change encoding as a delegate */
   1690 	fd = OPEN(devsound, openmode);
   1691 	REQUIRED_SYS_OK(fd);
   1692 	AUDIO_INITINFO(&ai);
   1693 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1694 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1695 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   1696 	REQUIRED_SYS_EQ(0, r);
   1697 	r = CLOSE(fd);
   1698 	REQUIRED_SYS_EQ(0, r);
   1699 
   1700 	/* Next, open /dev/audio.  It makes the encoding mulaw */
   1701 	fd = OPEN(devaudio, openmode);
   1702 	REQUIRED_SYS_OK(fd);
   1703 	r = CLOSE(fd);
   1704 	REQUIRED_SYS_EQ(0, r);
   1705 
   1706 	/* And then, open /dev/sound again */
   1707 	fd = OPEN(devsound, openmode);
   1708 	REQUIRED_SYS_OK(fd);
   1709 	memset(&ai, 0, sizeof(ai));
   1710 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1711 	REQUIRED_SYS_EQ(0, r);
   1712 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1713 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1714 	r = CLOSE(fd);
   1715 	REQUIRED_SYS_EQ(0, r);
   1716 }
   1717 
   1718 /*
   1719  * /dev/audioctl has stickiness like /dev/sound.
   1720  */
   1721 DEF(open_audioctl_sticky)
   1722 {
   1723 	struct audio_info ai;
   1724 	int fd;
   1725 	int r;
   1726 	int openmode;
   1727 
   1728 	TEST("open_audioctl_sticky");
   1729 
   1730 	openmode = openable_mode();
   1731 
   1732 	/* First, open /dev/audio and change encoding */
   1733 	fd = OPEN(devaudio, openmode);
   1734 	REQUIRED_SYS_OK(fd);
   1735 	AUDIO_INITINFO(&ai);
   1736 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1737 	ai.play.precision = 16;
   1738 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1739 	ai.record.precision = 16;
   1740 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "SLINEAR_LE");
   1741 	REQUIRED_SYS_EQ(0, r);
   1742 	r = CLOSE(fd);
   1743 	REQUIRED_SYS_EQ(0, r);
   1744 
   1745 	/* Next, open /dev/audioctl.  It should be affected */
   1746 	fd = OPEN(devaudioctl, openmode);
   1747 	REQUIRED_SYS_OK(fd);
   1748 	memset(&ai, 0, sizeof(ai));
   1749 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1750 	REQUIRED_SYS_EQ(0, r);
   1751 	XP_EQ(AUDIO_ENCODING_SLINEAR_LE, ai.play.encoding);
   1752 	XP_EQ(16, ai.play.precision);
   1753 	XP_EQ(AUDIO_ENCODING_SLINEAR_LE, ai.record.encoding);
   1754 	XP_EQ(16, ai.record.precision);
   1755 
   1756 	/* Then, change /dev/audioctl */
   1757 	AUDIO_INITINFO(&ai);
   1758 	ai.play.encoding = AUDIO_ENCODING_ULAW;
   1759 	ai.play.precision = 8;
   1760 	ai.record.encoding = AUDIO_ENCODING_ULAW;
   1761 	ai.record.precision = 8;
   1762 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "ULAW");
   1763 	REQUIRED_SYS_EQ(0, r);
   1764 	r = CLOSE(fd);
   1765 	REQUIRED_SYS_EQ(0, r);
   1766 
   1767 	/* Finally, open /dev/sound.  It also should be affected  */
   1768 	fd = OPEN(devsound, openmode);
   1769 	REQUIRED_SYS_OK(fd);
   1770 	memset(&ai, 0, sizeof(ai));
   1771 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1772 	REQUIRED_SYS_EQ(0, r);
   1773 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1774 	XP_EQ(8, ai.play.precision);
   1775 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1776 	XP_EQ(8, ai.record.precision);
   1777 	r = CLOSE(fd);
   1778 	REQUIRED_SYS_EQ(0, r);
   1779 }
   1780 
   1781 /*
   1782  * Open two descriptors simultaneously.
   1783  */
   1784 void
   1785 test_open_simul(int mode0, int mode1)
   1786 {
   1787 	struct audio_info ai;
   1788 	int fd0, fd1;
   1789 	int i;
   1790 	int r;
   1791 	int actmode;
   1792 #define AUMODE_BOTH (AUMODE_PLAY | AUMODE_RECORD)
   1793 	struct {
   1794 		int mode0;
   1795 		int mode1;
   1796 	} expfulltable[] = {
   1797 		/* expected fd0		expected fd1 (-errno expects error) */
   1798 		{ AUMODE_RECORD,	AUMODE_RECORD },	// REC, REC
   1799 		{ AUMODE_RECORD,	AUMODE_PLAY },		// REC, PLAY
   1800 		{ AUMODE_RECORD,	AUMODE_BOTH },		// REC, BOTH
   1801 		{ AUMODE_PLAY,		AUMODE_RECORD },	// PLAY, REC
   1802 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, PLAY
   1803 		{ AUMODE_PLAY,		AUMODE_BOTH },		// PLAY, BOTH
   1804 		{ AUMODE_BOTH,		AUMODE_RECORD },	// BOTH, REC
   1805 		{ AUMODE_BOTH,		AUMODE_PLAY },		// BOTH, PLAY
   1806 		{ AUMODE_BOTH,		AUMODE_BOTH },		// BOTH, BOTH
   1807 	},
   1808 	exphalftable[] = {
   1809 		/* expected fd0		expected fd1 (-errno expects error) */
   1810 		{ AUMODE_RECORD,	AUMODE_RECORD },	// REC, REC
   1811 		{ AUMODE_RECORD,	-ENODEV },		// REC, PLAY
   1812 		{ AUMODE_RECORD,	-ENODEV },		// REC, BOTH
   1813 		{ AUMODE_PLAY,		-ENODEV },		// PLAY, REC
   1814 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, PLAY
   1815 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, BOTH
   1816 		{ AUMODE_PLAY,		-ENODEV },		// BOTH, REC
   1817 		{ AUMODE_PLAY,		AUMODE_PLAY },		// BOTH, PLAY
   1818 		{ AUMODE_PLAY,		AUMODE_PLAY },		// BOTH, BOTH
   1819 	}, *exptable;
   1820 
   1821 	/* The expected values are different in half-duplex or full-duplex */
   1822 	if (hw_fulldup()) {
   1823 		exptable = expfulltable;
   1824 	} else {
   1825 		exptable = exphalftable;
   1826 	}
   1827 
   1828 	TEST("open_simul_%s_%s",
   1829 	    openmode_str[mode0] + 2,
   1830 	    openmode_str[mode1] + 2);
   1831 
   1832 	if (netbsd < 8) {
   1833 		XP_SKIP("Multiple open is not supported");
   1834 		return;
   1835 	}
   1836 
   1837 	if (mode2aumode(mode0) == 0 || mode2aumode(mode1) == 0) {
   1838 		XP_SKIP("Operation not allowed on this hardware property");
   1839 		return;
   1840 	}
   1841 
   1842 	i = mode0 * 3 + mode1;
   1843 
   1844 	/* Open first one */
   1845 	fd0 = OPEN(devaudio, mode0);
   1846 	REQUIRED_SYS_OK(fd0);
   1847 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "");
   1848 	REQUIRED_SYS_EQ(0, r);
   1849 	actmode = ai.mode & AUMODE_BOTH;
   1850 	XP_EQ(exptable[i].mode0, actmode);
   1851 
   1852 	/* Open second one */
   1853 	fd1 = OPEN(devaudio, mode1);
   1854 	if (exptable[i].mode1 >= 0) {
   1855 		/* Case to expect to be able to open */
   1856 		REQUIRED_SYS_OK(fd1);
   1857 		r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   1858 		XP_SYS_EQ(0, r);
   1859 		if (r == 0) {
   1860 			actmode = ai.mode & AUMODE_BOTH;
   1861 			XP_EQ(exptable[i].mode1, actmode);
   1862 		}
   1863 	} else {
   1864 		/* Case to expect not to be able to open */
   1865 		XP_SYS_NG(ENODEV, fd1);
   1866 		if (fd1 == -1) {
   1867 			XP_EQ(-exptable[i].mode1, errno);
   1868 		} else {
   1869 			r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   1870 			XP_SYS_EQ(0, r);
   1871 			if (r == 0) {
   1872 				actmode = ai.mode & AUMODE_BOTH;
   1873 				XP_FAIL("expects error but %d", actmode);
   1874 			}
   1875 		}
   1876 	}
   1877 
   1878 	if (fd1 >= 0) {
   1879 		r = CLOSE(fd1);
   1880 		XP_SYS_EQ(0, r);
   1881 	}
   1882 
   1883 	r = CLOSE(fd0);
   1884 	XP_SYS_EQ(0, r);
   1885 }
   1886 DEF(open_simul_RDONLY_RDONLY)	{ test_open_simul(O_RDONLY, O_RDONLY);	}
   1887 DEF(open_simul_RDONLY_WRONLY)	{ test_open_simul(O_RDONLY, O_WRONLY);	}
   1888 DEF(open_simul_RDONLY_RDWR)	{ test_open_simul(O_RDONLY, O_RDWR);	}
   1889 DEF(open_simul_WRONLY_RDONLY)	{ test_open_simul(O_WRONLY, O_RDONLY);	}
   1890 DEF(open_simul_WRONLY_WRONLY)	{ test_open_simul(O_WRONLY, O_WRONLY);	}
   1891 DEF(open_simul_WRONLY_RDWR)	{ test_open_simul(O_WRONLY, O_RDWR);	}
   1892 DEF(open_simul_RDWR_RDONLY)	{ test_open_simul(O_RDWR, O_RDONLY);	}
   1893 DEF(open_simul_RDWR_WRONLY)	{ test_open_simul(O_RDWR, O_WRONLY);	}
   1894 DEF(open_simul_RDWR_RDWR)	{ test_open_simul(O_RDWR, O_RDWR);	}
   1895 
   1896 /*
   1897  * /dev/audio can be opened by other user who opens /dev/audio.
   1898  */
   1899 void
   1900 try_open_multiuser(bool multiuser)
   1901 {
   1902 	int fd0;
   1903 	int fd1;
   1904 	int r;
   1905 	uid_t ouid;
   1906 
   1907 	/*
   1908 	 * Test1: Open as root first and then unprivileged user.
   1909 	 */
   1910 
   1911 	/* At first, open as root */
   1912 	fd0 = OPEN(devaudio, openable_mode());
   1913 	REQUIRED_SYS_OK(fd0);
   1914 
   1915 	ouid = GETUID();
   1916 	r = SETEUID(1);
   1917 	REQUIRED_SYS_EQ(0, r);
   1918 
   1919 	/* Then, open as unprivileged user */
   1920 	fd1 = OPEN(devaudio, openable_mode());
   1921 	if (multiuser) {
   1922 		/* If multiuser, another user also can open */
   1923 		XP_SYS_OK(fd1);
   1924 	} else {
   1925 		/* If not multiuser, another user cannot open */
   1926 		XP_SYS_NG(EPERM, fd1);
   1927 	}
   1928 	if (fd1 != -1) {
   1929 		r = CLOSE(fd1);
   1930 		XP_SYS_EQ(0, r);
   1931 	}
   1932 
   1933 	r = SETEUID(ouid);
   1934 	REQUIRED_SYS_EQ(0, r);
   1935 
   1936 	r = CLOSE(fd0);
   1937 	XP_SYS_EQ(0, r);
   1938 
   1939 	/*
   1940 	 * Test2: Open as unprivileged user first and then root.
   1941 	 */
   1942 
   1943 	/* At first, open as unprivileged user */
   1944 	ouid = GETUID();
   1945 	r = SETEUID(1);
   1946 	REQUIRED_SYS_EQ(0, r);
   1947 
   1948 	fd0 = OPEN(devaudio, openable_mode());
   1949 	REQUIRED_SYS_OK(fd0);
   1950 
   1951 	/* Then open as root */
   1952 	r = SETEUID(ouid);
   1953 	REQUIRED_SYS_EQ(0, r);
   1954 
   1955 	/* root always can open */
   1956 	fd1 = OPEN(devaudio, openable_mode());
   1957 	XP_SYS_OK(fd1);
   1958 	if (fd1 != -1) {
   1959 		r = CLOSE(fd1);
   1960 		XP_SYS_EQ(0, r);
   1961 	}
   1962 
   1963 	/* Close first one as unprivileged user */
   1964 	r = SETEUID(1);
   1965 	REQUIRED_SYS_EQ(0, r);
   1966 	r = CLOSE(fd0);
   1967 	XP_SYS_EQ(0, r);
   1968 	r = SETEUID(ouid);
   1969 	REQUIRED_SYS_EQ(0, r);
   1970 }
   1971 /*
   1972  * This is a wrapper for open_multiuser.
   1973  * XXX XP_* macros are not compatible with on-error-goto, we need try-catch...
   1974  */
   1975 void
   1976 test_open_multiuser(bool multiuser)
   1977 {
   1978 	char mibname[32];
   1979 	bool oldval;
   1980 	size_t oldlen;
   1981 	int r;
   1982 
   1983 	TEST("open_multiuser_%d", multiuser);
   1984 	if (netbsd < 8) {
   1985 		XP_SKIP("Multiple open is not supported");
   1986 		return;
   1987 	}
   1988 	if (netbsd < 9) {
   1989 		/* NetBSD8 has no way (difficult) to determine device name */
   1990 		XP_SKIP("NetBSD8 cannot determine device name");
   1991 		return;
   1992 	}
   1993 	if (geteuid() != 0) {
   1994 		XP_SKIP("Must be run as a privileged user");
   1995 		return;
   1996 	}
   1997 
   1998 	/* Get current multiuser mode (and save it) */
   1999 	snprintf(mibname, sizeof(mibname), "hw.%s.multiuser", devicename);
   2000 	oldlen = sizeof(oldval);
   2001 	r = SYSCTLBYNAME(mibname, &oldval, &oldlen, NULL, 0);
   2002 	REQUIRED_SYS_EQ(0, r);
   2003 	DPRINTF("  > multiuser=%d\n", oldval);
   2004 
   2005 	/* Change if necessary */
   2006 	if (oldval != multiuser) {
   2007 		r = SYSCTLBYNAME(mibname, NULL, NULL, &multiuser,
   2008 		    sizeof(multiuser));
   2009 		REQUIRED_SYS_EQ(0, r);
   2010 		DPRINTF("  > new multiuser=%d\n", multiuser);
   2011 	}
   2012 
   2013 	/* Do test */
   2014 	try_open_multiuser(multiuser);
   2015 
   2016 	/* Restore multiuser mode */
   2017 	if (oldval != multiuser) {
   2018 		DPRINTF("  > restore multiuser to %d\n", oldval);
   2019 		r = SYSCTLBYNAME(mibname, NULL, NULL, &oldval, sizeof(oldval));
   2020 		REQUIRED_SYS_EQ(0, r);
   2021 	}
   2022 }
   2023 DEF(open_multiuser_0)	{ test_open_multiuser(false); }
   2024 DEF(open_multiuser_1)	{ test_open_multiuser(true); }
   2025 
   2026 /*
   2027  * Normal playback (with PLAY_ALL).
   2028  * It does not verify real playback data.
   2029  */
   2030 DEF(write_PLAY_ALL)
   2031 {
   2032 	char buf[8000];
   2033 	int fd;
   2034 	int r;
   2035 
   2036 	TEST("write_PLAY_ALL");
   2037 
   2038 	fd = OPEN(devaudio, O_WRONLY);
   2039 	if (hw_canplay()) {
   2040 		REQUIRED_SYS_OK(fd);
   2041 	} else {
   2042 		XP_SYS_NG(ENXIO, fd);
   2043 		return;
   2044 	}
   2045 
   2046 	/* mulaw 1sec silence */
   2047 	memset(buf, 0xff, sizeof(buf));
   2048 	r = WRITE(fd, buf, sizeof(buf));
   2049 	XP_SYS_EQ(sizeof(buf), r);
   2050 
   2051 	r = CLOSE(fd);
   2052 	XP_SYS_EQ(0, r);
   2053 }
   2054 
   2055 /*
   2056  * Normal playback (without PLAY_ALL).
   2057  * It does not verify real playback data.
   2058  */
   2059 DEF(write_PLAY)
   2060 {
   2061 	struct audio_info ai;
   2062 	char *wav;
   2063 	int wavsize;
   2064 	int totalsize;
   2065 	int fd;
   2066 	int r;
   2067 
   2068 	TEST("write_PLAY");
   2069 
   2070 	fd = OPEN(devaudio, O_WRONLY);
   2071 	if (hw_canplay()) {
   2072 		REQUIRED_SYS_OK(fd);
   2073 	} else {
   2074 		XP_SYS_NG(ENXIO, fd);
   2075 		return;
   2076 	}
   2077 
   2078 	/* Drop PLAY_ALL */
   2079 	AUDIO_INITINFO(&ai);
   2080 	ai.mode = AUMODE_PLAY;
   2081 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "mode");
   2082 	REQUIRED_SYS_EQ(0, r);
   2083 
   2084 	/* Check mode and get blocksize */
   2085 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2086 	REQUIRED_SYS_EQ(0, r);
   2087 	XP_EQ(AUMODE_PLAY, ai.mode);
   2088 
   2089 	wavsize = ai.blocksize;
   2090 	wav = (char *)malloc(wavsize);
   2091 	REQUIRED_IF(wav != NULL);
   2092 	memset(wav, 0xff, wavsize);
   2093 
   2094 	/* Write blocks until 1sec */
   2095 	for (totalsize = 0; totalsize < 8000; ) {
   2096 		r = WRITE(fd, wav, wavsize);
   2097 		XP_SYS_EQ(wavsize, r);
   2098 		if (r == -1)
   2099 			break;	/* XXX */
   2100 		totalsize += r;
   2101 	}
   2102 
   2103 	/* XXX What should I test it? */
   2104 	/* Check ai.play.error */
   2105 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2106 	REQUIRED_SYS_EQ(0, r);
   2107 	XP_EQ(0, ai.play.error);
   2108 
   2109 	/* Playback data is no longer necessary */
   2110 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   2111 	REQUIRED_SYS_EQ(0, r);
   2112 
   2113 	r = CLOSE(fd);
   2114 	REQUIRED_SYS_EQ(0, r);
   2115 
   2116 	free(wav);
   2117 }
   2118 
   2119 /*
   2120  * Normal recording.
   2121  * It does not verify real recorded data.
   2122  */
   2123 DEF(read)
   2124 {
   2125 	char buf[8000];
   2126 	int fd;
   2127 	int r;
   2128 
   2129 	TEST("read");
   2130 
   2131 	fd = OPEN(devaudio, O_RDONLY);
   2132 	if (hw_canrec()) {
   2133 		REQUIRED_SYS_OK(fd);
   2134 	} else {
   2135 		XP_SYS_NG(ENXIO, fd);
   2136 		return;
   2137 	}
   2138 
   2139 	/* mulaw 1sec */
   2140 	r = READ(fd, buf, sizeof(buf));
   2141 	XP_SYS_EQ(sizeof(buf), r);
   2142 
   2143 	r = CLOSE(fd);
   2144 	XP_SYS_EQ(0, r);
   2145 }
   2146 
   2147 /*
   2148  * Repeat open-write-close cycle.
   2149  */
   2150 DEF(rept_write)
   2151 {
   2152 	struct timeval start, end, result;
   2153 	double res;
   2154 	char buf[8000];	/* 1sec in 8bit-mulaw,1ch,8000Hz */
   2155 	int fd;
   2156 	int r;
   2157 	int n;
   2158 
   2159 	TEST("rept_write");
   2160 
   2161 	if (hw_canplay() == 0) {
   2162 		XP_SKIP("This test is only for playable device");
   2163 		return;
   2164 	}
   2165 
   2166 	/* XXX It may timeout on some hardware driver. */
   2167 	XP_SKIP("not yet");
   2168 	return;
   2169 
   2170 	memset(buf, 0xff, sizeof(buf));
   2171 	n = 3;
   2172 	gettimeofday(&start, NULL);
   2173 	for (int i = 0; i < n; i++) {
   2174 		fd = OPEN(devaudio, O_WRONLY);
   2175 		REQUIRED_SYS_OK(fd);
   2176 
   2177 		r = WRITE(fd, buf, sizeof(buf));
   2178 		XP_SYS_EQ(sizeof(buf), r);
   2179 
   2180 		r = CLOSE(fd);
   2181 		XP_SYS_EQ(0, r);
   2182 	}
   2183 	gettimeofday(&end, NULL);
   2184 	timersub(&end, &start, &result);
   2185 	res = (double)result.tv_sec + (double)result.tv_usec / 1000000;
   2186 	/* Make judgement but not too strict */
   2187 	if (res >= n * 1.5) {
   2188 		XP_FAIL("expects %d sec but %4.1f sec", n, res);
   2189 		return;
   2190 	}
   2191 }
   2192 
   2193 /*
   2194  * Repeat open-read-close cycle.
   2195  */
   2196 DEF(rept_read)
   2197 {
   2198 	struct timeval start, end, result;
   2199 	double res;
   2200 	char buf[8000];	/* 1sec in 8bit-mulaw,1ch,8000Hz */
   2201 	int fd;
   2202 	int r;
   2203 	int n;
   2204 
   2205 	TEST("rept_read");
   2206 
   2207 	if (hw_canrec() == 0) {
   2208 		XP_SKIP("This test is only for recordable device");
   2209 		return;
   2210 	}
   2211 
   2212 	/* XXX It may timeout on some hardware driver. */
   2213 	XP_SKIP("not yet");
   2214 	return;
   2215 
   2216 	n = 3;
   2217 	gettimeofday(&start, NULL);
   2218 	for (int i = 0; i < n; i++) {
   2219 		fd = OPEN(devaudio, O_RDONLY);
   2220 		REQUIRED_SYS_OK(fd);
   2221 
   2222 		r = READ(fd, buf, sizeof(buf));
   2223 		XP_SYS_EQ(sizeof(buf), r);
   2224 
   2225 		r = CLOSE(fd);
   2226 		XP_SYS_EQ(0, r);
   2227 	}
   2228 	gettimeofday(&end, NULL);
   2229 	timersub(&end, &start, &result);
   2230 	res = (double)result.tv_sec + (double)result.tv_usec / 1000000;
   2231 	/* Make judgement but not too strict */
   2232 	if (res >= n * 1.5) {
   2233 		XP_FAIL("expects %d sec but %4.1f sec", n, res);
   2234 		return;
   2235 	}
   2236 }
   2237 
   2238 /*
   2239  * Opening with O_RDWR on half-duplex hardware falls back to O_WRONLY.
   2240  * expwrite: expected to be able to play.
   2241  * expread : expected to be able to recored.
   2242  */
   2243 void
   2244 test_rdwr_fallback(int openmode, bool expwrite, bool expread)
   2245 {
   2246 	struct audio_info ai;
   2247 	char buf[10];
   2248 	int fd;
   2249 	int r;
   2250 
   2251 	TEST("rdwr_fallback_%s", openmode_str[openmode] + 2);
   2252 
   2253 	if (hw_bidir() == 0) {
   2254 		XP_SKIP("This test is only for bi-directional device");
   2255 		return;
   2256 	}
   2257 
   2258 	AUDIO_INITINFO(&ai);
   2259 	ai.play.pause = 1;
   2260 	ai.record.pause = 1;
   2261 
   2262 	fd = OPEN(devaudio, openmode);
   2263 	REQUIRED_SYS_OK(fd);
   2264 
   2265 	/* Set pause not to play noise */
   2266 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   2267 	REQUIRED_SYS_EQ(0, r);
   2268 
   2269 	memset(buf, 0xff, sizeof(buf));
   2270 	r = WRITE(fd, buf, sizeof(buf));
   2271 	if (expwrite) {
   2272 		XP_SYS_EQ(sizeof(buf), r);
   2273 	} else {
   2274 		XP_SYS_NG(EBADF, r);
   2275 	}
   2276 
   2277 	r = READ(fd, buf, 0);
   2278 	if (expread) {
   2279 		XP_SYS_EQ(0, r);
   2280 	} else {
   2281 		XP_SYS_NG(EBADF, r);
   2282 	}
   2283 
   2284 	r = CLOSE(fd);
   2285 	REQUIRED_SYS_EQ(0, r);
   2286 }
   2287 DEF(rdwr_fallback_RDONLY) { test_rdwr_fallback(O_RDONLY, false, true); }
   2288 DEF(rdwr_fallback_WRONLY) { test_rdwr_fallback(O_WRONLY, true, false); }
   2289 DEF(rdwr_fallback_RDWR) {
   2290 	bool expread;
   2291 	/*
   2292 	 * On NetBSD7, O_RDWR on half-duplex is accepted. It's possible to
   2293 	 * read and write if they don't occur at the same time.
   2294 	 * On NetBSD9, O_RDWR on half-duplex falls back O_WRONLY.
   2295 	 */
   2296 	if (netbsd < 8) {
   2297 		expread = true;
   2298 	} else {
   2299 		expread = hw_fulldup() ? true : false;
   2300 	}
   2301 	test_rdwr_fallback(O_RDWR, true, expread);
   2302 }
   2303 
   2304 /*
   2305  * On full-duplex hardware, the second descriptor's readablity/writability
   2306  * is not depend on the first descriptor('s open mode).
   2307  * On half-duplex hardware, it depends on the first descriptor's open mode.
   2308  */
   2309 void
   2310 test_rdwr_two(int mode0, int mode1)
   2311 {
   2312 	struct audio_info ai;
   2313 	char wbuf[100];	/* 1/80sec in 8bit-mulaw,1ch,8000Hz */
   2314 	char rbuf[100];	/* 1/80sec in 8bit-mulaw,1ch,8000Hz */
   2315 	bool canopen;
   2316 	bool canwrite;
   2317 	bool canread;
   2318 	int fd0;
   2319 	int fd1;
   2320 	int r;
   2321 	struct {
   2322 		bool canopen;
   2323 		bool canwrite;
   2324 		bool canread;
   2325 	} exptable_full[] = {
   2326 	/*	open write read	   1st, 2nd mode */
   2327 		{ 1, 0, 1 },	/* REC, REC */
   2328 		{ 1, 1, 0 },	/* REC, PLAY */
   2329 		{ 1, 1, 1 },	/* REC, BOTH */
   2330 		{ 1, 0, 1 },	/* PLAY, REC */
   2331 		{ 1, 1, 0 },	/* PLAY, PLAY */
   2332 		{ 1, 1, 1 },	/* PLAY, BOTH */
   2333 		{ 1, 0, 1 },	/* BOTH, REC */
   2334 		{ 1, 1, 0 },	/* BOTH, PLAY */
   2335 		{ 1, 1, 1 },	/* BOTH, BOTH */
   2336 	},
   2337 	exptable_half[] = {
   2338 		{ 1, 0, 1 },	/* REC, REC */
   2339 		{ 0, 0, 0 },	/* REC, PLAY */
   2340 		{ 0, 0, 0 },	/* REC, BOTH */
   2341 		{ 0, 0, 0 },	/* PLAY, REC */
   2342 		{ 1, 1, 0 },	/* PLAY, PLAY */
   2343 		{ 1, 1, 0 },	/* PLAY, BOTH */
   2344 		{ 0, 0, 0 },	/* BOTH, REC */
   2345 		{ 1, 1, 0 },	/* BOTH, PLAY */
   2346 		{ 0, 0, 0 },	/* BOTH, BOTH */
   2347 	}, *exptable;
   2348 
   2349 	TEST("rdwr_two_%s_%s",
   2350 	    openmode_str[mode0] + 2,
   2351 	    openmode_str[mode1] + 2);
   2352 
   2353 	if (netbsd < 8) {
   2354 		XP_SKIP("Multiple open is not supported");
   2355 		return;
   2356 	}
   2357 	if (hw_bidir() == 0) {
   2358 		XP_SKIP("This test is only for bi-directional device");
   2359 		return;
   2360 	}
   2361 
   2362 	exptable = hw_fulldup() ? exptable_full : exptable_half;
   2363 
   2364 	canopen  = exptable[mode0 * 3 + mode1].canopen;
   2365 	canwrite = exptable[mode0 * 3 + mode1].canwrite;
   2366 	canread  = exptable[mode0 * 3 + mode1].canread;
   2367 
   2368 	if (!canopen) {
   2369 		XP_SKIP("This combination is not openable on half-duplex");
   2370 		return;
   2371 	}
   2372 
   2373 	fd0 = OPEN(devaudio, mode0);
   2374 	REQUIRED_SYS_OK(fd0);
   2375 
   2376 	fd1 = OPEN(devaudio, mode1);
   2377 	REQUIRED_SYS_OK(fd1);
   2378 
   2379 	/* Silent data to make no sound */
   2380 	memset(&wbuf, 0xff, sizeof(wbuf));
   2381 	/* Pause to make no sound */
   2382 	AUDIO_INITINFO(&ai);
   2383 	ai.play.pause = 1;
   2384 	r = IOCTL(fd0, AUDIO_SETINFO, &ai, "pause");
   2385 	XP_SYS_EQ(0, r);
   2386 
   2387 	/* write(fd1) */
   2388 	r = WRITE(fd1, wbuf, sizeof(wbuf));
   2389 	if (canwrite) {
   2390 		XP_SYS_EQ(100, r);
   2391 	} else {
   2392 		XP_SYS_NG(EBADF, r);
   2393 	}
   2394 
   2395 	/* read(fd1) */
   2396 	r = READ(fd1, rbuf, sizeof(rbuf));
   2397 	if (canread) {
   2398 		XP_SYS_EQ(100, r);
   2399 	} else {
   2400 		XP_SYS_NG(EBADF, r);
   2401 	}
   2402 
   2403 	r = CLOSE(fd0);
   2404 	XP_SYS_EQ(0, r);
   2405 	r = CLOSE(fd1);
   2406 	XP_SYS_EQ(0, r);
   2407 }
   2408 DEF(rdwr_two_RDONLY_RDONLY)	{ test_rdwr_two(O_RDONLY, O_RDONLY);	}
   2409 DEF(rdwr_two_RDONLY_WRONLY)	{ test_rdwr_two(O_RDONLY, O_WRONLY);	}
   2410 DEF(rdwr_two_RDONLY_RDWR)	{ test_rdwr_two(O_RDONLY, O_RDWR);	}
   2411 DEF(rdwr_two_WRONLY_RDONLY)	{ test_rdwr_two(O_WRONLY, O_RDONLY);	}
   2412 DEF(rdwr_two_WRONLY_WRONLY)	{ test_rdwr_two(O_WRONLY, O_WRONLY);	}
   2413 DEF(rdwr_two_WRONLY_RDWR)	{ test_rdwr_two(O_WRONLY, O_RDWR);	}
   2414 DEF(rdwr_two_RDWR_RDONLY)	{ test_rdwr_two(O_RDWR, O_RDONLY);	}
   2415 DEF(rdwr_two_RDWR_WRONLY)	{ test_rdwr_two(O_RDWR, O_WRONLY);	}
   2416 DEF(rdwr_two_RDWR_RDWR)		{ test_rdwr_two(O_RDWR, O_RDWR);	}
   2417 
   2418 /*
   2419  * Read and write different descriptors simultaneously.
   2420  * Only on full-duplex.
   2421  */
   2422 DEF(rdwr_simul)
   2423 {
   2424 	char wbuf[1000];	/* 1/8sec in mulaw,1ch,8kHz */
   2425 	char rbuf[1000];
   2426 	int fd0;
   2427 	int fd1;
   2428 	int r;
   2429 	int status;
   2430 	pid_t pid;
   2431 
   2432 	TEST("rdwr_simul");
   2433 	if (netbsd < 8) {
   2434 		XP_SKIP("Multiple open is not supported");
   2435 		return;
   2436 	}
   2437 	if (!hw_fulldup()) {
   2438 		XP_SKIP("This test is only for full-duplex device");
   2439 		return;
   2440 	}
   2441 
   2442 	/* Silence data to make no sound */
   2443 	memset(wbuf, 0xff, sizeof(wbuf));
   2444 
   2445 	fd0 = OPEN(devaudio, O_WRONLY);
   2446 	REQUIRED_SYS_OK(fd0);
   2447 	fd1 = OPEN(devaudio, O_RDONLY);
   2448 	REQUIRED_SYS_OK(fd1);
   2449 
   2450 	fflush(stdout);
   2451 	fflush(stderr);
   2452 	pid = fork();
   2453 	if (pid == -1)
   2454 		xp_err(1, __LINE__, "fork");
   2455 
   2456 	if (pid == 0) {
   2457 		/* child (read) */
   2458 		for (int i = 0; i < 10; i++) {
   2459 			r = READ(fd1, rbuf, sizeof(rbuf));
   2460 			if (r == -1)
   2461 				xp_err(1, __LINE__, "read(i=%d)", i);
   2462 		}
   2463 		exit(0);
   2464 	} else {
   2465 		/* parent (write) */
   2466 		for (int i = 0; i < 10; i++) {
   2467 			r = WRITE(fd0, wbuf, sizeof(wbuf));
   2468 			if (r == -1)
   2469 				xp_err(1, __LINE__, "write(i=%d)", i);
   2470 		}
   2471 		waitpid(pid, &status, 0);
   2472 	}
   2473 
   2474 	CLOSE(fd0);
   2475 	CLOSE(fd1);
   2476 	/* If you reach here, consider as success */
   2477 	XP_EQ(0, 0);
   2478 }
   2479 
   2480 /*
   2481  * DRAIN should work even on incomplete data left.
   2482  */
   2483 DEF(drain_incomplete)
   2484 {
   2485 	struct audio_info ai;
   2486 	int r;
   2487 	int fd;
   2488 
   2489 	TEST("drain_incomplete");
   2490 
   2491 	if (hw_canplay() == 0) {
   2492 		XP_SKIP("This test is only for playable device");
   2493 		return;
   2494 	}
   2495 
   2496 	fd = OPEN(devaudio, O_WRONLY);
   2497 	REQUIRED_SYS_OK(fd);
   2498 
   2499 	AUDIO_INITINFO(&ai);
   2500 	/* let precision > 8 */
   2501 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   2502 	ai.play.precision = 16;
   2503 	ai.mode = AUMODE_PLAY;
   2504 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   2505 	REQUIRED_SYS_EQ(0, r);
   2506 	/* Write one byte and then close */
   2507 	r = WRITE(fd, &r, 1);
   2508 	XP_SYS_EQ(1, r);
   2509 	r = CLOSE(fd);
   2510 	XP_SYS_EQ(0, r);
   2511 }
   2512 
   2513 /*
   2514  * DRAIN should work even in pause.
   2515  */
   2516 DEF(drain_pause)
   2517 {
   2518 	struct audio_info ai;
   2519 	int r;
   2520 	int fd;
   2521 
   2522 	TEST("drain_pause");
   2523 
   2524 	if (hw_canplay() == 0) {
   2525 		XP_SKIP("This test is only for playable device");
   2526 		return;
   2527 	}
   2528 
   2529 	fd = OPEN(devaudio, O_WRONLY);
   2530 	REQUIRED_SYS_OK(fd);
   2531 
   2532 	/* Set pause */
   2533 	AUDIO_INITINFO(&ai);
   2534 	ai.play.pause = 1;
   2535 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   2536 	XP_SYS_EQ(0, r);
   2537 	/* Write some data and then close */
   2538 	r = WRITE(fd, &r, 4);
   2539 	XP_SYS_EQ(4, r);
   2540 	r = CLOSE(fd);
   2541 	XP_SYS_EQ(0, r);
   2542 }
   2543 
   2544 /*
   2545  * DRAIN does not affect for record-only descriptor.
   2546  */
   2547 DEF(drain_onrec)
   2548 {
   2549 	int fd;
   2550 	int r;
   2551 
   2552 	TEST("drain_onrec");
   2553 
   2554 	if (hw_canrec() == 0) {
   2555 		XP_SKIP("This test is only for recordable device");
   2556 		return;
   2557 	}
   2558 
   2559 	fd = OPEN(devaudio, O_RDONLY);
   2560 	REQUIRED_SYS_OK(fd);
   2561 
   2562 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   2563 	XP_SYS_EQ(0, r);
   2564 
   2565 	r = CLOSE(fd);
   2566 	XP_SYS_EQ(0, r);
   2567 }
   2568 
   2569 /*
   2570  * Whether mmap() succeeds with specified parameter.
   2571  */
   2572 void
   2573 test_mmap_mode(int mode, int prot)
   2574 {
   2575 	char buf[10];
   2576 	struct audio_info ai;
   2577 	const char *protstr;
   2578 	int expected;
   2579 	int fd;
   2580 	int r;
   2581 	int len;
   2582 	void *ptr;
   2583 
   2584 	if (prot == PROT_NONE) {
   2585 		protstr = "NONE";
   2586 	} else if (prot == PROT_READ) {
   2587 		protstr = "READ";
   2588 	} else if (prot == PROT_WRITE) {
   2589 		protstr = "WRITE";
   2590 	} else if (prot == (PROT_READ | PROT_WRITE)) {
   2591 		protstr = "READWRITE";
   2592 	} else {
   2593 		xp_errx(1, __LINE__, "unknown prot %x\n", prot);
   2594 	}
   2595 	TEST("mmap_%s_%s", openmode_str[mode] + 2, protstr);
   2596 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2597 		XP_SKIP("This test is only for mmap-able device");
   2598 		return;
   2599 	}
   2600 	if (mode2aumode(mode) == 0) {
   2601 		XP_SKIP("Operation not allowed on this hardware property");
   2602 		return;
   2603 	}
   2604 #if !defined(NO_RUMP)
   2605 	if (use_rump) {
   2606 		XP_SKIP("rump doesn't support mmap");
   2607 		return;
   2608 	}
   2609 #endif
   2610 
   2611 	/*
   2612 	 * On NetBSD7 and 8, mmap() always succeeds regardless of open mode.
   2613 	 * On NetBSD9, mmap() succeeds only for writable descriptor.
   2614 	 */
   2615 	expected = mode2play(mode);
   2616 	if (netbsd < 9) {
   2617 		expected = true;
   2618 	}
   2619 
   2620 	fd = OPEN(devaudio, mode);
   2621 	REQUIRED_SYS_OK(fd);
   2622 
   2623 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "get");
   2624 	REQUIRED_SYS_EQ(0, r);
   2625 
   2626 	len = ai.play.buffer_size;
   2627 
   2628 	/* Make it pause */
   2629 	AUDIO_INITINFO(&ai);
   2630 	ai.play.pause = 1;
   2631 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   2632 	REQUIRED_SYS_EQ(0, r);
   2633 
   2634 	ptr = MMAP(NULL, len, prot, MAP_FILE, fd, 0);
   2635 	XP_SYS_PTR(expected ? 0 : EACCES, ptr);
   2636 	if (expected) {
   2637 		/* XXX Doing mmap(2) doesn't inhibit read(2) */
   2638 		if (mode2rec(mode)) {
   2639 			r = READ(fd, buf, 0);
   2640 			XP_SYS_EQ(0, r);
   2641 		}
   2642 		/* Doing mmap(2) inhibits write(2) */
   2643 		if (mode2play(mode)) {
   2644 			/* NetBSD9 changes errno */
   2645 			r = WRITE(fd, buf, 0);
   2646 			if (netbsd < 9) {
   2647 				XP_SYS_NG(EINVAL, r);
   2648 			} else {
   2649 				XP_SYS_NG(EPERM, r);
   2650 			}
   2651 		}
   2652 	}
   2653 	if (ptr != MAP_FAILED) {
   2654 		r = MUNMAP(ptr, len);
   2655 		XP_SYS_EQ(0, r);
   2656 	}
   2657 
   2658 	/* Whether the pause is still valid */
   2659 	if (mode2play(mode)) {
   2660 		r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2661 		XP_SYS_EQ(0, r);
   2662 		XP_EQ(1, ai.play.pause);
   2663 	}
   2664 
   2665 	r = CLOSE(fd);
   2666 	XP_SYS_EQ(0, r);
   2667 
   2668 	reset_after_mmap();
   2669 }
   2670 #define PROT_READWRITE	(PROT_READ | PROT_WRITE)
   2671 DEF(mmap_mode_RDONLY_NONE)	{ test_mmap_mode(O_RDONLY, PROT_NONE); }
   2672 DEF(mmap_mode_RDONLY_READ)	{ test_mmap_mode(O_RDONLY, PROT_READ); }
   2673 DEF(mmap_mode_RDONLY_WRITE)	{ test_mmap_mode(O_RDONLY, PROT_WRITE); }
   2674 DEF(mmap_mode_RDONLY_READWRITE)	{ test_mmap_mode(O_RDONLY, PROT_READWRITE); }
   2675 DEF(mmap_mode_WRONLY_NONE)	{ test_mmap_mode(O_WRONLY, PROT_NONE); }
   2676 DEF(mmap_mode_WRONLY_READ)	{ test_mmap_mode(O_WRONLY, PROT_READ); }
   2677 DEF(mmap_mode_WRONLY_WRITE)	{ test_mmap_mode(O_WRONLY, PROT_WRITE); }
   2678 DEF(mmap_mode_WRONLY_READWRITE)	{ test_mmap_mode(O_WRONLY, PROT_READWRITE); }
   2679 DEF(mmap_mode_RDWR_NONE)	{ test_mmap_mode(O_RDWR, PROT_NONE); }
   2680 DEF(mmap_mode_RDWR_READ)	{ test_mmap_mode(O_RDWR, PROT_READ); }
   2681 DEF(mmap_mode_RDWR_WRITE)	{ test_mmap_mode(O_RDWR, PROT_WRITE); }
   2682 DEF(mmap_mode_RDWR_READWRITE)	{ test_mmap_mode(O_RDWR, PROT_READWRITE); }
   2683 
   2684 /*
   2685  * Check mmap()'s length and offset.
   2686  */
   2687 DEF(mmap_len)
   2688 {
   2689 	struct audio_info ai;
   2690 	int fd;
   2691 	int r;
   2692 	size_t len;
   2693 	off_t offset;
   2694 	void *ptr;
   2695 	int bufsize;
   2696 	int pagesize;
   2697 	int lsize;
   2698 
   2699 	TEST("mmap_len");
   2700 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2701 		XP_SKIP("This test is only for mmap-able device");
   2702 		return;
   2703 	}
   2704 #if !defined(NO_RUMP)
   2705 	if (use_rump) {
   2706 		XP_SKIP("rump doesn't support mmap");
   2707 		return;
   2708 	}
   2709 #endif
   2710 
   2711 	len = sizeof(pagesize);
   2712 	r = SYSCTLBYNAME("hw.pagesize", &pagesize, &len, NULL, 0);
   2713 	REQUIRED_SYS_EQ(0, r);
   2714 
   2715 	fd = OPEN(devaudio, O_WRONLY);
   2716 	REQUIRED_SYS_OK(r);
   2717 
   2718 	/* Get buffer_size */
   2719 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2720 	REQUIRED_SYS_EQ(0, r);
   2721 	bufsize = ai.play.buffer_size;
   2722 
   2723 	/*
   2724 	 * XXX someone refers bufsize and another one does pagesize.
   2725 	 * I'm not sure.
   2726 	 */
   2727 	lsize = roundup2(bufsize, pagesize);
   2728 	struct {
   2729 		size_t len;
   2730 		off_t offset;
   2731 		int exp;
   2732 	} table[] = {
   2733 		/* len offset	expected */
   2734 
   2735 		{ 0,	0,	0 },		/* len is 0  */
   2736 		{ 1,	0,	0 },		/* len is smaller than lsize */
   2737 		{ lsize, 0,	0 },		/* len is the same as lsize */
   2738 		{ lsize + 1, 0,	EOVERFLOW },	/* len is larger */
   2739 
   2740 		{ 0, -1,	EINVAL },	/* offset is negative */
   2741 		{ 0, lsize,	0 },		/* pointless param but ok */
   2742 		{ 0, lsize + 1,	EOVERFLOW },	/* exceed */
   2743 		{ 1, lsize,	EOVERFLOW },	/* exceed */
   2744 
   2745 		/*
   2746 		 * When you treat offset as 32bit, offset will be 0
   2747 		 * and thus it incorrectly succeeds.
   2748 		 */
   2749 		{ lsize,	1ULL<<32,	EOVERFLOW },
   2750 	};
   2751 
   2752 	for (int i = 0; i < (int)__arraycount(table); i++) {
   2753 		len = table[i].len;
   2754 		offset = table[i].offset;
   2755 		int exp = table[i].exp;
   2756 
   2757 		ptr = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, offset);
   2758 		if (exp == 0) {
   2759 			XP_SYS_PTR(0, ptr);
   2760 		} else {
   2761 			/* NetBSD8 introduces EOVERFLOW */
   2762 			if (netbsd < 8 && exp == EOVERFLOW)
   2763 				exp = EINVAL;
   2764 			XP_SYS_PTR(exp, ptr);
   2765 		}
   2766 
   2767 		if (ptr != MAP_FAILED) {
   2768 			r = MUNMAP(ptr, len);
   2769 			XP_SYS_EQ(0, r);
   2770 		}
   2771 	}
   2772 
   2773 	r = CLOSE(fd);
   2774 	XP_SYS_EQ(0, r);
   2775 
   2776 	reset_after_mmap();
   2777 }
   2778 
   2779 /*
   2780  * mmap() the same descriptor twice.
   2781  */
   2782 DEF(mmap_twice)
   2783 {
   2784 	struct audio_info ai;
   2785 	int fd;
   2786 	int r;
   2787 	int len;
   2788 	void *ptr1;
   2789 	void *ptr2;
   2790 
   2791 	TEST("mmap_twice");
   2792 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2793 		XP_SKIP("This test is only for mmap-able device");
   2794 		return;
   2795 	}
   2796 #if !defined(NO_RUMP)
   2797 	if (use_rump) {
   2798 		XP_SKIP("rump doesn't support mmap");
   2799 		return;
   2800 	}
   2801 #endif
   2802 
   2803 	fd = OPEN(devaudio, O_WRONLY);
   2804 	REQUIRED_SYS_OK(fd);
   2805 
   2806 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "get");
   2807 	REQUIRED_SYS_EQ(0, r);
   2808 	len = ai.play.buffer_size;
   2809 
   2810 	ptr1 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, 0);
   2811 	XP_SYS_PTR(0, ptr1);
   2812 
   2813 	/* XXX I'm not sure this sucess is intended.  Anyway I follow it */
   2814 	ptr2 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, 0);
   2815 	XP_SYS_PTR(0, ptr2);
   2816 
   2817 	if (ptr2 != MAP_FAILED) {
   2818 		r = MUNMAP(ptr2, len);
   2819 		XP_SYS_EQ(0, r);
   2820 	}
   2821 	if (ptr1 != MAP_FAILED) {
   2822 		r = MUNMAP(ptr1, len);
   2823 		XP_SYS_EQ(0, r);
   2824 	}
   2825 
   2826 	r = CLOSE(fd);
   2827 	XP_SYS_EQ(0, r);
   2828 
   2829 	reset_after_mmap();
   2830 }
   2831 
   2832 /*
   2833  * mmap() different descriptors.
   2834  */
   2835 DEF(mmap_multi)
   2836 {
   2837 	struct audio_info ai;
   2838 	int fd0;
   2839 	int fd1;
   2840 	int r;
   2841 	int len;
   2842 	void *ptr0;
   2843 	void *ptr1;
   2844 
   2845 	TEST("mmap_multi");
   2846 	if (netbsd < 8) {
   2847 		XP_SKIP("Multiple open is not supported");
   2848 		return;
   2849 	}
   2850 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2851 		XP_SKIP("This test is only for mmap-able device");
   2852 		return;
   2853 	}
   2854 #if !defined(NO_RUMP)
   2855 	if (use_rump) {
   2856 		XP_SKIP("rump doesn't support mmap");
   2857 		return;
   2858 	}
   2859 #endif
   2860 
   2861 	fd0 = OPEN(devaudio, O_WRONLY);
   2862 	REQUIRED_SYS_OK(fd0);
   2863 
   2864 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "get");
   2865 	REQUIRED_SYS_EQ(0, r);
   2866 	len = ai.play.buffer_size;
   2867 
   2868 	fd1 = OPEN(devaudio, O_WRONLY);
   2869 	REQUIRED_SYS_OK(fd1);
   2870 
   2871 	ptr0 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd0, 0);
   2872 	XP_SYS_PTR(0, ptr0);
   2873 
   2874 	ptr1 = MMAP(NULL, len,  PROT_WRITE, MAP_FILE, fd1, 0);
   2875 	XP_SYS_PTR(0, ptr1);
   2876 
   2877 	if (ptr0 != MAP_FAILED) {
   2878 		r = MUNMAP(ptr1, len);
   2879 		XP_SYS_EQ(0, r);
   2880 	}
   2881 
   2882 	r = CLOSE(fd1);
   2883 	XP_SYS_EQ(0, r);
   2884 
   2885 	if (ptr1 != MAP_FAILED) {
   2886 		r = MUNMAP(ptr0, len);
   2887 		XP_SYS_EQ(0, r);
   2888 	}
   2889 
   2890 	r = CLOSE(fd0);
   2891 	XP_SYS_EQ(0, r);
   2892 
   2893 	reset_after_mmap();
   2894 }
   2895 
   2896 #define IN	POLLIN
   2897 #define OUT	POLLOUT
   2898 /*
   2899  * Whether poll() succeeds with specified mode.
   2900  */
   2901 void
   2902 test_poll_mode(int mode, int events, int expected_revents)
   2903 {
   2904 	struct pollfd pfd;
   2905 	const char *events_str;
   2906 	int fd;
   2907 	int r;
   2908 	int expected_r;
   2909 
   2910 	if (events == IN) {
   2911 		events_str = "IN";
   2912 	} else if (events == OUT) {
   2913 		events_str = "OUT";
   2914 	} else if (events == (IN | OUT)) {
   2915 		events_str = "INOUT";
   2916 	} else {
   2917 		events_str = "?";
   2918 	}
   2919 	TEST("poll_mode_%s_%s", openmode_str[mode] + 2, events_str);
   2920 	if (mode2aumode(mode) == 0) {
   2921 		XP_SKIP("Operation not allowed on this hardware property");
   2922 		return;
   2923 	}
   2924 
   2925 	expected_r = (expected_revents != 0) ? 1 : 0;
   2926 
   2927 	fd = OPEN(devaudio, mode);
   2928 	REQUIRED_SYS_OK(fd);
   2929 
   2930 	/* Wait a bit to be recorded. */
   2931 	usleep(100 * 1000);
   2932 
   2933 	memset(&pfd, 0, sizeof(pfd));
   2934 	pfd.fd = fd;
   2935 	pfd.events = events;
   2936 
   2937 	r = POLL(&pfd, 1, 100);
   2938 	/* It's a bit complicated..  */
   2939 	if (r < 0 || r > 1) {
   2940 		/*
   2941 		 * Check these two cases first:
   2942 		 * - system call fails.
   2943 		 * - poll() with one nfds returns >1.  It's strange.
   2944 		 */
   2945 		XP_SYS_EQ(expected_r, r);
   2946 	} else {
   2947 		/*
   2948 		 * Otherwise, poll() returned 0 or 1.
   2949 		 */
   2950 		DPRINTF("  > pfd.revents=%s\n", event_tostr(pfd.revents));
   2951 
   2952 		/* NetBSD7,8 have several strange behavior.  It must be bug. */
   2953 
   2954 		XP_SYS_EQ(expected_r, r);
   2955 		XP_EQ(expected_revents, pfd.revents);
   2956 	}
   2957 	r = CLOSE(fd);
   2958 	XP_SYS_EQ(0, r);
   2959 }
   2960 DEF(poll_mode_RDONLY_IN)	{ test_poll_mode(O_RDONLY, IN,     IN); }
   2961 DEF(poll_mode_RDONLY_OUT)	{ test_poll_mode(O_RDONLY, OUT,    0); }
   2962 DEF(poll_mode_RDONLY_INOUT)	{ test_poll_mode(O_RDONLY, IN|OUT, IN); }
   2963 DEF(poll_mode_WRONLY_IN)	{ test_poll_mode(O_WRONLY, IN,     0); }
   2964 DEF(poll_mode_WRONLY_OUT)	{ test_poll_mode(O_WRONLY, OUT,	   OUT); }
   2965 DEF(poll_mode_WRONLY_INOUT)	{ test_poll_mode(O_WRONLY, IN|OUT, OUT); }
   2966 DEF(poll_mode_RDWR_IN)		{
   2967 	/* On half-duplex, O_RDWR is the same as O_WRONLY. */
   2968 	if (hw_fulldup()) test_poll_mode(O_RDWR,   IN,     IN);
   2969 	else		  test_poll_mode(O_RDWR,   IN,     0);
   2970 }
   2971 DEF(poll_mode_RDWR_OUT)		{ test_poll_mode(O_RDWR,   OUT,	   OUT); }
   2972 DEF(poll_mode_RDWR_INOUT)	{
   2973 	/* On half-duplex, O_RDWR is the same as O_WRONLY. */
   2974 	if (hw_fulldup()) test_poll_mode(O_RDWR,   IN|OUT, IN|OUT);
   2975 	else		  test_poll_mode(O_RDWR,   IN|OUT,    OUT);
   2976 }
   2977 
   2978 /*
   2979  * Poll(OUT) when buffer is empty.
   2980  */
   2981 DEF(poll_out_empty)
   2982 {
   2983 	struct pollfd pfd;
   2984 	int fd;
   2985 	int r;
   2986 
   2987 	TEST("poll_out_empty");
   2988 
   2989 	fd = OPEN(devaudio, O_WRONLY);
   2990 	REQUIRED_SYS_OK(fd);
   2991 
   2992 	memset(&pfd, 0, sizeof(pfd));
   2993 	pfd.fd = fd;
   2994 	pfd.events = POLLOUT;
   2995 
   2996 	/* Check when empty.  It should succeed even if timeout == 0 */
   2997 	r = POLL(&pfd, 1, 0);
   2998 	XP_SYS_EQ(1, r);
   2999 	XP_EQ(POLLOUT, pfd.revents);
   3000 
   3001 	r = CLOSE(fd);
   3002 	XP_SYS_EQ(0, r);
   3003 }
   3004 
   3005 /*
   3006  * Poll(OUT) when buffer is full.
   3007  */
   3008 DEF(poll_out_full)
   3009 {
   3010 	struct audio_info ai;
   3011 	struct pollfd pfd;
   3012 	int fd;
   3013 	int r;
   3014 	char *buf;
   3015 	int buflen;
   3016 
   3017 	TEST("poll_out_full");
   3018 
   3019 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3020 	REQUIRED_SYS_OK(fd);
   3021 
   3022 	/* Pause */
   3023 	AUDIO_INITINFO(&ai);
   3024 	ai.play.pause = 1;
   3025 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   3026 	XP_SYS_EQ(0, r);
   3027 
   3028 	/* Get buffer size */
   3029 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3030 	XP_SYS_EQ(0, r);
   3031 
   3032 	/* Write until full */
   3033 	buflen = ai.play.buffer_size;
   3034 	buf = (char *)malloc(buflen);
   3035 	REQUIRED_IF(buf != NULL);
   3036 	memset(buf, 0xff, buflen);
   3037 	do {
   3038 		r = WRITE(fd, buf, buflen);
   3039 	} while (r == buflen);
   3040 	if (r == -1) {
   3041 		XP_SYS_NG(EAGAIN, r);
   3042 	}
   3043 
   3044 	/* Do poll */
   3045 	memset(&pfd, 0, sizeof(pfd));
   3046 	pfd.fd = fd;
   3047 	pfd.events = POLLOUT;
   3048 	r = POLL(&pfd, 1, 0);
   3049 	XP_SYS_EQ(0, r);
   3050 	XP_EQ(0, pfd.revents);
   3051 
   3052 	r = CLOSE(fd);
   3053 	XP_SYS_EQ(0, r);
   3054 	free(buf);
   3055 }
   3056 
   3057 /*
   3058  * Poll(OUT) when buffer is full but hiwat sets lower than full.
   3059  */
   3060 DEF(poll_out_hiwat)
   3061 {
   3062 	struct audio_info ai;
   3063 	struct pollfd pfd;
   3064 	int fd;
   3065 	int r;
   3066 	char *buf;
   3067 	int buflen;
   3068 	int newhiwat;
   3069 
   3070 	TEST("poll_out_hiwat");
   3071 
   3072 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3073 	REQUIRED_SYS_OK(fd);
   3074 
   3075 	/* Get buffer size and hiwat */
   3076 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3077 	XP_SYS_EQ(0, r);
   3078 	/* Change hiwat some different value */
   3079 	newhiwat = ai.lowat;
   3080 
   3081 	/* Set pause and hiwat */
   3082 	AUDIO_INITINFO(&ai);
   3083 	ai.play.pause = 1;
   3084 	ai.hiwat = newhiwat;
   3085 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1;hiwat");
   3086 	XP_SYS_EQ(0, r);
   3087 
   3088 	/* Get the set hiwat again */
   3089 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3090 	XP_SYS_EQ(0, r);
   3091 
   3092 	/* Write until full */
   3093 	buflen = ai.blocksize * ai.hiwat;
   3094 	buf = (char *)malloc(buflen);
   3095 	REQUIRED_IF(buf != NULL);
   3096 	memset(buf, 0xff, buflen);
   3097 	do {
   3098 		r = WRITE(fd, buf, buflen);
   3099 	} while (r == buflen);
   3100 	if (r == -1) {
   3101 		XP_SYS_NG(EAGAIN, r);
   3102 	}
   3103 
   3104 	/* Do poll */
   3105 	memset(&pfd, 0, sizeof(pfd));
   3106 	pfd.fd = fd;
   3107 	pfd.events = POLLOUT;
   3108 	r = POLL(&pfd, 1, 0);
   3109 	XP_SYS_EQ(0, r);
   3110 	XP_EQ(0, pfd.revents);
   3111 
   3112 	r = CLOSE(fd);
   3113 	XP_SYS_EQ(0, r);
   3114 	free(buf);
   3115 }
   3116 
   3117 /*
   3118  * Unpause from buffer full, POLLOUT should raise.
   3119  * XXX poll(2) on NetBSD7 is really incomplete and wierd.  I don't test it.
   3120  */
   3121 DEF(poll_out_unpause)
   3122 {
   3123 	struct audio_info ai;
   3124 	struct pollfd pfd;
   3125 	int fd;
   3126 	int r;
   3127 	char *buf;
   3128 	int buflen;
   3129 	u_int blocksize;
   3130 	int hiwat;
   3131 	int lowat;
   3132 
   3133 	TEST("poll_out_unpause");
   3134 	if (netbsd < 8) {
   3135 		XP_SKIP("NetBSD7's poll() is too incomplete to test.");
   3136 		return;
   3137 	}
   3138 
   3139 	/* Non-blocking open */
   3140 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3141 	REQUIRED_SYS_OK(fd);
   3142 
   3143 	/* Adjust block size and hiwat/lowat to make the test time 1sec */
   3144 	blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3145 	hiwat = 12;		/* 1.5sec */
   3146 	lowat = 4;		/* 0.5sec */
   3147 	AUDIO_INITINFO(&ai);
   3148 	ai.blocksize = blocksize;
   3149 	ai.hiwat = hiwat;
   3150 	ai.lowat = lowat;
   3151 	/* and also set encoding */
   3152 	/*
   3153 	 * XXX NetBSD7 has different results depending on whether the input
   3154 	 * encoding is emulated (AUDIO_ENCODINGFLAG_EMULATED) or not.  It's
   3155 	 * not easy to ensure this situation on all hardware environment.
   3156 	 * On NetBSD9, the result is the same regardless of input encoding.
   3157 	 */
   3158 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "blocksize=%d", blocksize);
   3159 	XP_SYS_EQ(0, r);
   3160 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3161 	if (ai.blocksize != blocksize) {
   3162 		/*
   3163 		 * NetBSD9 can not change the blocksize.  Then,
   3164 		 * adjust using hiwat/lowat.
   3165 		 */
   3166 		blocksize = ai.blocksize;
   3167 		hiwat = howmany(8000 * 1.5, blocksize);
   3168 		lowat = howmany(8000 * 0.5, blocksize);
   3169 	}
   3170 	/* Anyway, set the parameters */
   3171 	AUDIO_INITINFO(&ai);
   3172 	ai.blocksize = blocksize;
   3173 	ai.hiwat = hiwat;
   3174 	ai.lowat = lowat;
   3175 	ai.play.pause = 1;
   3176 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   3177 	XP_SYS_EQ(0, r);
   3178 
   3179 	/* Get the set parameters again */
   3180 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3181 	XP_SYS_EQ(0, r);
   3182 
   3183 	/* Write until full */
   3184 	buflen = ai.blocksize * ai.hiwat;
   3185 	buf = (char *)malloc(buflen);
   3186 	REQUIRED_IF(buf != NULL);
   3187 	memset(buf, 0xff, buflen);
   3188 	do {
   3189 		r = WRITE(fd, buf, buflen);
   3190 	} while (r == buflen);
   3191 	if (r == -1) {
   3192 		XP_SYS_NG(EAGAIN, r);
   3193 	}
   3194 
   3195 	/* At this time, POLLOUT should not be set because buffer is full */
   3196 	memset(&pfd, 0, sizeof(pfd));
   3197 	pfd.fd = fd;
   3198 	pfd.events = POLLOUT;
   3199 	r = POLL(&pfd, 1, 0);
   3200 	XP_SYS_EQ(0, r);
   3201 	XP_EQ(0, pfd.revents);
   3202 
   3203 	/* Unpause */
   3204 	AUDIO_INITINFO(&ai);
   3205 	ai.play.pause = 0;
   3206 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=0");
   3207 	XP_SYS_EQ(0, r);
   3208 
   3209 	/*
   3210 	 * When unpause occurs:
   3211 	 * - NetBSD7 (emul=0) -> the buffer remains.
   3212 	 * - NetBSD7 (emul=1) -> the buffer is cleared.
   3213 	 * - NetBSD8          -> the buffer remains.
   3214 	 * - NetBSD9          -> the buffer remains.
   3215 	 */
   3216 
   3217 	/* Check poll() up to 2sec */
   3218 	pfd.revents = 0;
   3219 	r = POLL(&pfd, 1, 2000);
   3220 	XP_SYS_EQ(1, r);
   3221 	XP_EQ(POLLOUT, pfd.revents);
   3222 
   3223 	/*
   3224 	 * Since POLLOUT is set, it should be writable.
   3225 	 * But at this time, no all buffer may be writable.
   3226 	 */
   3227 	r = WRITE(fd, buf, buflen);
   3228 	XP_SYS_OK(r);
   3229 
   3230 	/* Flush it because there is no need to play it */
   3231 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   3232 	XP_SYS_EQ(0, r);
   3233 
   3234 	r = CLOSE(fd);
   3235 	XP_SYS_EQ(0, r);
   3236 	free(buf);
   3237 }
   3238 
   3239 /*
   3240  * poll(2) must not be affected by playback of other descriptors.
   3241  */
   3242 DEF(poll_out_simul)
   3243 {
   3244 	struct audio_info ai;
   3245 	struct pollfd pfd[2];
   3246 	int fd[2];
   3247 	int r;
   3248 	char *buf;
   3249 	u_int blocksize;
   3250 	int hiwat;
   3251 	int lowat;
   3252 	int buflen;
   3253 	int time;
   3254 
   3255 	TEST("poll_out_simul");
   3256 	if (netbsd < 8) {
   3257 		XP_SKIP("Multiple open is not supported");
   3258 		return;
   3259 	}
   3260 
   3261 	/* Make sure that it's not affected by descriptor order */
   3262 	for (int i = 0; i < 2; i++) {
   3263 		int a = i;
   3264 		int b = 1 - i;
   3265 
   3266 		fd[0] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3267 		REQUIRED_SYS_OK(fd[0]);
   3268 		fd[1] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3269 		REQUIRED_SYS_OK(fd[1]);
   3270 
   3271 		/*
   3272 		 * Adjust block size and hiwat/lowat.
   3273 		 * I want to choice suitable blocksize (if possible).
   3274 		 */
   3275 		blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3276 		hiwat = 12;		/* 1.5sec */
   3277 		lowat = 4;		/* 0.5sec */
   3278 		AUDIO_INITINFO(&ai);
   3279 		ai.blocksize = blocksize;
   3280 		ai.hiwat = hiwat;
   3281 		ai.lowat = lowat;
   3282 		r = IOCTL(fd[0], AUDIO_SETINFO, &ai, "blocksize=1000");
   3283 		XP_SYS_EQ(0, r);
   3284 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "read back blocksize");
   3285 		if (ai.blocksize != blocksize) {
   3286 			/*
   3287 			 * NetBSD9 can not change the blocksize.  Then,
   3288 			 * adjust using hiwat/lowat.
   3289 			 */
   3290 			blocksize = ai.blocksize;
   3291 			hiwat = howmany(8000 * 1.5, blocksize);
   3292 			lowat = howmany(8000 * 0.5, blocksize);
   3293 		}
   3294 		/* Anyway, set the parameters */
   3295 		AUDIO_INITINFO(&ai);
   3296 		ai.blocksize = blocksize;
   3297 		ai.hiwat = hiwat;
   3298 		ai.lowat = lowat;
   3299 		/* Pause fdA */
   3300 		ai.play.pause = 1;
   3301 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=1");
   3302 		XP_SYS_EQ(0, r);
   3303 		/* Unpause fdB */
   3304 		ai.play.pause = 0;
   3305 		r = IOCTL(fd[b], AUDIO_SETINFO, &ai, "pause=0");
   3306 		XP_SYS_EQ(0, r);
   3307 
   3308 		/* Get again. XXX two individual ioctls are correct */
   3309 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "");
   3310 		XP_SYS_EQ(0, r);
   3311 		DPRINTF("  > blocksize=%d lowat=%d hiwat=%d\n",
   3312 			ai.blocksize, ai.lowat, ai.hiwat);
   3313 
   3314 		/* Enough long time than the playback time */
   3315 		time = (ai.hiwat - ai.lowat) * blocksize / 8;  /*[msec]*/
   3316 		time *= 2;
   3317 
   3318 		/* Write fdA full */
   3319 		buflen = blocksize * ai.lowat;
   3320 		buf = (char *)malloc(buflen);
   3321 		REQUIRED_IF(buf != NULL);
   3322 		memset(buf, 0xff, buflen);
   3323 		do {
   3324 			r = WRITE(fd[a], buf, buflen);
   3325 		} while (r == buflen);
   3326 		if (r == -1) {
   3327 			XP_SYS_NG(EAGAIN, r);
   3328 		}
   3329 
   3330 		/* POLLOUT should not be set, because fdA is buffer full */
   3331 		memset(pfd, 0, sizeof(pfd));
   3332 		pfd[0].fd = fd[a];
   3333 		pfd[0].events = POLLOUT;
   3334 		r = POLL(pfd, 1, 0);
   3335 		XP_SYS_EQ(0, r);
   3336 		XP_EQ(0, pfd[0].revents);
   3337 
   3338 		/* Write fdB at least lowat */
   3339 		r = WRITE(fd[b], buf, buflen);
   3340 		XP_SYS_EQ(buflen, r);
   3341 		r = WRITE(fd[b], buf, buflen);
   3342 		if (r == -1) {
   3343 			XP_SYS_NG(EAGAIN, r);
   3344 		}
   3345 
   3346 		/* Only fdB should become POLLOUT */
   3347 		memset(pfd, 0, sizeof(pfd));
   3348 		pfd[0].fd = fd[0];
   3349 		pfd[0].events = POLLOUT;
   3350 		pfd[1].fd = fd[1];
   3351 		pfd[1].events = POLLOUT;
   3352 		r = POLL(pfd, 2, time);
   3353 		XP_SYS_EQ(1, r);
   3354 		if (r != -1) {
   3355 			XP_EQ(0, pfd[a].revents);
   3356 			XP_EQ(POLLOUT, pfd[b].revents);
   3357 		}
   3358 
   3359 		/* Drop the rest */
   3360 		r = IOCTL(fd[0], AUDIO_FLUSH, NULL, "");
   3361 		XP_SYS_EQ(0, r);
   3362 		r = IOCTL(fd[1], AUDIO_FLUSH, NULL, "");
   3363 		XP_SYS_EQ(0, r);
   3364 
   3365 		r = CLOSE(fd[0]);
   3366 		XP_SYS_EQ(0, r);
   3367 		r = CLOSE(fd[1]);
   3368 		XP_SYS_EQ(0, r);
   3369 		free(buf);
   3370 
   3371 		xxx_close_wait();
   3372 	}
   3373 }
   3374 
   3375 /*
   3376  * Open with READ mode starts recording immediately.
   3377  * Of course, audioctl doesn't start.
   3378  */
   3379 void
   3380 test_poll_in_open(const char *devname)
   3381 {
   3382 	struct audio_info ai;
   3383 	struct pollfd pfd;
   3384 	char buf[4096];
   3385 	char devfile[16];
   3386 	int fd;
   3387 	int r;
   3388 	bool is_audioctl;
   3389 
   3390 	TEST("poll_in_open_%s", devname);
   3391 	if (hw_canrec() == 0) {
   3392 		XP_SKIP("This test is only for recordable device");
   3393 		return;
   3394 	}
   3395 
   3396 	snprintf(devfile, sizeof(devfile), "/dev/%s%d", devname, unit);
   3397 	is_audioctl = (strcmp(devname, "audioctl") == 0);
   3398 
   3399 	fd = OPEN(devfile, O_RDONLY);
   3400 	REQUIRED_SYS_OK(fd);
   3401 
   3402 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3403 	REQUIRED_SYS_EQ(0, r);
   3404 	if (is_audioctl) {
   3405 		/* opening /dev/audioctl doesn't start recording. */
   3406 		XP_EQ(0, ai.record.active);
   3407 	} else {
   3408 		/* opening /dev/{audio,sound} starts recording. */
   3409 		/*
   3410 		 * On NetBSD7/8, opening /dev/sound doesn't start recording.
   3411 		 * It must be a bug.
   3412 		 */
   3413 		XP_EQ(1, ai.record.active);
   3414 	}
   3415 
   3416 	memset(&pfd, 0, sizeof(pfd));
   3417 	pfd.fd = fd;
   3418 	pfd.events = POLLIN;
   3419 	r = POLL(&pfd, 1, 1000);
   3420 	if (is_audioctl) {
   3421 		/*
   3422 		 * poll-ing /dev/audioctl always fails.
   3423 		 * XXX Returning error instead of timeout should be better(?).
   3424 		 */
   3425 		REQUIRED_SYS_EQ(0, r);
   3426 	} else {
   3427 		/*
   3428 		 * poll-ing /dev/{audio,sound} will succeed when recorded
   3429 		 * data is arrived.
   3430 		 */
   3431 		/*
   3432 		 * On NetBSD7/8, opening /dev/sound doesn't start recording.
   3433 		 * It must be a bug.
   3434 		 */
   3435 		REQUIRED_SYS_EQ(1, r);
   3436 
   3437 		/* In this case, read() should succeed. */
   3438 		r = READ(fd, buf, sizeof(buf));
   3439 		XP_SYS_OK(r);
   3440 		XP_NE(0, r);
   3441 	}
   3442 
   3443 	r = CLOSE(fd);
   3444 	XP_SYS_EQ(0, r);
   3445 }
   3446 DEF(poll_in_open_audio)		{ test_poll_in_open("audio"); }
   3447 DEF(poll_in_open_sound)		{ test_poll_in_open("sound"); }
   3448 DEF(poll_in_open_audioctl)	{ test_poll_in_open("audioctl"); }
   3449 
   3450 /*
   3451  * poll(2) must not be affected by other recording descriptors even if
   3452  * playback descriptor waits with POLLIN (though it's not normal usage).
   3453  * In other words, two POLLIN must not interfere.
   3454  */
   3455 DEF(poll_in_simul)
   3456 {
   3457 	struct audio_info ai;
   3458 	struct pollfd pfd;
   3459 	int fd[2];
   3460 	int r;
   3461 	char *buf;
   3462 	int blocksize;
   3463 
   3464 	TEST("poll_in_simul");
   3465 	if (netbsd < 8) {
   3466 		XP_SKIP("Multiple open is not supported");
   3467 		return;
   3468 	}
   3469 	if (hw_fulldup() == 0) {
   3470 		XP_SKIP("This test is only for full-duplex device");
   3471 		return;
   3472 	}
   3473 
   3474 	int play = 0;
   3475 	int rec = 1;
   3476 
   3477 	fd[play] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3478 	REQUIRED_SYS_OK(fd[play]);
   3479 	fd[rec] = OPEN(devaudio, O_RDONLY);
   3480 	REQUIRED_SYS_OK(fd[rec]);
   3481 
   3482 	/* Get block size */
   3483 	r = IOCTL(fd[rec], AUDIO_GETBUFINFO, &ai, "");
   3484 	XP_SYS_EQ(0, r);
   3485 	blocksize = ai.blocksize;
   3486 
   3487 	buf = (char *)malloc(blocksize);
   3488 	REQUIRED_IF(buf != NULL);
   3489 
   3490 	/*
   3491 	 * At first, make sure the playback one doesn't return POLLIN.
   3492 	 */
   3493 	memset(&pfd, 0, sizeof(pfd));
   3494 	pfd.fd = fd[play];
   3495 	pfd.events = POLLIN;
   3496 	r = POLL(&pfd, 1, 0);
   3497 	if (r == 0 && pfd.revents == 0) {
   3498 		XP_SYS_EQ(0, r);
   3499 		XP_EQ(0, pfd.revents);
   3500 	} else {
   3501 		XP_FAIL("play fd returns POLLIN");
   3502 		goto abort;
   3503 	}
   3504 
   3505 	/* Start recording */
   3506 	r = READ(fd[rec], buf, blocksize);
   3507 	XP_SYS_EQ(blocksize, r);
   3508 
   3509 	/* Poll()ing playback descriptor with POLLIN should not raise */
   3510 	r = POLL(&pfd, 1, 1000);
   3511 	XP_SYS_EQ(0, r);
   3512 	XP_EQ(0, pfd.revents);
   3513 
   3514 	/* Poll()ing recording descriptor with POLLIN should raise */
   3515 	pfd.fd = fd[rec];
   3516 	r = POLL(&pfd, 1, 0);
   3517 	XP_SYS_EQ(1, r);
   3518 	XP_EQ(POLLIN, pfd.revents);
   3519 
   3520 abort:
   3521 	r = CLOSE(fd[play]);
   3522 	XP_SYS_EQ(0, r);
   3523 	r = CLOSE(fd[rec]);
   3524 	XP_SYS_EQ(0, r);
   3525 	free(buf);
   3526 }
   3527 
   3528 /*
   3529  * Whether kqueue() succeeds with specified mode.
   3530  */
   3531 void
   3532 test_kqueue_mode(int openmode, int filt, int expected)
   3533 {
   3534 	struct kevent kev;
   3535 	struct timespec ts;
   3536 	int fd;
   3537 	int kq;
   3538 	int r;
   3539 
   3540 	TEST("kqueue_mode_%s_%s",
   3541 	    openmode_str[openmode] + 2,
   3542 	    (filt == EVFILT_READ) ? "READ" : "WRITE");
   3543 	if (mode2aumode(openmode) == 0) {
   3544 		XP_SKIP("Operation not allowed on this hardware property");
   3545 		return;
   3546 	}
   3547 
   3548 	ts.tv_sec = 0;
   3549 	ts.tv_nsec = 100 * 1000 * 1000;	// 100msec
   3550 
   3551 	kq = KQUEUE();
   3552 	XP_SYS_OK(kq);
   3553 
   3554 	fd = OPEN(devaudio, openmode);
   3555 	REQUIRED_SYS_OK(fd);
   3556 
   3557 	/*
   3558 	 * Check whether the specified filter can be set.
   3559 	 * Any filters can always be set, even if pointless combination.
   3560 	 * For example, EVFILT_READ can be set on O_WRONLY descriptor
   3561 	 * though it will never raise.
   3562 	 * I will not mention about good or bad of this behavior here.
   3563 	 */
   3564 	EV_SET(&kev, fd, filt, EV_ADD, 0, 0, 0);
   3565 	r = KEVENT_SET(kq, &kev, 1);
   3566 	XP_SYS_EQ(0, r);
   3567 
   3568 	if (r == 0) {
   3569 		/* If the filter can be set, try kevent(poll) */
   3570 		r = KEVENT_POLL(kq, &kev, 1, &ts);
   3571 		XP_SYS_EQ(expected, r);
   3572 
   3573 		/* Delete it */
   3574 		EV_SET(&kev, fd, filt, EV_DELETE, 0, 0, 0);
   3575 		r = KEVENT_SET(kq, &kev, 1);
   3576 		XP_SYS_EQ(0, r);
   3577 	}
   3578 
   3579 	r = CLOSE(fd);
   3580 	XP_SYS_EQ(0, r);
   3581 	r = CLOSE(kq);
   3582 	XP_SYS_EQ(0, r);
   3583 }
   3584 DEF(kqueue_mode_RDONLY_READ) {
   3585 	/* Should not raise yet (NetBSD7 has bugs?) */
   3586 	int expected = (netbsd < 8) ? 1 : 0;
   3587 	test_kqueue_mode(O_RDONLY, EVFILT_READ, expected);
   3588 }
   3589 DEF(kqueue_mode_RDONLY_WRITE) {
   3590 	/* Should never raise (NetBSD7 has bugs) */
   3591 	int expected = (netbsd < 8) ? 1 : 0;
   3592 	test_kqueue_mode(O_RDONLY, EVFILT_WRITE, expected);
   3593 }
   3594 DEF(kqueue_mode_WRONLY_READ) {
   3595 	/* Should never raise */
   3596 	test_kqueue_mode(O_WRONLY, EVFILT_READ, 0);
   3597 }
   3598 DEF(kqueue_mode_WRONLY_WRITE) {
   3599 	/* Should raise */
   3600 	test_kqueue_mode(O_WRONLY, EVFILT_WRITE, 1);
   3601 }
   3602 DEF(kqueue_mode_RDWR_READ) {
   3603 	/* Should not raise yet (NetBSD7 is something strange) */
   3604 	int expected = (netbsd < 8 && hw_fulldup()) ? 1 : 0;
   3605 	test_kqueue_mode(O_RDWR, EVFILT_READ, expected);
   3606 }
   3607 DEF(kqueue_mode_RDWR_WRITE) {
   3608 	/* Should raise */
   3609 	test_kqueue_mode(O_RDWR, EVFILT_WRITE, 1);
   3610 }
   3611 
   3612 /*
   3613  * kqueue(2) when buffer is empty.
   3614  */
   3615 DEF(kqueue_empty)
   3616 {
   3617 	struct audio_info ai;
   3618 	struct kevent kev;
   3619 	struct timespec ts;
   3620 	int kq;
   3621 	int fd;
   3622 	int r;
   3623 
   3624 	TEST("kqueue_empty");
   3625 
   3626 	fd = OPEN(devaudio, O_WRONLY);
   3627 	REQUIRED_SYS_OK(fd);
   3628 
   3629 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3630 	XP_SYS_EQ(0, r);
   3631 
   3632 	kq = KQUEUE();
   3633 	XP_SYS_OK(kq);
   3634 
   3635 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3636 	r = KEVENT_SET(kq, &kev, 1);
   3637 	XP_SYS_EQ(0, r);
   3638 
   3639 	/* When the buffer is empty, it should succeed even if timeout == 0 */
   3640 	memset(&ts, 0, sizeof(ts));
   3641 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3642 	XP_SYS_EQ(1, r);
   3643 	XP_EQ(fd, kev.ident);
   3644 	/*
   3645 	 * XXX According to kqueue(2) manpage, returned kev.data contains
   3646 	 * "the amount of space remaining in the write buffer".
   3647 	 * NetBSD7 returns buffer_size.  Shouldn't it be blocksize * hiwat?
   3648 	 */
   3649 	/* XP_EQ(ai.blocksize * ai.hiwat, kev.data); */
   3650 	XP_EQ(ai.play.buffer_size, kev.data);
   3651 
   3652 	r = CLOSE(fd);
   3653 	XP_SYS_EQ(0, r);
   3654 	r = CLOSE(kq);
   3655 	XP_SYS_EQ(0, r);
   3656 }
   3657 
   3658 /*
   3659  * kqueue(2) when buffer is full.
   3660  */
   3661 DEF(kqueue_full)
   3662 {
   3663 	struct audio_info ai;
   3664 	struct kevent kev;
   3665 	struct timespec ts;
   3666 	int kq;
   3667 	int fd;
   3668 	int r;
   3669 	char *buf;
   3670 	int buflen;
   3671 
   3672 	TEST("kqueue_full");
   3673 
   3674 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3675 	REQUIRED_SYS_OK(fd);
   3676 
   3677 	/* Pause */
   3678 	AUDIO_INITINFO(&ai);
   3679 	ai.play.pause = 1;
   3680 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   3681 	XP_SYS_EQ(0, r);
   3682 
   3683 	/* Get buffer size */
   3684 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3685 	XP_SYS_EQ(0, r);
   3686 
   3687 	/* Write until full */
   3688 	buflen = ai.play.buffer_size;
   3689 	buf = (char *)malloc(buflen);
   3690 	REQUIRED_IF(buf != NULL);
   3691 	memset(buf, 0xff, buflen);
   3692 	do {
   3693 		r = WRITE(fd, buf, buflen);
   3694 	} while (r == buflen);
   3695 	if (r == -1) {
   3696 		XP_SYS_NG(EAGAIN, r);
   3697 	}
   3698 
   3699 	kq = KQUEUE();
   3700 	XP_SYS_OK(kq);
   3701 
   3702 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3703 	r = KEVENT_SET(kq, &kev, 1);
   3704 	XP_SYS_EQ(0, r);
   3705 
   3706 	/* kevent() should not raise */
   3707 	ts.tv_sec = 0;
   3708 	ts.tv_nsec = 100L * 1000 * 1000;	/* 100msec */
   3709 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3710 	XP_SYS_EQ(0, r);
   3711 	if (r > 0) {
   3712 		XP_EQ(fd, kev.ident);
   3713 		XP_EQ(0, kev.data);
   3714 	}
   3715 
   3716 	r = CLOSE(fd);
   3717 	XP_SYS_EQ(0, r);
   3718 	r = CLOSE(kq);
   3719 	XP_SYS_EQ(0, r);
   3720 	free(buf);
   3721 }
   3722 
   3723 /*
   3724  * kqueue(2) when buffer is full but hiwat sets lower than full.
   3725  */
   3726 DEF(kqueue_hiwat)
   3727 {
   3728 	struct audio_info ai;
   3729 	struct kevent kev;
   3730 	struct timespec ts;
   3731 	int kq;
   3732 	int fd;
   3733 	int r;
   3734 	char *buf;
   3735 	int buflen;
   3736 	int newhiwat;
   3737 
   3738 	TEST("kqueue_hiwat");
   3739 
   3740 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3741 	REQUIRED_SYS_OK(fd);
   3742 
   3743 	/* Get buffer size and hiwat */
   3744 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "hiwat");
   3745 	XP_SYS_EQ(0, r);
   3746 	/* Change hiwat some different value */
   3747 	newhiwat = ai.hiwat - 1;
   3748 
   3749 	/* Set pause and hiwat */
   3750 	AUDIO_INITINFO(&ai);
   3751 	ai.play.pause = 1;
   3752 	ai.hiwat = newhiwat;
   3753 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1;hiwat");
   3754 	XP_SYS_EQ(0, r);
   3755 
   3756 	/* Get the set parameters again */
   3757 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3758 	XP_SYS_EQ(0, r);
   3759 	XP_EQ(1, ai.play.pause);
   3760 	XP_EQ(newhiwat, ai.hiwat);
   3761 
   3762 	/* Write until full */
   3763 	buflen = ai.blocksize * ai.hiwat;
   3764 	buf = (char *)malloc(buflen);
   3765 	REQUIRED_IF(buf != NULL);
   3766 	memset(buf, 0xff, buflen);
   3767 	do {
   3768 		r = WRITE(fd, buf, buflen);
   3769 	} while (r == buflen);
   3770 	if (r == -1) {
   3771 		XP_SYS_NG(EAGAIN, r);
   3772 	}
   3773 
   3774 	kq = KQUEUE();
   3775 	XP_SYS_OK(kq);
   3776 
   3777 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3778 	r = KEVENT_SET(kq, &kev, 1);
   3779 	XP_SYS_EQ(0, r);
   3780 
   3781 	/* Should not raise because it's not possible to write */
   3782 	ts.tv_sec = 0;
   3783 	ts.tv_nsec = 100L * 1000 * 1000;	/* 100msec */
   3784 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3785 	if (r > 0)
   3786 		DEBUG_KEV("kev", &kev);
   3787 	XP_SYS_EQ(0, r);
   3788 
   3789 	r = CLOSE(fd);
   3790 	XP_SYS_EQ(0, r);
   3791 	r = CLOSE(kq);
   3792 	XP_SYS_EQ(0, r);
   3793 	free(buf);
   3794 }
   3795 
   3796 /*
   3797  * Unpause from buffer full, kevent() should raise.
   3798  */
   3799 DEF(kqueue_unpause)
   3800 {
   3801 	struct audio_info ai;
   3802 	struct kevent kev;
   3803 	struct timespec ts;
   3804 	int fd;
   3805 	int r;
   3806 	int kq;
   3807 	char *buf;
   3808 	int buflen;
   3809 	u_int blocksize;
   3810 	int hiwat;
   3811 	int lowat;
   3812 
   3813 	TEST("kqueue_unpause");
   3814 
   3815 	/* Non-blocking open */
   3816 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3817 	REQUIRED_SYS_OK(fd);
   3818 
   3819 	/* Adjust block size and hiwat/lowat to make the test time 1sec */
   3820 	blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3821 	hiwat = 12;		/* 1.5sec */
   3822 	lowat = 4;		/* 0.5sec */
   3823 	AUDIO_INITINFO(&ai);
   3824 	ai.blocksize = blocksize;
   3825 	ai.hiwat = hiwat;
   3826 	ai.lowat = lowat;
   3827 	/* and also set encoding */
   3828 	/*
   3829 	 * XXX NetBSD7 has different results depending on whether the input
   3830 	 * encoding is emulated (AUDIO_ENCODINGFLAG_EMULATED) or not.  It's
   3831 	 * not easy to ensure this situation on all hardware environment.
   3832 	 * On NetBSD9, the result is the same regardless of input encoding.
   3833 	 */
   3834 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "blocksize=%d", blocksize);
   3835 	XP_SYS_EQ(0, r);
   3836 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3837 	if (ai.blocksize != blocksize) {
   3838 		/*
   3839 		 * NetBSD9 can not change the blocksize.  Then,
   3840 		 * adjust using hiwat/lowat.
   3841 		 */
   3842 		blocksize = ai.blocksize;
   3843 		hiwat = howmany(8000 * 1.5, blocksize);
   3844 		lowat = howmany(8000 * 0.5, blocksize);
   3845 	}
   3846 	/* Anyway, set the parameters */
   3847 	AUDIO_INITINFO(&ai);
   3848 	ai.blocksize = blocksize;
   3849 	ai.hiwat = hiwat;
   3850 	ai.lowat = lowat;
   3851 	ai.play.pause = 1;
   3852 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   3853 	XP_SYS_EQ(0, r);
   3854 
   3855 	/* Get the set parameters again */
   3856 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3857 	XP_SYS_EQ(0, r);
   3858 	DPRINTF("  > blocksize=%d hiwat=%d lowat=%d buffer_size=%d\n",
   3859 	    ai.blocksize, ai.hiwat, ai.lowat, ai.play.buffer_size);
   3860 
   3861 	/* Write until full */
   3862 	buflen = ai.blocksize * ai.hiwat;
   3863 	buf = (char *)malloc(buflen);
   3864 	REQUIRED_IF(buf != NULL);
   3865 	memset(buf, 0xff, buflen);
   3866 	do {
   3867 		r = WRITE(fd, buf, buflen);
   3868 	} while (r == buflen);
   3869 	if (r == -1) {
   3870 		XP_SYS_NG(EAGAIN, r);
   3871 	}
   3872 
   3873 	kq = KQUEUE();
   3874 	XP_SYS_OK(kq);
   3875 
   3876 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3877 	r = KEVENT_SET(kq, &kev, 1);
   3878 	XP_SYS_EQ(0, r);
   3879 
   3880 	/* Unpause */
   3881 	AUDIO_INITINFO(&ai);
   3882 	ai.play.pause = 0;
   3883 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=0");
   3884 	XP_SYS_EQ(0, r);
   3885 
   3886 	/* Check kevent() up to 2sec */
   3887 	ts.tv_sec = 2;
   3888 	ts.tv_nsec = 0;
   3889 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3890 	if (r >= 1)
   3891 		DEBUG_KEV("kev", &kev);
   3892 	if (netbsd < 8) {
   3893 		/*
   3894 		 * NetBSD7 with EMULATED_FLAG unset has bugs.  Unpausing
   3895 		 * unintentionally clears buffer (and therefore it becomes
   3896 		 * writable) but it doesn't raise EVFILT_WRITE.
   3897 		 */
   3898 	} else {
   3899 		XP_SYS_EQ(1, r);
   3900 	}
   3901 
   3902 	/* Flush it because there is no need to play it */
   3903 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   3904 	XP_SYS_EQ(0, r);
   3905 
   3906 	r = CLOSE(fd);
   3907 	XP_SYS_EQ(0, r);
   3908 	r = CLOSE(kq);
   3909 	XP_SYS_EQ(0, r);
   3910 	free(buf);
   3911 }
   3912 
   3913 /*
   3914  * kevent(2) must not be affected by other audio descriptors.
   3915  */
   3916 DEF(kqueue_simul)
   3917 {
   3918 	struct audio_info ai;
   3919 	struct audio_info ai2;
   3920 	struct kevent kev[2];
   3921 	struct timespec ts;
   3922 	int fd[2];
   3923 	int r;
   3924 	int kq;
   3925 	u_int blocksize;
   3926 	int hiwat;
   3927 	int lowat;
   3928 	char *buf;
   3929 	int buflen;
   3930 
   3931 	TEST("kqueue_simul");
   3932 	if (netbsd < 8) {
   3933 		XP_SKIP("Multiple open is not supported");
   3934 		return;
   3935 	}
   3936 
   3937 	memset(&ts, 0, sizeof(ts));
   3938 
   3939 	/* Make sure that it's not affected by descriptor order */
   3940 	for (int i = 0; i < 2; i++) {
   3941 		int a = i;
   3942 		int b = 1 - i;
   3943 
   3944 		fd[0] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3945 		REQUIRED_SYS_OK(fd[0]);
   3946 		fd[1] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3947 		REQUIRED_SYS_OK(fd[1]);
   3948 
   3949 		/*
   3950 		 * Adjust block size and hiwat/lowat.
   3951 		 * I want to choice suitable blocksize (if possible).
   3952 		 */
   3953 		blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3954 		hiwat = 12;		/* 1.5sec */
   3955 		lowat = 4;		/* 0.5sec */
   3956 		AUDIO_INITINFO(&ai);
   3957 		ai.blocksize = blocksize;
   3958 		ai.hiwat = hiwat;
   3959 		ai.lowat = lowat;
   3960 		r = IOCTL(fd[0], AUDIO_SETINFO, &ai, "blocksize=1000");
   3961 		XP_SYS_EQ(0, r);
   3962 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "read back blocksize");
   3963 		if (ai.blocksize != blocksize) {
   3964 			/*
   3965 			 * NetBSD9 can not change the blocksize.  Then,
   3966 			 * adjust using hiwat/lowat.
   3967 			 */
   3968 			blocksize = ai.blocksize;
   3969 			hiwat = howmany(8000 * 1.5, blocksize);
   3970 			lowat = howmany(8000 * 0.5, blocksize);
   3971 		}
   3972 		/* Anyway, set the parameters to both */
   3973 		AUDIO_INITINFO(&ai);
   3974 		ai.blocksize = blocksize;
   3975 		ai.hiwat = hiwat;
   3976 		ai.lowat = lowat;
   3977 		ai.play.pause = 1;
   3978 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=1");
   3979 		XP_SYS_EQ(0, r);
   3980 		r = IOCTL(fd[b], AUDIO_SETINFO, &ai, "pause=1");
   3981 		XP_SYS_EQ(0, r);
   3982 
   3983 		/* Write both until full */
   3984 		buflen = ai.blocksize * ai.hiwat;
   3985 		buf = (char *)malloc(buflen);
   3986 		REQUIRED_IF(buf != NULL);
   3987 		memset(buf, 0xff, buflen);
   3988 		/* Write fdA */
   3989 		do {
   3990 			r = WRITE(fd[a], buf, buflen);
   3991 		} while (r == buflen);
   3992 		if (r == -1) {
   3993 			XP_SYS_NG(EAGAIN, r);
   3994 		}
   3995 		/* Write fdB */
   3996 		do {
   3997 			r = WRITE(fd[b], buf, buflen);
   3998 		} while (r == buflen);
   3999 		if (r == -1) {
   4000 			XP_SYS_NG(EAGAIN, r);
   4001 		}
   4002 
   4003 		/* Get fdB's initial seek for later */
   4004 		r = IOCTL(fd[b], AUDIO_GETBUFINFO, &ai2, "");
   4005 		XP_SYS_EQ(0, r);
   4006 
   4007 		kq = KQUEUE();
   4008 		XP_SYS_OK(kq);
   4009 
   4010 		/* Both aren't raised at this point */
   4011 		EV_SET(&kev[0], fd[a], EV_ADD, EVFILT_WRITE, 0, 0, 0);
   4012 		EV_SET(&kev[1], fd[b], EV_ADD, EVFILT_WRITE, 0, 0, 0);
   4013 		r = KEVENT_SET(kq, kev, 2);
   4014 		XP_SYS_EQ(0, r);
   4015 
   4016 		/* Unpause only fdA */
   4017 		AUDIO_INITINFO(&ai);
   4018 		ai.play.pause = 0;
   4019 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=0");
   4020 		XP_SYS_EQ(0, r);
   4021 
   4022 		/* kevent() up to 2sec */
   4023 		ts.tv_sec = 2;
   4024 		ts.tv_nsec = 0;
   4025 		r = KEVENT_POLL(kq, &kev[0], 1, &ts);
   4026 		if (r >= 1)
   4027 			DEBUG_KEV("kev", &kev[0]);
   4028 		/* fdA should raise */
   4029 		XP_SYS_EQ(1, r);
   4030 		XP_EQ(fd[a], kev[0].ident);
   4031 
   4032 		/* Make sure that fdB keeps whole data */
   4033 		r = IOCTL(fd[b], AUDIO_GETBUFINFO, &ai, "");
   4034 		XP_EQ(ai2.play.seek, ai.play.seek);
   4035 
   4036 		/* Flush it because there is no need to play it */
   4037 		r = IOCTL(fd[0], AUDIO_FLUSH, NULL, "");
   4038 		XP_SYS_EQ(0, r);
   4039 		r = IOCTL(fd[1], AUDIO_FLUSH, NULL, "");
   4040 		XP_SYS_EQ(0, r);
   4041 
   4042 		r = CLOSE(fd[0]);
   4043 		XP_SYS_EQ(0, r);
   4044 		r = CLOSE(fd[1]);
   4045 		XP_SYS_EQ(0, r);
   4046 		r = CLOSE(kq);
   4047 		XP_SYS_EQ(0, r);
   4048 		free(buf);
   4049 
   4050 		xxx_close_wait();
   4051 	}
   4052 }
   4053 
   4054 /* Shared data between threads for ioctl_while_write */
   4055 struct ioctl_while_write_data {
   4056 	int fd;
   4057 	struct timeval start;
   4058 	int terminated;
   4059 };
   4060 
   4061 /* Test thread for ioctl_while_write */
   4062 void *thread_ioctl_while_write(void *);
   4063 void *
   4064 thread_ioctl_while_write(void *arg)
   4065 {
   4066 	struct ioctl_while_write_data *data = arg;
   4067 	struct timeval now, res;
   4068 	struct audio_info ai;
   4069 	int r;
   4070 
   4071 	/* If 0.5 seconds have elapsed since writing, assume it's blocked */
   4072 	do {
   4073 		usleep(100);
   4074 		gettimeofday(&now, NULL);
   4075 		timersub(&now, &data->start, &res);
   4076 	} while (res.tv_usec < 500000);
   4077 
   4078 	/* Then, do ioctl() */
   4079 	r = IOCTL(data->fd, AUDIO_GETBUFINFO, &ai, "");
   4080 	XP_SYS_EQ(0, r);
   4081 
   4082 	/* Terminate */
   4083 	data->terminated = 1;
   4084 
   4085 	/* Resume write() by unpause */
   4086 	AUDIO_INITINFO(&ai);
   4087 	if (netbsd < 8) {
   4088 		/*
   4089 		 * XXX NetBSD7 has bugs and it cannot be unpaused.
   4090 		 * However, it also has another bug and it clears buffer
   4091 		 * when encoding is changed.  I use it. :-P
   4092 		 */
   4093 		ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   4094 	}
   4095 	ai.play.pause = 0;
   4096 	r = IOCTL(data->fd, AUDIO_SETINFO, &ai, "pause=0");
   4097 	XP_SYS_EQ(0, r);
   4098 
   4099 	return NULL;
   4100 }
   4101 
   4102 /*
   4103  * ioctl(2) can be issued while write(2)-ing.
   4104  */
   4105 DEF(ioctl_while_write)
   4106 {
   4107 	struct audio_info ai;
   4108 	struct ioctl_while_write_data data0, *data;
   4109 	char buf[8000];	/* 1sec in mulaw,1ch,8000Hz */
   4110 	pthread_t tid;
   4111 	int r;
   4112 
   4113 	TEST("ioctl_while_write");
   4114 
   4115 	data = &data0;
   4116 	memset(data, 0, sizeof(*data));
   4117 	memset(buf, 0xff, sizeof(buf));
   4118 
   4119 	data->fd = OPEN(devaudio, O_WRONLY);
   4120 	REQUIRED_SYS_OK(data->fd);
   4121 
   4122 	/* Pause to block write(2)ing */
   4123 	AUDIO_INITINFO(&ai);
   4124 	ai.play.pause = 1;
   4125 	r = IOCTL(data->fd, AUDIO_SETINFO, &ai, "pause=1");
   4126 	XP_SYS_EQ(0, r);
   4127 
   4128 	gettimeofday(&data->start, NULL);
   4129 
   4130 	pthread_create(&tid, NULL, thread_ioctl_while_write, data);
   4131 
   4132 	/* Write until blocking */
   4133 	for (;;) {
   4134 		r = WRITE(data->fd, buf, sizeof(buf));
   4135 		if (data->terminated)
   4136 			break;
   4137 		XP_SYS_EQ(sizeof(buf), r);
   4138 
   4139 		/* Update written time */
   4140 		gettimeofday(&data->start, NULL);
   4141 	}
   4142 
   4143 	pthread_join(tid, NULL);
   4144 
   4145 	/* Flush */
   4146 	r = IOCTL(data->fd, AUDIO_FLUSH, NULL, "");
   4147 	XP_SYS_EQ(0, r);
   4148 	r = CLOSE(data->fd);
   4149 	XP_SYS_EQ(0, r);
   4150 }
   4151 
   4152 volatile int sigio_caught;
   4153 void
   4154 signal_FIOASYNC(int signo)
   4155 {
   4156 	if (signo == SIGIO) {
   4157 		sigio_caught = 1;
   4158 		DPRINTF("  > %d: pid %d got SIGIO\n", __LINE__, (int)getpid());
   4159 	}
   4160 }
   4161 
   4162 /*
   4163  * FIOASYNC between two descriptors should be splitted.
   4164  */
   4165 DEF(FIOASYNC_reset)
   4166 {
   4167 	int fd0, fd1;
   4168 	int r;
   4169 	int val;
   4170 
   4171 	TEST("FIOASYNC_reset");
   4172 	if (netbsd < 8) {
   4173 		XP_SKIP("Multiple open is not supported");
   4174 		return;
   4175 	}
   4176 
   4177 	/* The first one opens */
   4178 	fd0 = OPEN(devaudio, O_WRONLY);
   4179 	REQUIRED_SYS_OK(fd0);
   4180 
   4181 	/* The second one opens, enables ASYNC, and closes */
   4182 	fd1 = OPEN(devaudio, O_WRONLY);
   4183 	REQUIRED_SYS_OK(fd1);
   4184 	val = 1;
   4185 	r = IOCTL(fd1, FIOASYNC, &val, "on");
   4186 	XP_SYS_EQ(0, r);
   4187 	r = CLOSE(fd1);
   4188 	XP_SYS_EQ(0, r);
   4189 
   4190 	/* Again, the second one opens and enables ASYNC */
   4191 	fd1 = OPEN(devaudio, O_WRONLY);
   4192 	REQUIRED_SYS_OK(fd1);
   4193 	val = 1;
   4194 	r = IOCTL(fd1, FIOASYNC, &val, "on");
   4195 	XP_SYS_EQ(0, r);	/* NetBSD8 fails */
   4196 	r = CLOSE(fd1);
   4197 	XP_SYS_EQ(0, r);
   4198 	r = CLOSE(fd0);
   4199 	XP_SYS_EQ(0, r);
   4200 }
   4201 
   4202 /*
   4203  * Whether SIGIO is emitted on plyaback.
   4204  * XXX I don't understand conditions that NetBSD7 emits signal.
   4205  */
   4206 DEF(FIOASYNC_play_signal)
   4207 {
   4208 	struct audio_info ai;
   4209 	int r;
   4210 	int fd;
   4211 	int val;
   4212 	char *data;
   4213 	int i;
   4214 
   4215 	TEST("FIOASYNC_play_signal");
   4216 	if (hw_canplay() == 0) {
   4217 		XP_SKIP("This test is only for playable device");
   4218 		return;
   4219 	}
   4220 
   4221 	signal(SIGIO, signal_FIOASYNC);
   4222 	sigio_caught = 0;
   4223 
   4224 	fd = OPEN(devaudio, O_WRONLY);
   4225 	REQUIRED_SYS_OK(fd);
   4226 
   4227 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4228 	REQUIRED_SYS_EQ(0, r);
   4229 	REQUIRED_IF(ai.blocksize != 0);
   4230 	data = (char *)malloc(ai.blocksize);
   4231 	REQUIRED_IF(data != NULL);
   4232 	memset(data, 0xff, ai.blocksize);
   4233 
   4234 	val = 1;
   4235 	r = IOCTL(fd, FIOASYNC, &val, "on");
   4236 	XP_SYS_EQ(0, r);
   4237 
   4238 	r = WRITE(fd, data, ai.blocksize);
   4239 	XP_SYS_EQ(ai.blocksize, r);
   4240 
   4241 	/* Waits signal until 1sec */
   4242 	for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4243 		usleep(10000);
   4244 	}
   4245 	signal(SIGIO, SIG_IGN);
   4246 	XP_EQ(1, sigio_caught);
   4247 
   4248 	r = CLOSE(fd);
   4249 	XP_SYS_EQ(0, r);
   4250 
   4251 	free(data);
   4252 	signal(SIGIO, SIG_IGN);
   4253 	sigio_caught = 0;
   4254 }
   4255 
   4256 /*
   4257  * Whether SIGIO is emitted on recording.
   4258  */
   4259 DEF(FIOASYNC_rec_signal)
   4260 {
   4261 	char buf[10];
   4262 	int r;
   4263 	int fd;
   4264 	int val;
   4265 	int i;
   4266 
   4267 	TEST("FIOASYNC_rec_signal");
   4268 	if (hw_canrec() == 0) {
   4269 		XP_SKIP("This test is only for recordable device");
   4270 		return;
   4271 	}
   4272 
   4273 	signal(SIGIO, signal_FIOASYNC);
   4274 	sigio_caught = 0;
   4275 
   4276 	fd = OPEN(devaudio, O_RDONLY);
   4277 	REQUIRED_SYS_OK(fd);
   4278 
   4279 	val = 1;
   4280 	r = IOCTL(fd, FIOASYNC, &val, "on");
   4281 	XP_SYS_EQ(0, r);
   4282 
   4283 	r = READ(fd, buf, sizeof(buf));
   4284 	XP_SYS_EQ(sizeof(buf), r);
   4285 
   4286 	/* Wait signal until 1sec */
   4287 	for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4288 		usleep(10000);
   4289 	}
   4290 	signal(SIGIO, SIG_IGN);
   4291 	XP_EQ(1, sigio_caught);
   4292 
   4293 	r = CLOSE(fd);
   4294 	XP_SYS_EQ(0, r);
   4295 
   4296 	signal(SIGIO, SIG_IGN);
   4297 	sigio_caught = 0;
   4298 }
   4299 
   4300 /*
   4301  * FIOASYNC doesn't affect other descriptor.
   4302  * For simplify, test only for playback...
   4303  */
   4304 DEF(FIOASYNC_multi)
   4305 {
   4306 	struct audio_info ai;
   4307 	char *buf;
   4308 	char pipebuf[1];
   4309 	int r;
   4310 	int i;
   4311 	int fd1;
   4312 	int fd2;
   4313 	int pd[2];
   4314 	int val;
   4315 	pid_t pid;
   4316 	int status;
   4317 
   4318 	TEST("FIOASYNC_multi");
   4319 	if (netbsd < 8) {
   4320 		XP_SKIP("Multiple open is not supported");
   4321 		return;
   4322 	}
   4323 	if (hw_canplay() == 0) {
   4324 		XP_SKIP("This test is only for playable device");
   4325 		return;
   4326 	}
   4327 
   4328 	/* Pipe used between parent and child */
   4329 	r = pipe(pd);
   4330 	REQUIRED_SYS_EQ(0, r);
   4331 
   4332 	fd1 = OPEN(devaudio, O_WRONLY);
   4333 	REQUIRED_SYS_OK(fd1);
   4334 	fd2 = OPEN(devaudio, O_WRONLY);
   4335 	REQUIRED_SYS_OK(fd2);
   4336 
   4337 	/* Pause fd2 */
   4338 	AUDIO_INITINFO(&ai);
   4339 	ai.play.pause = 1;
   4340 	r = IOCTL(fd2, AUDIO_SETINFO, &ai, "pause");
   4341 	REQUIRED_SYS_EQ(0, r);
   4342 
   4343 	/* Fill both */
   4344 	r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   4345 	REQUIRED_SYS_EQ(0, r);
   4346 	REQUIRED_IF(ai.blocksize != 0);
   4347 	buf = (char *)malloc(ai.blocksize);
   4348 	REQUIRED_IF(buf != NULL);
   4349 	memset(buf, 0xff, ai.blocksize);
   4350 	r = WRITE(fd1, buf, ai.blocksize);
   4351 	XP_SYS_EQ(ai.blocksize, r);
   4352 
   4353 	sigio_caught = 0;
   4354 	val = 1;
   4355 
   4356 	fflush(stdout);
   4357 	fflush(stderr);
   4358 	pid = fork();
   4359 	if (pid == -1) {
   4360 		REQUIRED_SYS_OK(pid);
   4361 	}
   4362 	if (pid == 0) {
   4363 		/* Child */
   4364 		close(fd1);
   4365 
   4366 		/* Child enables ASYNC on fd2 */
   4367 		signal(SIGIO, signal_FIOASYNC);
   4368 		r = IOCTL(fd2, FIOASYNC, &val, "on");
   4369 		/* It cannot count errors because here is a child process */
   4370 		/* XP_SYS_EQ(0, r); */
   4371 
   4372 		/*
   4373 		 * Waits signal until 1sec.
   4374 		 * But fd2 is paused so it should never raise.
   4375 		 */
   4376 		for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4377 			usleep(10000);
   4378 		}
   4379 		signal(SIGIO, SIG_IGN);
   4380 		pipebuf[0] = sigio_caught;
   4381 		/* This is not WRITE() macro here */
   4382 		write(pd[1], pipebuf, sizeof(pipebuf));
   4383 
   4384 		/* XXX? */
   4385 		close(fd2);
   4386 		sleep(1);
   4387 		exit(0);
   4388 	} else {
   4389 		/* Parent */
   4390 		DPRINTF("  > fork() = %d\n", (int)pid);
   4391 
   4392 		/* Parent enables ASYNC on fd1 */
   4393 		signal(SIGIO, signal_FIOASYNC);
   4394 		r = IOCTL(fd1, FIOASYNC, &val, "on");
   4395 		XP_SYS_EQ(0, r);
   4396 
   4397 		/* Waits signal until 1sec */
   4398 		for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4399 			usleep(10000);
   4400 		}
   4401 		signal(SIGIO, SIG_IGN);
   4402 		XP_EQ(1, sigio_caught);
   4403 
   4404 		/* Then read child's result from pipe */
   4405 		r = read(pd[0], pipebuf, sizeof(pipebuf));
   4406 		if (r != 1) {
   4407 			XP_FAIL("reading from child failed");
   4408 		}
   4409 		DPRINTF("  > child's sigio_cauht = %d\n", pipebuf[0]);
   4410 		XP_EQ(0, pipebuf[0]);
   4411 
   4412 		waitpid(pid, &status, 0);
   4413 	}
   4414 
   4415 	r = CLOSE(fd1);
   4416 	XP_SYS_EQ(0, r);
   4417 	r = CLOSE(fd2);
   4418 	XP_SYS_EQ(0, r);
   4419 
   4420 	signal(SIGIO, SIG_IGN);
   4421 	sigio_caught = 0;
   4422 	free(buf);
   4423 }
   4424 
   4425 /*
   4426  * Check AUDIO_WSEEK behavior.
   4427  */
   4428 DEF(AUDIO_WSEEK)
   4429 {
   4430 	char buf[4];
   4431 	struct audio_info ai;
   4432 	int r;
   4433 	int fd;
   4434 	int n;
   4435 
   4436 	TEST("AUDIO_WSEEK");
   4437 
   4438 	fd = OPEN(devaudio, O_WRONLY);
   4439 	REQUIRED_SYS_OK(fd);
   4440 
   4441 	/* Pause to count sample data */
   4442 	AUDIO_INITINFO(&ai);
   4443 	ai.play.pause = 1;
   4444 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   4445 	REQUIRED_SYS_EQ(0, r);
   4446 
   4447 	/* On the initial state, it should be 0 bytes */
   4448 	n = 0;
   4449 	r = IOCTL(fd, AUDIO_WSEEK, &n, "");
   4450 	XP_SYS_EQ(0, r);
   4451 	XP_EQ(0, n);
   4452 
   4453 	/* When writing 4 bytes, it should be 4 bytes */
   4454 	memset(buf, 0xff, sizeof(buf));
   4455 	r = WRITE(fd, buf, sizeof(buf));
   4456 	REQUIRED_EQ(sizeof(buf), r);
   4457 	r = IOCTL(fd, AUDIO_WSEEK, &n, "");
   4458 	XP_SYS_EQ(0, r);
   4459 	if (netbsd < 9) {
   4460 		/*
   4461 		 * On NetBSD7, it will return 0.
   4462 		 * Perhaps, WSEEK returns the number of pustream bytes but
   4463 		 * data has already advanced...
   4464 		 */
   4465 		XP_EQ(0, n);
   4466 	} else {
   4467 		/* Data less than one block remains here */
   4468 		XP_EQ(4, n);
   4469 	}
   4470 
   4471 	r = CLOSE(fd);
   4472 	XP_SYS_EQ(0, r);
   4473 }
   4474 
   4475 /*
   4476  * Check AUDIO_SETFD behavior for O_*ONLY descriptor.
   4477  * On NetBSD7, SETFD modify audio layer's state (and MD driver's state)
   4478  * regardless of open mode.  GETFD obtains audio layer's duplex.
   4479  * On NetBSD9, SETFD is obsoleted.  GETFD obtains hardware's duplex.
   4480  */
   4481 void
   4482 test_AUDIO_SETFD_xxONLY(int openmode)
   4483 {
   4484 	struct audio_info ai;
   4485 	int r;
   4486 	int fd;
   4487 	int n;
   4488 
   4489 	TEST("AUDIO_SETFD_%s", openmode_str[openmode] + 2);
   4490 	if (openmode == O_RDONLY && hw_canrec() == 0) {
   4491 		XP_SKIP("This test is for recordable device");
   4492 		return;
   4493 	}
   4494 	if (openmode == O_WRONLY && hw_canplay() == 0) {
   4495 		XP_SKIP("This test is for playable device");
   4496 		return;
   4497 	}
   4498 
   4499 	fd = OPEN(devaudio, openmode);
   4500 	REQUIRED_SYS_OK(fd);
   4501 
   4502 	/*
   4503 	 * Just after open(2),
   4504 	 * - On NetBSD7, it's always half-duplex.
   4505 	 * - On NetBSD9, it's the same as hardware one regardless of openmode.
   4506 	 */
   4507 	n = 0;
   4508 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4509 	XP_SYS_EQ(0, r);
   4510 	if (netbsd < 9) {
   4511 		XP_EQ(0, n);
   4512 	} else {
   4513 		XP_EQ(hw_fulldup(), n);
   4514 	}
   4515 
   4516 	/*
   4517 	 * When trying to set to full-duplex,
   4518 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4519 	 *   will fail if the hardware is half-duplex.
   4520 	 * - On NetBSD9, it will always succeed but will not be modified.
   4521 	 */
   4522 	n = 1;
   4523 	r = IOCTL(fd, AUDIO_SETFD, &n, "on");
   4524 	if (netbsd < 8) {
   4525 		if (hw_fulldup()) {
   4526 			XP_SYS_EQ(0, r);
   4527 		} else {
   4528 			XP_SYS_NG(ENOTTY, r);
   4529 		}
   4530 	} else if (netbsd == 8) {
   4531 		XP_FAIL("expected result is unknown");
   4532 	} else {
   4533 		XP_SYS_EQ(0, r);
   4534 	}
   4535 
   4536 	/*
   4537 	 * When obtain it,
   4538 	 * - On NetBSD7, it will be 1 if the hardware is full-duplex or
   4539 	 *   0 if half-duplex.
   4540 	 * - On NetBSD9, it will never be changed because it's the hardware
   4541 	 *   property.
   4542 	 */
   4543 	n = 0;
   4544 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4545 	XP_SYS_EQ(0, r);
   4546 	if (netbsd < 8) {
   4547 		XP_EQ(hw_fulldup(), n);
   4548 	} else if (netbsd == 8) {
   4549 		XP_FAIL("expected result is unknown");
   4550 	} else {
   4551 		XP_EQ(hw_fulldup(), n);
   4552 	}
   4553 
   4554 	/* Some track parameters like ai.*.open should not change */
   4555 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4556 	XP_SYS_EQ(0, r);
   4557 	XP_EQ(mode2play(openmode), ai.play.open);
   4558 	XP_EQ(mode2rec(openmode), ai.record.open);
   4559 
   4560 	/*
   4561 	 * When trying to set to half-duplex,
   4562 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4563 	 *   it will succeed with nothing happens.
   4564 	 * - On NetBSD9, it will always succeed but nothing happens.
   4565 	 */
   4566 	n = 0;
   4567 	r = IOCTL(fd, AUDIO_SETFD, &n, "off");
   4568 	XP_SYS_EQ(0, r);
   4569 
   4570 	/*
   4571 	 * When obtain it again,
   4572 	 * - On NetBSD7, it will be 0 if the hardware is full-duplex, or
   4573 	 *   still 0 if half-duplex.
   4574 	 * - On NetBSD9, it should not change.
   4575 	 */
   4576 	n = 0;
   4577 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4578 	XP_SYS_EQ(0, r);
   4579 	if (netbsd < 9) {
   4580 		XP_EQ(0, n);
   4581 	} else {
   4582 		XP_EQ(hw_fulldup(), n);
   4583 	}
   4584 
   4585 	/* Some track parameters like ai.*.open should not change */
   4586 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4587 	XP_SYS_EQ(0, r);
   4588 	XP_EQ(mode2play(openmode), ai.play.open);
   4589 	XP_EQ(mode2rec(openmode), ai.record.open);
   4590 
   4591 	r = CLOSE(fd);
   4592 	XP_SYS_EQ(0, r);
   4593 }
   4594 DEF(AUDIO_SETFD_RDONLY)	{ test_AUDIO_SETFD_xxONLY(O_RDONLY); }
   4595 DEF(AUDIO_SETFD_WRONLY)	{ test_AUDIO_SETFD_xxONLY(O_WRONLY); }
   4596 
   4597 /*
   4598  * Check AUDIO_SETFD behavior for O_RDWR descriptor.
   4599  */
   4600 DEF(AUDIO_SETFD_RDWR)
   4601 {
   4602 	struct audio_info ai;
   4603 	int r;
   4604 	int fd;
   4605 	int n;
   4606 
   4607 	TEST("AUDIO_SETFD_RDWR");
   4608 	if (!hw_fulldup()) {
   4609 		XP_SKIP("This test is only for full-duplex device");
   4610 		return;
   4611 	}
   4612 
   4613 	fd = OPEN(devaudio, O_RDWR);
   4614 	REQUIRED_SYS_OK(fd);
   4615 
   4616 	/*
   4617 	 * - audio(4) manpage until NetBSD7 said "If a full-duplex capable
   4618 	 *   audio device is opened for both reading and writing it will
   4619 	 *   start in half-duplex play mode", but implementation doesn't
   4620 	 *   seem to follow it.  It returns full-duplex.
   4621 	 * - On NetBSD9, it should return full-duplex on full-duplex, or
   4622 	 *   half-duplex on half-duplex.
   4623 	 */
   4624 	n = 0;
   4625 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4626 	XP_SYS_EQ(0, r);
   4627 	XP_EQ(hw_fulldup(), n);
   4628 
   4629 	/*
   4630 	 * When trying to set to full-duplex,
   4631 	 * - On NetBSD7, it will succeed with nothing happens if full-duplex,
   4632 	 *   or will fail if half-duplex.
   4633 	 * - On NetBSD9, it will always succeed with nothing happens.
   4634 	 */
   4635 	n = 1;
   4636 	r = IOCTL(fd, AUDIO_SETFD, &n, "on");
   4637 	if (netbsd < 9) {
   4638 		if (hw_fulldup()) {
   4639 			XP_SYS_EQ(0, r);
   4640 		} else {
   4641 			XP_SYS_NG(ENOTTY, r);
   4642 		}
   4643 	} else {
   4644 		XP_SYS_EQ(0, r);
   4645 	}
   4646 
   4647 	/* When obtains it, it retuns half/full-duplex as is */
   4648 	n = 0;
   4649 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4650 	XP_SYS_EQ(0, r);
   4651 	XP_EQ(hw_fulldup(), n);
   4652 
   4653 	/* Some track parameters like ai.*.open should not change */
   4654 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4655 	XP_SYS_EQ(0, r);
   4656 	XP_EQ(1, ai.play.open);
   4657 	XP_EQ(mode2rec(O_RDWR), ai.record.open);
   4658 
   4659 	/*
   4660 	 * When trying to set to half-duplex,
   4661 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4662 	 *   it will succeed with nothing happens.
   4663 	 * - On NetBSD9, it will always succeed but nothing happens.
   4664 	 */
   4665 	n = 0;
   4666 	r = IOCTL(fd, AUDIO_SETFD, &n, "off");
   4667 	if (netbsd < 8) {
   4668 		XP_SYS_EQ(0, r);
   4669 	} else if (netbsd == 8) {
   4670 		XP_FAIL("expected result is unknown");
   4671 	} else {
   4672 		XP_SYS_EQ(0, r);
   4673 	}
   4674 
   4675 	/*
   4676 	 * When obtain it again,
   4677 	 * - On NetBSD7, it will be 0 if the hardware is full-duplex, or
   4678 	 *   still 0 if half-duplex.
   4679 	 * - On NetBSD9, it should be 1 if the hardware is full-duplex, or
   4680 	 *   0 if half-duplex.
   4681 	 */
   4682 	n = 0;
   4683 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4684 	XP_SYS_EQ(0, r);
   4685 	if (netbsd < 8) {
   4686 		XP_EQ(0, n);
   4687 	} else if (netbsd == 8) {
   4688 		XP_FAIL("expected result is unknown");
   4689 	} else {
   4690 		XP_EQ(hw_fulldup(), n);
   4691 	}
   4692 
   4693 	/* Some track parameters like ai.*.open should not change */
   4694 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4695 	XP_SYS_EQ(0, r);
   4696 	XP_EQ(1, ai.play.open);
   4697 	XP_EQ(mode2rec(O_RDWR), ai.record.open);
   4698 
   4699 	r = CLOSE(fd);
   4700 	XP_SYS_EQ(0, r);
   4701 }
   4702 
   4703 /*
   4704  * Check AUDIO_GETINFO.eof behavior.
   4705  */
   4706 DEF(AUDIO_GETINFO_eof)
   4707 {
   4708 	struct audio_info ai;
   4709 	char buf[4];
   4710 	int r;
   4711 	int fd, fd1;
   4712 
   4713 	TEST("AUDIO_GETINFO_eof");
   4714 	if (hw_canplay() == 0) {
   4715 		XP_SKIP("This test is for playable device");
   4716 		return;
   4717 	}
   4718 
   4719 	fd = OPEN(devaudio, O_RDWR);
   4720 	REQUIRED_SYS_OK(fd);
   4721 
   4722 	/* Pause to make no sound */
   4723 	AUDIO_INITINFO(&ai);
   4724 	ai.play.pause = 1;
   4725 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   4726 	REQUIRED_SYS_EQ(0, r);
   4727 
   4728 	/* It should be 0 initially */
   4729 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4730 	XP_SYS_EQ(0, r);
   4731 	XP_EQ(0, ai.play.eof);
   4732 	XP_EQ(0, ai.record.eof);
   4733 
   4734 	/* Writing zero bytes should increment it */
   4735 	r = WRITE(fd, &r, 0);
   4736 	REQUIRED_SYS_OK(r);
   4737 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4738 	XP_SYS_EQ(0, r);
   4739 	XP_EQ(1, ai.play.eof);
   4740 	XP_EQ(0, ai.record.eof);
   4741 
   4742 	/* Writing one ore more bytes should noto increment it */
   4743 	memset(buf, 0xff, sizeof(buf));
   4744 	r = WRITE(fd, buf, sizeof(buf));
   4745 	REQUIRED_SYS_OK(r);
   4746 	memset(&ai, 0, sizeof(ai));
   4747 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4748 	XP_SYS_EQ(0, r);
   4749 	XP_EQ(1, ai.play.eof);
   4750 	XP_EQ(0, ai.record.eof);
   4751 
   4752 	/* Writing zero bytes again should increment it */
   4753 	r = WRITE(fd, buf, 0);
   4754 	REQUIRED_SYS_OK(r);
   4755 	memset(&ai, 0, sizeof(ai));
   4756 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4757 	XP_SYS_EQ(0, r);
   4758 	XP_EQ(2, ai.play.eof);
   4759 	XP_EQ(0, ai.record.eof);
   4760 
   4761 	/* Reading zero bytes should not increment it */
   4762 	if (hw_fulldup()) {
   4763 		r = READ(fd, buf, 0);
   4764 		REQUIRED_SYS_OK(r);
   4765 		memset(&ai, 0, sizeof(ai));
   4766 		r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4767 		XP_SYS_EQ(0, r);
   4768 		XP_EQ(2, ai.play.eof);
   4769 		XP_EQ(0, ai.record.eof);
   4770 	}
   4771 
   4772 	/* should not interfere with other descriptor */
   4773 	if (netbsd >= 8) {
   4774 		fd1 = OPEN(devaudio, O_RDWR);
   4775 		REQUIRED_SYS_OK(fd1);
   4776 		memset(&ai, 0, sizeof(ai));
   4777 		r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   4778 		XP_SYS_EQ(0, r);
   4779 		XP_EQ(0, ai.play.eof);
   4780 		XP_EQ(0, ai.record.eof);
   4781 		r = CLOSE(fd1);
   4782 		XP_SYS_EQ(0, r);
   4783 	}
   4784 
   4785 	r = CLOSE(fd);
   4786 	XP_SYS_EQ(0, r);
   4787 
   4788 	xxx_close_wait();
   4789 
   4790 	/* When reopen, it should reset the counter */
   4791 	fd = OPEN(devaudio, O_RDWR);
   4792 	REQUIRED_SYS_OK(fd);
   4793 
   4794 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4795 	XP_SYS_EQ(0, r);
   4796 	XP_EQ(0, ai.play.eof);
   4797 	XP_EQ(0, ai.record.eof);
   4798 
   4799 	r = CLOSE(fd);
   4800 	XP_SYS_EQ(0, r);
   4801 }
   4802 
   4803 /*
   4804  * Check relationship between openmode and mode set by AUDIO_SETINFO.
   4805  */
   4806 void
   4807 test_AUDIO_SETINFO_mode(int openmode, int index, int setmode, int expected)
   4808 {
   4809 	struct audio_info ai;
   4810 	char buf[10];
   4811 	int inimode;
   4812 	int r;
   4813 	int fd;
   4814 	bool canwrite;
   4815 	bool canread;
   4816 
   4817 	/* index was passed only for displaying here */
   4818 	TEST("AUDIO_SETINFO_mode_%s_%d", openmode_str[openmode] + 2, index);
   4819 	if (mode2aumode(openmode) == 0) {
   4820 		XP_SKIP("Operation not allowed on this hardware property");
   4821 		return;
   4822 	}
   4823 
   4824 	inimode = mode2aumode(openmode);
   4825 
   4826 	fd = OPEN(devaudio, openmode);
   4827 	REQUIRED_SYS_OK(fd);
   4828 
   4829 	/* When just after opening */
   4830 	memset(&ai, 0, sizeof(ai));
   4831 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   4832 	REQUIRED_SYS_EQ(0, r);
   4833 	XP_EQ(inimode, ai.mode);
   4834 	XP_EQ(mode2play(openmode), ai.play.open);
   4835 	XP_EQ(mode2rec(openmode),  ai.record.open);
   4836 	XP_NE(0, ai.play.buffer_size);
   4837 	XP_NE(0, ai.record.buffer_size);
   4838 
   4839 	/* Change mode (and pause here) */
   4840 	ai.mode = setmode;
   4841 	ai.play.pause = 1;
   4842 	ai.record.pause = 1;
   4843 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "mode");
   4844 	XP_SYS_EQ(0, r);
   4845 	if (r == 0) {
   4846 		r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   4847 		XP_SYS_EQ(0, r);
   4848 		XP_EQ(expected, ai.mode);
   4849 
   4850 		/* It seems to keep the initial openmode regardless of mode */
   4851 		XP_EQ(mode2play(openmode), ai.play.open);
   4852 		XP_EQ(mode2rec(openmode), ai.record.open);
   4853 		XP_NE(0, ai.play.buffer_size);
   4854 		XP_NE(0, ai.record.buffer_size);
   4855 	}
   4856 
   4857 	/*
   4858 	 * On NetBSD7, whether writable depends openmode when open.
   4859 	 * On NetBSD9, whether writable should depend inimode when open.
   4860 	 * Modifying after open should not affect this mode.
   4861 	 */
   4862 	if (netbsd < 9) {
   4863 		canwrite = (openmode != O_RDONLY);
   4864 	} else {
   4865 		canwrite = ((inimode & AUMODE_PLAY) != 0);
   4866 	}
   4867 	r = WRITE(fd, buf, 0);
   4868 	if (canwrite) {
   4869 		XP_SYS_EQ(0, r);
   4870 	} else {
   4871 		XP_SYS_NG(EBADF, r);
   4872 	}
   4873 
   4874 	/*
   4875 	 * On NetBSD7, whether readable depends openmode when open.
   4876 	 * On NetBSD9, whether readable should depend inimode when open.
   4877 	 * Modifying after open should not affect this mode.
   4878 	 */
   4879 	if (netbsd < 9) {
   4880 		canread = (openmode != O_WRONLY);
   4881 	} else {
   4882 		canread = ((inimode & AUMODE_RECORD) != 0);
   4883 	}
   4884 	r = READ(fd, buf, 0);
   4885 	if (canread) {
   4886 		XP_SYS_EQ(0, r);
   4887 	} else {
   4888 		XP_SYS_NG(EBADF, r);
   4889 	}
   4890 
   4891 	r = CLOSE(fd);
   4892 	XP_SYS_EQ(0, r);
   4893 }
   4894 /*
   4895  * XXX hmm... it's too complex
   4896  */
   4897 /* shortcut for table form */
   4898 #define P	AUMODE_PLAY
   4899 #define A	AUMODE_PLAY_ALL
   4900 #define R	AUMODE_RECORD
   4901 struct setinfo_mode_t {
   4902 	int setmode;	/* mode used in SETINFO */
   4903 	int expmode7;	/* expected mode on NetBSD7 */
   4904 	int expmode9;	/* expected mode on NetBSD9 */
   4905 };
   4906 /*
   4907  * The following tables show this operation on NetBSD7 is almost 'undefined'.
   4908  * In contrast, NetBSD9 never changes mode by AUDIO_SETINFO except
   4909  * AUMODE_PLAY_ALL.
   4910  *
   4911  * setmode == 0 and 8 are out of range and invalid input samples.
   4912  * But NetBSD7 seems to accept it as is.
   4913  */
   4914 struct setinfo_mode_t table_SETINFO_mode_O_RDONLY[] = {
   4915 	/* setmode	expmode7	expmode9 */
   4916 	{     0,	     0,		 R    },
   4917 	{     P,	     P,		 R    },
   4918 	{   A  ,	   A|P,		 R    },
   4919 	{   A|P,	   A|P,		 R    },
   4920 	{ R    ,	 R    ,		 R    },
   4921 	{ R|  P,	     P,		 R    },
   4922 	{ R|A  ,	   A|P,		 R    },
   4923 	{ R|A|P,	   A|P,		 R    },
   4924 	{     8,	     8,		 R    },
   4925 };
   4926 struct setinfo_mode_t table_SETINFO_mode_O_WRONLY[] = {
   4927 	/* setmode	expmode7	expmode9 */
   4928 	{     0,	     0,		     P },
   4929 	{     P,	     P,		     P },
   4930 	{   A  ,	   A|P,		   A|P },
   4931 	{   A|P,	   A|P,		   A|P },
   4932 	{ R    ,	 R    ,		     P },
   4933 	{ R|  P,	     P,		     P },
   4934 	{ R|A  ,	   A|P,		   A|P },
   4935 	{ R|A|P,	   A|P,		   A|P },
   4936 	{     8,	     8,		     P },
   4937 };
   4938 #define f(openmode, index)	do {					\
   4939 	struct setinfo_mode_t *table = table_SETINFO_mode_##openmode;	\
   4940 	int setmode = table[index].setmode;				\
   4941 	int expected = (netbsd < 9)					\
   4942 	    ? table[index].expmode7					\
   4943 	    : table[index].expmode9;					\
   4944 	test_AUDIO_SETINFO_mode(openmode, index, setmode, expected);	\
   4945 } while (0)
   4946 DEF(AUDIO_SETINFO_mode_RDONLY_0) { f(O_RDONLY, 0); }
   4947 DEF(AUDIO_SETINFO_mode_RDONLY_1) { f(O_RDONLY, 1); }
   4948 DEF(AUDIO_SETINFO_mode_RDONLY_2) { f(O_RDONLY, 2); }
   4949 DEF(AUDIO_SETINFO_mode_RDONLY_3) { f(O_RDONLY, 3); }
   4950 DEF(AUDIO_SETINFO_mode_RDONLY_4) { f(O_RDONLY, 4); }
   4951 DEF(AUDIO_SETINFO_mode_RDONLY_5) { f(O_RDONLY, 5); }
   4952 DEF(AUDIO_SETINFO_mode_RDONLY_6) { f(O_RDONLY, 6); }
   4953 DEF(AUDIO_SETINFO_mode_RDONLY_7) { f(O_RDONLY, 7); }
   4954 DEF(AUDIO_SETINFO_mode_RDONLY_8) { f(O_RDONLY, 8); }
   4955 DEF(AUDIO_SETINFO_mode_WRONLY_0) { f(O_WRONLY, 0); }
   4956 DEF(AUDIO_SETINFO_mode_WRONLY_1) { f(O_WRONLY, 1); }
   4957 DEF(AUDIO_SETINFO_mode_WRONLY_2) { f(O_WRONLY, 2); }
   4958 DEF(AUDIO_SETINFO_mode_WRONLY_3) { f(O_WRONLY, 3); }
   4959 DEF(AUDIO_SETINFO_mode_WRONLY_4) { f(O_WRONLY, 4); }
   4960 DEF(AUDIO_SETINFO_mode_WRONLY_5) { f(O_WRONLY, 5); }
   4961 DEF(AUDIO_SETINFO_mode_WRONLY_6) { f(O_WRONLY, 6); }
   4962 DEF(AUDIO_SETINFO_mode_WRONLY_7) { f(O_WRONLY, 7); }
   4963 DEF(AUDIO_SETINFO_mode_WRONLY_8) { f(O_WRONLY, 8); }
   4964 #undef f
   4965 /*
   4966  * The following tables also show that NetBSD7's behavior is almost
   4967  * 'undefined'.
   4968  */
   4969 struct setinfo_mode_t table_SETINFO_mode_O_RDWR_full[] = {
   4970 	/* setmode	expmode7	expmode9 */
   4971 	{     0,	    0,		R|  P },
   4972 	{     P,	    P,		R|  P },
   4973 	{   A  ,	  A|P,		R|A|P },
   4974 	{   A|P,	  A|P,		R|A|P },
   4975 	{ R    ,	R    ,		R|  P },
   4976 	{ R|  P,	R|  P,		R|  P },
   4977 	{ R|A  ,	R|A|P,		R|A|P },
   4978 	{ R|A|P,	R|A|P,		R|A|P },
   4979 	{     8,	    8,		R|  P },
   4980 };
   4981 struct setinfo_mode_t table_SETINFO_mode_O_RDWR_half[] = {
   4982 	/* setmode	expmode7	expmode9 */
   4983 	{     0,	    0,		    P },
   4984 	{     P,	    P,		    P },
   4985 	{   A  ,	  A|P,		  A|P },
   4986 	{   A|P,	  A|P,		  A|P },
   4987 	{ R    ,	R    ,		    P },
   4988 	{ R|  P,	    P,		    P },
   4989 	{ R|A  ,	  A|P,		  A|P },
   4990 	{ R|A|P,	  A|P,		  A|P },
   4991 	{     8,	    8,		    P },
   4992 };
   4993 #define f(index)	do {						\
   4994 	struct setinfo_mode_t *table = (hw_fulldup())			\
   4995 	    ? table_SETINFO_mode_O_RDWR_full				\
   4996 	    : table_SETINFO_mode_O_RDWR_half;				\
   4997 	int setmode = table[index].setmode;				\
   4998 	int expected = (netbsd < 9)					\
   4999 	    ? table[index].expmode7					\
   5000 	    : table[index].expmode9;					\
   5001 	test_AUDIO_SETINFO_mode(O_RDWR, index, setmode, expected);	\
   5002 } while (0)
   5003 DEF(AUDIO_SETINFO_mode_RDWR_0) { f(0); }
   5004 DEF(AUDIO_SETINFO_mode_RDWR_1) { f(1); }
   5005 DEF(AUDIO_SETINFO_mode_RDWR_2) { f(2); }
   5006 DEF(AUDIO_SETINFO_mode_RDWR_3) { f(3); }
   5007 DEF(AUDIO_SETINFO_mode_RDWR_4) { f(4); }
   5008 DEF(AUDIO_SETINFO_mode_RDWR_5) { f(5); }
   5009 DEF(AUDIO_SETINFO_mode_RDWR_6) { f(6); }
   5010 DEF(AUDIO_SETINFO_mode_RDWR_7) { f(7); }
   5011 DEF(AUDIO_SETINFO_mode_RDWR_8) { f(8); }
   5012 #undef f
   5013 #undef P
   5014 #undef A
   5015 #undef R
   5016 
   5017 /*
   5018  * Check whether encoding params can be set.
   5019  */
   5020 void
   5021 test_AUDIO_SETINFO_params_set(int openmode, int aimode, int pause)
   5022 {
   5023 	struct audio_info ai;
   5024 	int r;
   5025 	int fd;
   5026 
   5027 	/*
   5028 	 * aimode is bool value that indicates whether to change ai.mode.
   5029 	 * pause is bool value that indicates whether to change ai.*.pause.
   5030 	 */
   5031 
   5032 	TEST("AUDIO_SETINFO_params_%s_%d_%d",
   5033 	    openmode_str[openmode] + 2, aimode, pause);
   5034 	if (mode2aumode(openmode) == 0) {
   5035 		XP_SKIP("Operation not allowed on this hardware property");
   5036 		return;
   5037 	}
   5038 
   5039 	/* On half-duplex, O_RDWR is the same as O_WRONLY, so skip it */
   5040 	if (!hw_fulldup() && openmode == O_RDWR) {
   5041 		XP_SKIP("This is the same with O_WRONLY on half-duplex");
   5042 		return;
   5043 	}
   5044 
   5045 	fd = OPEN(devaudio, openmode);
   5046 	REQUIRED_SYS_OK(fd);
   5047 
   5048 	AUDIO_INITINFO(&ai);
   5049 	/*
   5050 	 * It takes time and effort to check all parameters independently,
   5051 	 * so that use sample_rate as a representative.
   5052 	 */
   5053 	ai.play.sample_rate = 11025;
   5054 	ai.record.sample_rate = 11025;
   5055 	if (aimode)
   5056 		ai.mode = mode2aumode(openmode) & ~AUMODE_PLAY_ALL;
   5057 	if (pause) {
   5058 		ai.play.pause = 1;
   5059 		ai.record.pause = 1;
   5060 	}
   5061 
   5062 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5063 	XP_SYS_EQ(0, r);
   5064 
   5065 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5066 	XP_SYS_EQ(0, r);
   5067 	int expmode = (aimode)
   5068 	    ? (mode2aumode(openmode) & ~AUMODE_PLAY_ALL)
   5069 	    : mode2aumode(openmode);
   5070 	XP_EQ(expmode, ai.mode);
   5071 	XP_EQ(11025, ai.play.sample_rate);
   5072 	XP_EQ(pause, ai.play.pause);
   5073 	XP_EQ(11025, ai.record.sample_rate);
   5074 	XP_EQ(pause, ai.record.pause);
   5075 
   5076 	r = CLOSE(fd);
   5077 	XP_SYS_EQ(0, r);
   5078 }
   5079 #define f(a,b,c) test_AUDIO_SETINFO_params_set(a, b, c)
   5080 DEF(AUDIO_SETINFO_params_set_RDONLY_0)	{ f(O_RDONLY, 0, 0); }
   5081 DEF(AUDIO_SETINFO_params_set_RDONLY_1)	{ f(O_RDONLY, 0, 1); }
   5082 /* On RDONLY, ai.mode is not changable
   5083  *  AUDIO_SETINFO_params_set_RDONLY_2)	{ f(O_RDONLY, 1, 0); }
   5084  *  AUDIO_SETINFO_params_set_RDONLY_3)	{ f(O_RDONLY, 1, 1); }
   5085  */
   5086 DEF(AUDIO_SETINFO_params_set_WRONLY_0)	{ f(O_WRONLY, 0, 0); }
   5087 DEF(AUDIO_SETINFO_params_set_WRONLY_1)	{ f(O_WRONLY, 0, 1); }
   5088 DEF(AUDIO_SETINFO_params_set_WRONLY_2)	{ f(O_WRONLY, 1, 0); }
   5089 DEF(AUDIO_SETINFO_params_set_WRONLY_3)	{ f(O_WRONLY, 1, 1); }
   5090 DEF(AUDIO_SETINFO_params_set_RDWR_0)	{ f(O_RDWR, 0, 0); }
   5091 DEF(AUDIO_SETINFO_params_set_RDWR_1)	{ f(O_RDWR, 0, 1); }
   5092 DEF(AUDIO_SETINFO_params_set_RDWR_2)	{ f(O_RDWR, 1, 0); }
   5093 DEF(AUDIO_SETINFO_params_set_RDWR_3)	{ f(O_RDWR, 1, 1); }
   5094 #undef f
   5095 
   5096 /*
   5097  * AUDIO_SETINFO for existing track should not be interfered by other
   5098  * descriptor.
   5099  * AUDIO_SETINFO for non-existing track affects/is affected sticky parameters
   5100  * for backward compatibility.
   5101  */
   5102 DEF(AUDIO_SETINFO_params_simul)
   5103 {
   5104 	struct audio_info ai;
   5105 	int fd0;
   5106 	int fd1;
   5107 	int r;
   5108 
   5109 	TEST("AUDIO_SETINFO_params_simul");
   5110 	if (netbsd < 8) {
   5111 		XP_SKIP("Multiple open is not supported");
   5112 		return;
   5113 	}
   5114 	if (hw_canplay() == 0) {
   5115 		XP_SKIP("This test is for playable device");
   5116 		return;
   5117 	}
   5118 
   5119 	/* Open the 1st one as playback only */
   5120 	fd0 = OPEN(devaudio, O_WRONLY);
   5121 	REQUIRED_SYS_OK(fd0);
   5122 
   5123 	/* Open the 2nd one as both of playback and recording */
   5124 	fd1 = OPEN(devaudio, O_RDWR);
   5125 	REQUIRED_SYS_OK(fd1);
   5126 
   5127 	/* Change some parameters of both track on the 2nd one */
   5128 	AUDIO_INITINFO(&ai);
   5129 	ai.play.sample_rate = 11025;
   5130 	ai.record.sample_rate = 11025;
   5131 	r = IOCTL(fd1, AUDIO_SETINFO, &ai, "");
   5132 	XP_SYS_EQ(0, r);
   5133 
   5134 	/*
   5135 	 * The 1st one doesn't have recording track so that only recording
   5136 	 * parameter is affected by sticky parameter.
   5137 	 */
   5138 	memset(&ai, 0, sizeof(ai));
   5139 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "");
   5140 	XP_SYS_EQ(0, r);
   5141 	XP_EQ(8000, ai.play.sample_rate);
   5142 	XP_EQ(11025, ai.record.sample_rate);
   5143 
   5144 	/* Next, change some parameters of both track on the 1st one */
   5145 	AUDIO_INITINFO(&ai);
   5146 	ai.play.sample_rate = 16000;
   5147 	ai.record.sample_rate = 16000;
   5148 	r = IOCTL(fd0, AUDIO_SETINFO, &ai, "");
   5149 	XP_SYS_EQ(0, r);
   5150 
   5151 	/*
   5152 	 * On bi-directional device, the 2nd one has both track so that
   5153 	 * both track are not affected by sticky parameter.
   5154 	 * On uni-directional device, the 2nd one has only playback track
   5155 	 * so that playback track is not affected by sticky parameter.
   5156 	 */
   5157 	memset(&ai, 0, sizeof(ai));
   5158 	r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   5159 	XP_SYS_EQ(0, r);
   5160 	XP_EQ(11025, ai.play.sample_rate);
   5161 	if (hw_bidir()) {
   5162 		XP_EQ(11025, ai.record.sample_rate);
   5163 	} else {
   5164 		XP_EQ(16000, ai.record.sample_rate);
   5165 	}
   5166 
   5167 	r = CLOSE(fd0);
   5168 	XP_SYS_EQ(0, r);
   5169 	r = CLOSE(fd1);
   5170 	XP_SYS_EQ(0, r);
   5171 }
   5172 
   5173 /*
   5174  * AUDIO_SETINFO(encoding/precision) is tested in AUDIO_GETENC_range below.
   5175  */
   5176 
   5177 /*
   5178  * Check whether the number of channels can be set.
   5179  */
   5180 DEF(AUDIO_SETINFO_channels)
   5181 {
   5182 	struct audio_info hwinfo;
   5183 	struct audio_info ai;
   5184 	int mode;
   5185 	int r;
   5186 	int fd;
   5187 	int i;
   5188 	unsigned int ch;
   5189 	struct {
   5190 		int ch;
   5191 		bool expected;
   5192 	} table[] = {
   5193 		{  0,	false },
   5194 		{  1,	true },	/* monaural */
   5195 		{  2,	true },	/* stereo */
   5196 	};
   5197 
   5198 	TEST("AUDIO_SETINFO_channels");
   5199 	if (netbsd < 8) {
   5200 		/*
   5201 		 * On NetBSD7, the result depends the hardware and there is
   5202 		 * no way to know it.
   5203 		 */
   5204 		XP_SKIP("The test doesn't make sense on NetBSD7");
   5205 		return;
   5206 	}
   5207 
   5208 	mode = openable_mode();
   5209 	fd = OPEN(devaudio, mode);
   5210 	REQUIRED_SYS_OK(fd);
   5211 
   5212 	/*
   5213 	 * The audio layer always supports monaural and stereo regardless of
   5214 	 * the hardware capability.
   5215 	 */
   5216 	for (i = 0; i < (int)__arraycount(table); i++) {
   5217 		ch = table[i].ch;
   5218 		bool expected = table[i].expected;
   5219 
   5220 		AUDIO_INITINFO(&ai);
   5221 		if (mode != O_RDONLY)
   5222 			ai.play.channels = ch;
   5223 		if (mode != O_WRONLY)
   5224 			ai.record.channels = ch;
   5225 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5226 		if (expected) {
   5227 			/* Expects to succeed */
   5228 			XP_SYS_EQ(0, r);
   5229 
   5230 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5231 			XP_SYS_EQ(0, r);
   5232 			if (mode != O_RDONLY)
   5233 				XP_EQ(ch, ai.play.channels);
   5234 			if (mode != O_WRONLY)
   5235 				XP_EQ(ch, ai.record.channels);
   5236 		} else {
   5237 			/* Expects to fail */
   5238 			XP_SYS_NG(EINVAL, r);
   5239 		}
   5240 	}
   5241 
   5242 	/*
   5243 	 * The maximum number of supported channels depends the hardware.
   5244 	 */
   5245 	/* Get the number of channels that the hardware supports */
   5246 	r = IOCTL(fd, AUDIO_GETFORMAT, &hwinfo, "");
   5247 	REQUIRED_SYS_EQ(0, r);
   5248 
   5249 	if ((hwinfo.mode & AUMODE_PLAY)) {
   5250 		DPRINTF("  > hwinfo.play.channels = %d\n",
   5251 		    hwinfo.play.channels);
   5252 		for (ch = 3; ch <= hwinfo.play.channels; ch++) {
   5253 			AUDIO_INITINFO(&ai);
   5254 			ai.play.channels = ch;
   5255 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5256 			XP_SYS_EQ(0, r);
   5257 
   5258 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5259 			XP_SYS_EQ(0, r);
   5260 			XP_EQ(ch, ai.play.channels);
   5261 		}
   5262 
   5263 		AUDIO_INITINFO(&ai);
   5264 		ai.play.channels = ch;
   5265 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5266 		XP_SYS_NG(EINVAL, r);
   5267 	}
   5268 	if ((hwinfo.mode & AUMODE_RECORD)) {
   5269 		DPRINTF("  > hwinfo.record.channels = %d\n",
   5270 		    hwinfo.record.channels);
   5271 		for (ch = 3; ch <= hwinfo.record.channels; ch++) {
   5272 			AUDIO_INITINFO(&ai);
   5273 			ai.record.channels = ch;
   5274 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5275 			XP_SYS_EQ(0, r);
   5276 
   5277 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5278 			XP_SYS_EQ(0, r);
   5279 			XP_EQ(ch, ai.record.channels);
   5280 		}
   5281 
   5282 		AUDIO_INITINFO(&ai);
   5283 		ai.record.channels = ch;
   5284 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5285 		XP_SYS_NG(EINVAL, r);
   5286 	}
   5287 
   5288 	r = CLOSE(fd);
   5289 	XP_SYS_EQ(0, r);
   5290 }
   5291 
   5292 /*
   5293  * Check whether the sample rate can be set.
   5294  */
   5295 DEF(AUDIO_SETINFO_sample_rate)
   5296 {
   5297 	struct audio_info ai;
   5298 	int mode;
   5299 	int r;
   5300 	int fd;
   5301 	int i;
   5302 	struct {
   5303 		int freq;
   5304 		bool expected;
   5305 	} table[] = {
   5306 		{    999,	false },
   5307 		{   1000,	true },	/* lower limit */
   5308 		{  48000,	true },
   5309 		{ 192000,	true },	/* upper limit */
   5310 		{ 192001,	false },
   5311 	};
   5312 
   5313 	TEST("AUDIO_SETINFO_sample_rate");
   5314 	if (netbsd < 8) {
   5315 		/*
   5316 		 * On NetBSD7, the result depends the hardware and there is
   5317 		 * no way to know it.
   5318 		 */
   5319 		XP_SKIP("The test doesn't make sense on NetBSD7");
   5320 		return;
   5321 	}
   5322 
   5323 	mode = openable_mode();
   5324 	fd = OPEN(devaudio, mode);
   5325 	REQUIRED_SYS_OK(fd);
   5326 
   5327 	for (i = 0; i < (int)__arraycount(table); i++) {
   5328 		int freq = table[i].freq;
   5329 		bool expected = table[i].expected;
   5330 
   5331 		AUDIO_INITINFO(&ai);
   5332 		if (mode != O_RDONLY)
   5333 			ai.play.sample_rate = freq;
   5334 		if (mode != O_WRONLY)
   5335 			ai.record.sample_rate = freq;
   5336 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "sample_rate=%d", freq);
   5337 		if (expected) {
   5338 			/* Expects to succeed */
   5339 			XP_SYS_EQ(0, r);
   5340 
   5341 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5342 			XP_SYS_EQ(0, r);
   5343 			if (mode != O_RDONLY)
   5344 				XP_EQ(freq, ai.play.sample_rate);
   5345 			if (mode != O_WRONLY)
   5346 				XP_EQ(freq, ai.record.sample_rate);
   5347 		} else {
   5348 			/* Expects to fail */
   5349 			XP_SYS_NG(EINVAL, r);
   5350 		}
   5351 	}
   5352 
   5353 	r = CLOSE(fd);
   5354 	XP_SYS_EQ(0, r);
   5355 }
   5356 
   5357 /*
   5358  * SETINFO(sample_rate = 0) should fail correctly.
   5359  */
   5360 DEF(AUDIO_SETINFO_sample_rate_0)
   5361 {
   5362 	struct audio_info ai;
   5363 	int mode;
   5364 	int r;
   5365 	int fd;
   5366 
   5367 	TEST("AUDIO_SETINFO_sample_rate_0");
   5368 	if (netbsd < 9) {
   5369 		/*
   5370 		 * On NetBSD7,8 this will block system call and you will not
   5371 		 * even be able to shutdown...
   5372 		 */
   5373 		XP_SKIP("This will cause an infinate loop in the kernel");
   5374 		return;
   5375 	}
   5376 
   5377 	mode = openable_mode();
   5378 	fd = OPEN(devaudio, mode);
   5379 	REQUIRED_SYS_OK(fd);
   5380 
   5381 	AUDIO_INITINFO(&ai);
   5382 	ai.play.sample_rate = 0;
   5383 	ai.record.sample_rate = 0;
   5384 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "sample_rate=0");
   5385 	/* Expects to fail */
   5386 	XP_SYS_NG(EINVAL, r);
   5387 
   5388 	r = CLOSE(fd);
   5389 	XP_SYS_EQ(0, r);
   5390 }
   5391 
   5392 /*
   5393  * Check whether the pause/unpause works.
   5394  */
   5395 void
   5396 test_AUDIO_SETINFO_pause(int openmode, int aimode, int param)
   5397 {
   5398 	struct audio_info ai;
   5399 	int r;
   5400 	int fd;
   5401 
   5402 	/*
   5403 	 * aimode is bool value that indicates whether to change ai.mode.
   5404 	 * param is bool value that indicates whether to change encoding
   5405 	 * parameters of ai.{play,record}.*.
   5406 	 */
   5407 
   5408 	TEST("AUDIO_SETINFO_pause_%s_%d_%d",
   5409 	    openmode_str[openmode] + 2, aimode, param);
   5410 	if (mode2aumode(openmode) == 0) {
   5411 		XP_SKIP("Operation not allowed on this hardware property");
   5412 		return;
   5413 	}
   5414 
   5415 	/* On half-duplex, O_RDWR is the same as O_WRONLY, so skip it */
   5416 	if (!hw_fulldup() && openmode == O_RDWR) {
   5417 		XP_SKIP("This is the same with O_WRONLY on half-duplex");
   5418 		return;
   5419 	}
   5420 
   5421 	fd = OPEN(devaudio, openmode);
   5422 	REQUIRED_SYS_OK(fd);
   5423 
   5424 	/* Set pause */
   5425 	AUDIO_INITINFO(&ai);
   5426 	ai.play.pause = 1;
   5427 	ai.record.pause = 1;
   5428 	if (aimode)
   5429 		ai.mode = mode2aumode(openmode) & ~AUMODE_PLAY_ALL;
   5430 	if (param) {
   5431 		ai.play.sample_rate = 11025;
   5432 		ai.record.sample_rate = 11025;
   5433 	}
   5434 
   5435 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5436 	XP_SYS_EQ(0, r);
   5437 
   5438 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5439 	XP_SYS_EQ(0, r);
   5440 	int expmode = (aimode)
   5441 	    ? (mode2aumode(openmode) & ~AUMODE_PLAY_ALL)
   5442 	    : mode2aumode(openmode);
   5443 	XP_EQ(expmode, ai.mode);
   5444 	XP_EQ(1, ai.play.pause);
   5445 	XP_EQ(param ? 11025 : 8000, ai.play.sample_rate);
   5446 	XP_EQ(1, ai.record.pause);
   5447 	XP_EQ(param ? 11025 : 8000, ai.record.sample_rate);
   5448 
   5449 	/* Set unpause (?) */
   5450 	AUDIO_INITINFO(&ai);
   5451 	ai.play.pause = 0;
   5452 	ai.record.pause = 0;
   5453 	if (aimode)
   5454 		ai.mode = mode2aumode(openmode);
   5455 	if (param) {
   5456 		ai.play.sample_rate = 16000;
   5457 		ai.record.sample_rate = 16000;
   5458 	}
   5459 
   5460 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5461 	XP_SYS_EQ(0, r);
   5462 
   5463 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5464 	XP_SYS_EQ(0, r);
   5465 	XP_EQ(mode2aumode(openmode), ai.mode);
   5466 	XP_EQ(0, ai.play.pause);
   5467 	XP_EQ(0, ai.record.pause);
   5468 	if (openmode != O_RDONLY)
   5469 		XP_EQ(param ? 16000 : 8000, ai.play.sample_rate);
   5470 	if (openmode != O_WRONLY)
   5471 		XP_EQ(param ? 16000 : 8000, ai.record.sample_rate);
   5472 
   5473 	r = CLOSE(fd);
   5474 	XP_SYS_EQ(0, r);
   5475 }
   5476 DEF(AUDIO_SETINFO_pause_RDONLY_0) { test_AUDIO_SETINFO_pause(O_RDONLY, 0, 0); }
   5477 DEF(AUDIO_SETINFO_pause_RDONLY_1) { test_AUDIO_SETINFO_pause(O_RDONLY, 0, 1); }
   5478 /* On RDONLY, ai.mode is not changable
   5479  *  AUDIO_SETINFO_pause_RDONLY_2) { test_AUDIO_SETINFO_pause(O_RDONLY, 1, 0); }
   5480  *  AUDIO_SETINFO_pause_RDONLY_3) { test_AUDIO_SETINFO_pause(O_RDONLY, 1, 1); }
   5481  */
   5482 DEF(AUDIO_SETINFO_pause_WRONLY_0) { test_AUDIO_SETINFO_pause(O_WRONLY, 0, 0); }
   5483 DEF(AUDIO_SETINFO_pause_WRONLY_1) { test_AUDIO_SETINFO_pause(O_WRONLY, 0, 1); }
   5484 DEF(AUDIO_SETINFO_pause_WRONLY_2) { test_AUDIO_SETINFO_pause(O_WRONLY, 1, 0); }
   5485 DEF(AUDIO_SETINFO_pause_WRONLY_3) { test_AUDIO_SETINFO_pause(O_WRONLY, 1, 1); }
   5486 DEF(AUDIO_SETINFO_pause_RDWR_0)   { test_AUDIO_SETINFO_pause(O_RDWR, 0, 0); }
   5487 DEF(AUDIO_SETINFO_pause_RDWR_1)   { test_AUDIO_SETINFO_pause(O_RDWR, 0, 1); }
   5488 DEF(AUDIO_SETINFO_pause_RDWR_2)   { test_AUDIO_SETINFO_pause(O_RDWR, 1, 0); }
   5489 DEF(AUDIO_SETINFO_pause_RDWR_3)   { test_AUDIO_SETINFO_pause(O_RDWR, 1, 1); }
   5490 
   5491 /*
   5492  * Check whether gain can be obtained/set.
   5493  * And the gain should work with rich mixer.
   5494  * PR kern/52781
   5495  */
   5496 DEF(AUDIO_SETINFO_gain)
   5497 {
   5498 	struct audio_info ai;
   5499 	mixer_ctrl_t m;
   5500 	int index;
   5501 	int master;
   5502 	int master_backup;
   5503 	int gain;
   5504 	int fd;
   5505 	int mixerfd;
   5506 	int r;
   5507 
   5508 	TEST("AUDIO_SETINFO_gain");
   5509 
   5510 	/* Open /dev/mixer */
   5511 	mixerfd = OPEN(devmixer, O_RDWR);
   5512 	REQUIRED_SYS_OK(mixerfd);
   5513 	index = mixer_get_outputs_master(mixerfd);
   5514 	if (index == -1) {
   5515 		XP_SKIP("Hardware has no outputs.master");
   5516 		CLOSE(mixerfd);
   5517 		return;
   5518 	}
   5519 
   5520 	/*
   5521 	 * Get current outputs.master.
   5522 	 * auich(4) requires class type (m.type) and number of channels
   5523 	 * (un.value.num_channels) in addition to the index (m.dev)...
   5524 	 * What is the index...?
   5525 	 */
   5526 	memset(&m, 0, sizeof(m));
   5527 	m.dev = index;
   5528 	m.type = AUDIO_MIXER_VALUE;
   5529 	m.un.value.num_channels = 1; /* dummy */
   5530 	r = IOCTL(mixerfd, AUDIO_MIXER_READ, &m, "m.dev=%d", m.dev);
   5531 	REQUIRED_SYS_EQ(0, r);
   5532 	master = m.un.value.level[0];
   5533 	DPRINTF("  > outputs.master = %d\n", master);
   5534 	master_backup = master;
   5535 
   5536 	/* Open /dev/audio */
   5537 	fd = OPEN(devaudio, O_WRONLY);
   5538 	REQUIRED_SYS_OK(fd);
   5539 
   5540 	/* Check ai.play.gain */
   5541 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   5542 	XP_SYS_EQ(0, r);
   5543 	XP_EQ(master, ai.play.gain);
   5544 
   5545 	/* Change it some different value */
   5546 	AUDIO_INITINFO(&ai);
   5547 	if (master == 0)
   5548 		gain = 255;
   5549 	else
   5550 		gain = 0;
   5551 	ai.play.gain = gain;
   5552 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "play.gain=%d", ai.play.gain);
   5553 	XP_SYS_EQ(0, r);
   5554 
   5555 	/* Check gain has changed */
   5556 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "play.gain");
   5557 	XP_SYS_EQ(0, r);
   5558 	XP_NE(master, ai.play.gain);
   5559 
   5560 	/* Check whether outputs.master work with gain */
   5561 	r = IOCTL(mixerfd, AUDIO_MIXER_READ, &m, "");
   5562 	XP_SYS_EQ(0, r);
   5563 	XP_EQ(ai.play.gain, m.un.value.level[0]);
   5564 
   5565 	/* Restore outputs.master */
   5566 	AUDIO_INITINFO(&ai);
   5567 	ai.play.gain = master_backup;
   5568 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "play.gain=%d", ai.play.gain);
   5569 	XP_SYS_EQ(0, r);
   5570 
   5571 	r = CLOSE(fd);
   5572 	XP_SYS_EQ(0, r);
   5573 	r = CLOSE(mixerfd);
   5574 	XP_SYS_EQ(0, r);
   5575 }
   5576 
   5577 #define NENC	(AUDIO_ENCODING_AC3 + 1)
   5578 #define NPREC	(5)
   5579 /*
   5580  * Make table of encoding+precision supported by this device.
   5581  * Return last used index .
   5582  * This function is called from test_AUDIO_GETENC_*()
   5583  */
   5584 int
   5585 getenc_make_table(int fd, int expected[][5])
   5586 {
   5587 	audio_encoding_t ae;
   5588 	int idx;
   5589 	int p;
   5590 	int r;
   5591 
   5592 	/*
   5593 	 * expected[][] is two dimensional table.
   5594 	 * encoding \ precision| 4  8  16  24  32
   5595 	 * --------------------+-----------------
   5596 	 * AUDIO_ENCODING_NONE |
   5597 	 * AUDIO_ENCODING_ULAW |
   5598 	 *  :
   5599 	 *
   5600 	 * Each cell has expected behavior.
   5601 	 *  0: the hardware doesn't support this encoding/precision.
   5602 	 *  1: the hardware supports this encoding/precision.
   5603 	 *  2: the hardware doesn't support this encoding/precision but
   5604 	 *     audio layer will respond as supported for compatibility.
   5605 	 */
   5606 	for (idx = 0; ; idx++) {
   5607 		memset(&ae, 0, sizeof(ae));
   5608 		ae.index = idx;
   5609 		r = IOCTL(fd, AUDIO_GETENC, &ae, "index=%d", idx);
   5610 		if (r != 0) {
   5611 			XP_SYS_NG(EINVAL, r);
   5612 			break;
   5613 		}
   5614 
   5615 		XP_EQ(idx, ae.index);
   5616 		if (0 <= ae.encoding && ae.encoding <= AUDIO_ENCODING_AC3) {
   5617 			XP_EQ_STR(encoding_names[ae.encoding], ae.name);
   5618 		} else {
   5619 			XP_FAIL("ae.encoding %d", ae.encoding);
   5620 		}
   5621 
   5622 		if (ae.precision != 4 &&
   5623 		    ae.precision != 8 &&
   5624 		    ae.precision != 16 &&
   5625 		    ae.precision != 24 &&
   5626 		    ae.precision != 32)
   5627 		{
   5628 			XP_FAIL("ae.precision %d", ae.precision);
   5629 		}
   5630 		/* Other bits should not be set */
   5631 		XP_EQ(0, (ae.flags & ~AUDIO_ENCODINGFLAG_EMULATED));
   5632 
   5633 		expected[ae.encoding][ae.precision / 8] = 1;
   5634 		DPRINTF("  > encoding=%s precision=%d\n",
   5635 		    encoding_names[ae.encoding], ae.precision);
   5636 	}
   5637 
   5638 	/*
   5639 	 * Backward compatibility bandaid.
   5640 	 *
   5641 	 * - Some encoding/precision pairs are obviously inconsistent
   5642 	 *   (e.g., encoding=AUDIO_ENCODING_PCM8, precision=16) but
   5643 	 *   it's due to historical reasons.
   5644 	 * - It's incomplete for NetBSD7 and NetBSD8.  I don't really
   5645 	 *   understand thier rule...  This is just memo, not specification.
   5646 	 */
   5647 #define SET(x) do {	\
   5648 	if ((x) == 0)	\
   5649 		x = 2;	\
   5650  } while (0)
   5651 #define p4 (0)
   5652 #define p8 (1)
   5653 #define p16 (2)
   5654 #define p24 (3)
   5655 #define p32 (4)
   5656 
   5657 	if (expected[AUDIO_ENCODING_SLINEAR][p8]) {
   5658 		SET(expected[AUDIO_ENCODING_SLINEAR_LE][p8]);
   5659 		SET(expected[AUDIO_ENCODING_SLINEAR_BE][p8]);
   5660 	}
   5661 	if (expected[AUDIO_ENCODING_ULINEAR][p8]) {
   5662 		SET(expected[AUDIO_ENCODING_ULINEAR_LE][p8]);
   5663 		SET(expected[AUDIO_ENCODING_ULINEAR_BE][p8]);
   5664 		SET(expected[AUDIO_ENCODING_PCM8][p8]);
   5665 		SET(expected[AUDIO_ENCODING_PCM16][p8]);
   5666 	}
   5667 	for (p = p16; p <= p32; p++) {
   5668 #if !defined(AUDIO_SUPPORT_LINEAR24)
   5669 		if (p == p24)
   5670 			continue;
   5671 #endif
   5672 		if (expected[AUDIO_ENCODING_SLINEAR_NE][p]) {
   5673 			SET(expected[AUDIO_ENCODING_SLINEAR][p]);
   5674 			SET(expected[AUDIO_ENCODING_PCM16][p]);
   5675 		}
   5676 		if (expected[AUDIO_ENCODING_ULINEAR_NE][p]) {
   5677 			SET(expected[AUDIO_ENCODING_ULINEAR][p]);
   5678 		}
   5679 	}
   5680 
   5681 	if (netbsd < 9) {
   5682 		if (expected[AUDIO_ENCODING_SLINEAR_LE][p16] ||
   5683 		    expected[AUDIO_ENCODING_SLINEAR_BE][p16] ||
   5684 		    expected[AUDIO_ENCODING_ULINEAR_LE][p16] ||
   5685 		    expected[AUDIO_ENCODING_ULINEAR_BE][p16])
   5686 		{
   5687 			SET(expected[AUDIO_ENCODING_PCM8][p8]);
   5688 			SET(expected[AUDIO_ENCODING_PCM16][p8]);
   5689 			SET(expected[AUDIO_ENCODING_SLINEAR_LE][p8]);
   5690 			SET(expected[AUDIO_ENCODING_SLINEAR_BE][p8]);
   5691 			SET(expected[AUDIO_ENCODING_ULINEAR_LE][p8]);
   5692 			SET(expected[AUDIO_ENCODING_ULINEAR_BE][p8]);
   5693 			SET(expected[AUDIO_ENCODING_SLINEAR][p8]);
   5694 			SET(expected[AUDIO_ENCODING_ULINEAR][p8]);
   5695 		}
   5696 	}
   5697 
   5698 	/* Return last used index */
   5699 	return idx;
   5700 #undef SET
   5701 #undef p4
   5702 #undef p8
   5703 #undef p16
   5704 #undef p24
   5705 #undef p32
   5706 }
   5707 
   5708 /*
   5709  * This function is called from test_AUDIO_GETENC below.
   5710  */
   5711 void
   5712 xp_getenc(int expected[][5], int enc, int j, int r, struct audio_prinfo *pr)
   5713 {
   5714 	int prec = (j == 0) ? 4 : j * 8;
   5715 
   5716 	if (expected[enc][j]) {
   5717 		/* expect to succeed */
   5718 		XP_SYS_EQ(0, r);
   5719 
   5720 		XP_EQ(enc, pr->encoding);
   5721 		XP_EQ(prec, pr->precision);
   5722 	} else {
   5723 		/* expect to fail */
   5724 		XP_SYS_NG(EINVAL, r);
   5725 	}
   5726 }
   5727 
   5728 /*
   5729  * This function is called from test_AUDIO_GETENC below.
   5730  */
   5731 void
   5732 getenc_check_encodings(int openmode, int expected[][5])
   5733 {
   5734 	struct audio_info ai;
   5735 	int fd;
   5736 	int i, j;
   5737 	int r;
   5738 
   5739 	fd = OPEN(devaudio, openmode);
   5740 	REQUIRED_SYS_OK(fd);
   5741 
   5742 	for (i = 0; i < NENC; i++) {
   5743 		for (j = 0; j < NPREC; j++) {
   5744 			/* precisions are 4 and 8, 16, 24, 32 */
   5745 			int prec = (j == 0) ? 4 : j * 8;
   5746 
   5747 			/*
   5748 			 * AUDIO_GETENC has no way to know range of
   5749 			 * supported channels and sample_rate.
   5750 			 */
   5751 			AUDIO_INITINFO(&ai);
   5752 			ai.play.encoding = i;
   5753 			ai.play.precision = prec;
   5754 			ai.record.encoding = i;
   5755 			ai.record.precision = prec;
   5756 
   5757 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s:%d",
   5758 			    encoding_names[i], prec);
   5759 			if (mode2play(openmode))
   5760 				xp_getenc(expected, i, j, r, &ai.play);
   5761 			if (mode2rec(openmode))
   5762 				xp_getenc(expected, i, j, r, &ai.record);
   5763 		}
   5764 	}
   5765 	r = CLOSE(fd);
   5766 	XP_SYS_EQ(0, r);
   5767 }
   5768 
   5769 /*
   5770  * Check whether encoding+precision obtained by AUDIO_GETENC can be set.
   5771  */
   5772 DEF(AUDIO_GETENC_range)
   5773 {
   5774 	audio_encoding_t ae;
   5775 	int fd;
   5776 	int r;
   5777 	int expected[NENC][NPREC];
   5778 	int i, j;
   5779 
   5780 	TEST("AUDIO_GETENC_range");
   5781 
   5782 	fd = OPEN(devaudio, openable_mode());
   5783 	REQUIRED_SYS_OK(fd);
   5784 
   5785 	memset(&expected, 0, sizeof(expected));
   5786 	i = getenc_make_table(fd, expected);
   5787 
   5788 	/* When error has occured, the next index should also occur error */
   5789 	ae.index = i + 1;
   5790 	r = IOCTL(fd, AUDIO_GETENC, &ae, "index=%d", ae.index);
   5791 	XP_SYS_NG(EINVAL, r);
   5792 
   5793 	r = CLOSE(fd);
   5794 	XP_SYS_EQ(0, r);
   5795 
   5796 	/* For debug */
   5797 	if (debug) {
   5798 		for (i = 0; i < NENC; i++) {
   5799 			printf("expected[%2d] %15s", i, encoding_names[i]);
   5800 			for (j = 0; j < NPREC; j++) {
   5801 				printf(" %d", expected[i][j]);
   5802 			}
   5803 			printf("\n");
   5804 		}
   5805 	}
   5806 
   5807 	/* Whether obtained encodings can be actually set */
   5808 	if (hw_fulldup()) {
   5809 		/* Test both R/W at once using single descriptor */
   5810 		getenc_check_encodings(O_RDWR, expected);
   5811 	} else {
   5812 		/* Test playback and recording if available */
   5813 		if (hw_canplay()) {
   5814 			getenc_check_encodings(O_WRONLY, expected);
   5815 		}
   5816 		if (hw_canplay() && hw_canrec()) {
   5817 			xxx_close_wait();
   5818 		}
   5819 		if (hw_canrec()) {
   5820 			getenc_check_encodings(O_RDONLY, expected);
   5821 		}
   5822 	}
   5823 }
   5824 #undef NENC
   5825 #undef NPREC
   5826 
   5827 /*
   5828  * Check AUDIO_GETENC out of range.
   5829  */
   5830 DEF(AUDIO_GETENC_error)
   5831 {
   5832 	audio_encoding_t e;
   5833 	int fd;
   5834 	int r;
   5835 
   5836 	TEST("AUDIO_GETENC_error");
   5837 
   5838 	fd = OPEN(devaudio, openable_mode());
   5839 	REQUIRED_SYS_OK(fd);
   5840 
   5841 	memset(&e, 0, sizeof(e));
   5842 	e.index = -1;
   5843 	r = IOCTL(fd, AUDIO_GETENC, &e, "index=-1");
   5844 	/* NetBSD7 may not fail depending on hardware driver */
   5845 	XP_SYS_NG(EINVAL, r);
   5846 
   5847 	r = CLOSE(fd);
   5848 	XP_SYS_EQ(0, r);
   5849 }
   5850 
   5851 /*
   5852  * AUDIO_[PR]ERROR should be zero on the initial state even on non-existent
   5853  * track.
   5854  */
   5855 void
   5856 test_AUDIO_ERROR(int openmode)
   5857 {
   5858 	int fd;
   5859 	int r;
   5860 	int errors;
   5861 
   5862 	TEST("AUDIO_ERROR_%s", openmode_str[openmode] + 2);
   5863 	if (mode2aumode(openmode) == 0) {
   5864 		XP_SKIP("Operation not allowed on this hardware property");
   5865 		return;
   5866 	}
   5867 
   5868 	fd = OPEN(devaudio, openmode);
   5869 	REQUIRED_SYS_OK(fd);
   5870 
   5871 	/* Check PERROR */
   5872 	errors = 0xdeadbeef;
   5873 	r = IOCTL(fd, AUDIO_PERROR, &errors, "");
   5874 	XP_SYS_EQ(0, r);
   5875 	XP_EQ(0, errors);
   5876 
   5877 	/* Check RERROR */
   5878 	errors = 0xdeadbeef;
   5879 	r = IOCTL(fd, AUDIO_RERROR, &errors, "");
   5880 	XP_SYS_EQ(0, r);
   5881 	XP_EQ(0, errors);
   5882 
   5883 	r = CLOSE(fd);
   5884 	XP_SYS_EQ(0, r);
   5885 }
   5886 DEF(AUDIO_ERROR_RDONLY)	{ test_AUDIO_ERROR(O_RDONLY); }
   5887 DEF(AUDIO_ERROR_WRONLY)	{ test_AUDIO_ERROR(O_WRONLY); }
   5888 DEF(AUDIO_ERROR_RDWR)	{ test_AUDIO_ERROR(O_RDWR); }
   5889 
   5890 /*
   5891  * /dev/audioctl can always be opened while /dev/audio is open.
   5892  */
   5893 void
   5894 test_audioctl_open_1(int fmode, int cmode)
   5895 {
   5896 	int fd;
   5897 	int ctl;
   5898 	int r;
   5899 
   5900 	TEST("audioctl_open_1_%s_%s",
   5901 	    openmode_str[fmode] + 2, openmode_str[cmode] + 2);
   5902 	if (hw_canplay() == 0 && fmode == O_WRONLY) {
   5903 		XP_SKIP("This test is for playable device");
   5904 		return;
   5905 	}
   5906 	if (hw_canrec() == 0 && fmode == O_RDONLY) {
   5907 		XP_SKIP("This test is for recordable device");
   5908 		return;
   5909 	}
   5910 
   5911 	fd = OPEN(devaudio, fmode);
   5912 	REQUIRED_SYS_OK(fd);
   5913 
   5914 	ctl = OPEN(devaudioctl, cmode);
   5915 	XP_SYS_OK(ctl);
   5916 
   5917 	r = CLOSE(ctl);
   5918 	XP_SYS_EQ(0, r);
   5919 
   5920 	r = CLOSE(fd);
   5921 	XP_SYS_EQ(0, r);
   5922 }
   5923 DEF(audioctl_open_1_RDONLY_RDONLY) { test_audioctl_open_1(O_RDONLY, O_RDONLY); }
   5924 DEF(audioctl_open_1_RDONLY_RWONLY) { test_audioctl_open_1(O_RDONLY, O_WRONLY); }
   5925 DEF(audioctl_open_1_RDONLY_RDWR)   { test_audioctl_open_1(O_RDONLY, O_RDWR); }
   5926 DEF(audioctl_open_1_WRONLY_RDONLY) { test_audioctl_open_1(O_WRONLY, O_RDONLY); }
   5927 DEF(audioctl_open_1_WRONLY_RWONLY) { test_audioctl_open_1(O_WRONLY, O_WRONLY); }
   5928 DEF(audioctl_open_1_WRONLY_RDWR)   { test_audioctl_open_1(O_WRONLY, O_RDWR); }
   5929 DEF(audioctl_open_1_RDWR_RDONLY)   { test_audioctl_open_1(O_RDWR, O_RDONLY); }
   5930 DEF(audioctl_open_1_RDWR_RWONLY)   { test_audioctl_open_1(O_RDWR, O_WRONLY); }
   5931 DEF(audioctl_open_1_RDWR_RDWR)     { test_audioctl_open_1(O_RDWR, O_RDWR); }
   5932 
   5933 /*
   5934  * /dev/audio can always be opened while /dev/audioctl is open.
   5935  */
   5936 void
   5937 test_audioctl_open_2(int fmode, int cmode)
   5938 {
   5939 	int fd;
   5940 	int ctl;
   5941 	int r;
   5942 
   5943 	TEST("audioctl_open_2_%s_%s",
   5944 	    openmode_str[fmode] + 2, openmode_str[cmode] + 2);
   5945 	if (hw_canplay() == 0 && fmode == O_WRONLY) {
   5946 		XP_SKIP("This test is for playable device");
   5947 		return;
   5948 	}
   5949 	if (hw_canrec() == 0 && fmode == O_RDONLY) {
   5950 		XP_SKIP("This test is for recordable device");
   5951 		return;
   5952 	}
   5953 
   5954 	ctl = OPEN(devaudioctl, cmode);
   5955 	REQUIRED_SYS_OK(ctl);
   5956 
   5957 	fd = OPEN(devaudio, fmode);
   5958 	XP_SYS_OK(fd);
   5959 
   5960 	r = CLOSE(fd);
   5961 	XP_SYS_EQ(0, r);
   5962 
   5963 	r = CLOSE(ctl);
   5964 	XP_SYS_EQ(0, r);
   5965 }
   5966 DEF(audioctl_open_2_RDONLY_RDONLY) { test_audioctl_open_2(O_RDONLY, O_RDONLY); }
   5967 DEF(audioctl_open_2_RDONLY_RWONLY) { test_audioctl_open_2(O_RDONLY, O_WRONLY); }
   5968 DEF(audioctl_open_2_RDONLY_RDWR)   { test_audioctl_open_2(O_RDONLY, O_RDWR); }
   5969 DEF(audioctl_open_2_WRONLY_RDONLY) { test_audioctl_open_2(O_WRONLY, O_RDONLY); }
   5970 DEF(audioctl_open_2_WRONLY_RWONLY) { test_audioctl_open_2(O_WRONLY, O_WRONLY); }
   5971 DEF(audioctl_open_2_WRONLY_RDWR)   { test_audioctl_open_2(O_WRONLY, O_RDWR); }
   5972 DEF(audioctl_open_2_RDWR_RDONLY)   { test_audioctl_open_2(O_RDWR, O_RDONLY); }
   5973 DEF(audioctl_open_2_RDWR_RWONLY)   { test_audioctl_open_2(O_RDWR, O_WRONLY); }
   5974 DEF(audioctl_open_2_RDWR_RDWR)     { test_audioctl_open_2(O_RDWR, O_RDWR); }
   5975 
   5976 /*
   5977  * Open multiple /dev/audioctl.
   5978  */
   5979 DEF(audioctl_open_simul)
   5980 {
   5981 	int ctl0;
   5982 	int ctl1;
   5983 	int r;
   5984 
   5985 	TEST("audioctl_open_simul");
   5986 
   5987 	ctl0 = OPEN(devaudioctl, O_RDWR);
   5988 	REQUIRED_SYS_OK(ctl0);
   5989 
   5990 	ctl1 = OPEN(devaudioctl, O_RDWR);
   5991 	XP_SYS_OK(ctl1);
   5992 
   5993 	r = CLOSE(ctl0);
   5994 	XP_SYS_EQ(0, r);
   5995 
   5996 	r = CLOSE(ctl1);
   5997 	XP_SYS_EQ(0, r);
   5998 }
   5999 
   6000 /*
   6001  * /dev/audioctl can be opened by other user who opens /dev/audioctl,
   6002  * /dev/audioctl can be opened by other user who opens /dev/audio,
   6003  * /dev/audio    can be opened by other user who opens /dev/audioctl,
   6004  * regardless of multiuser mode.
   6005  */
   6006 void
   6007 try_audioctl_open_multiuser(const char *dev1, const char *dev2)
   6008 {
   6009 	int fd1;
   6010 	int fd2;
   6011 	int r;
   6012 	uid_t ouid;
   6013 
   6014 	/*
   6015 	 * At first, open dev1 as root.
   6016 	 * And then open dev2 as unprivileged user.
   6017 	 */
   6018 
   6019 	fd1 = OPEN(dev1, O_RDWR);
   6020 	REQUIRED_SYS_OK(fd1);
   6021 
   6022 	ouid = GETUID();
   6023 	r = SETEUID(1);
   6024 	REQUIRED_SYS_EQ(0, r);
   6025 
   6026 	fd2 = OPEN(dev2, O_RDWR);
   6027 	XP_SYS_OK(fd2);
   6028 
   6029 	/* Close */
   6030 	r = CLOSE(fd2);
   6031 	XP_SYS_EQ(0, r);
   6032 
   6033 	r = SETEUID(ouid);
   6034 	REQUIRED_SYS_EQ(0, r);
   6035 
   6036 	r = CLOSE(fd1);
   6037 	XP_SYS_EQ(0, r);
   6038 }
   6039 /*
   6040  * This is a wrapper for audioctl_open_multiuser.
   6041  * XXX XP_* macros are not compatible with on-error-goto, we need try-catch...
   6042  */
   6043 void
   6044 test_audioctl_open_multiuser(bool multiuser,
   6045 	const char *dev1, const char *dev2)
   6046 {
   6047 	char mibname[32];
   6048 	bool oldval;
   6049 	size_t oldlen;
   6050 	int r;
   6051 
   6052 	if (netbsd < 8 && multiuser == 1) {
   6053 		XP_SKIP("multiuser is not supported");
   6054 		return;
   6055 	}
   6056 	if (netbsd < 9) {
   6057 		/* NetBSD8 has no way (difficult) to determine device name */
   6058 		XP_SKIP("NetBSD8 cannot determine device name");
   6059 		return;
   6060 	}
   6061 	if (geteuid() != 0) {
   6062 		XP_SKIP("This test must be priviledged user");
   6063 		return;
   6064 	}
   6065 
   6066 	/* Get current multiuser mode (and save it) */
   6067 	snprintf(mibname, sizeof(mibname), "hw.%s.multiuser", devicename);
   6068 	oldlen = sizeof(oldval);
   6069 	r = SYSCTLBYNAME(mibname, &oldval, &oldlen, NULL, 0);
   6070 	REQUIRED_SYS_EQ(0, r);
   6071 	DPRINTF("  > multiuser=%d\n", oldval);
   6072 
   6073 	/* Change if necessary */
   6074 	if (oldval != multiuser) {
   6075 		r = SYSCTLBYNAME(mibname, NULL, NULL, &multiuser,
   6076 		    sizeof(multiuser));
   6077 		REQUIRED_SYS_EQ(0, r);
   6078 		DPRINTF("  > new multiuser=%d\n", multiuser);
   6079 	}
   6080 
   6081 	/* Do test */
   6082 	try_audioctl_open_multiuser(dev1, dev2);
   6083 
   6084 	/* Restore multiuser mode */
   6085 	if (oldval != multiuser) {
   6086 		DPRINTF("  > restore multiuser to %d\n", oldval);
   6087 		r = SYSCTLBYNAME(mibname, NULL, NULL, &oldval, sizeof(oldval));
   6088 		XP_SYS_EQ(0, r);
   6089 	}
   6090 }
   6091 DEF(audioctl_open_multiuser0_audio1) {
   6092 	TEST("audioctl_open_multiuser0_audio1");
   6093 	test_audioctl_open_multiuser(false, devaudio, devaudioctl);
   6094 }
   6095 DEF(audioctl_open_multiuser1_audio1) {
   6096 	TEST("audioctl_open_multiuser1_audio1");
   6097 	test_audioctl_open_multiuser(true, devaudio, devaudioctl);
   6098 }
   6099 DEF(audioctl_open_multiuser0_audio2) {
   6100 	TEST("audioctl_open_multiuser0_audio2");
   6101 	test_audioctl_open_multiuser(false, devaudioctl, devaudio);
   6102 }
   6103 DEF(audioctl_open_multiuser1_audio2) {
   6104 	TEST("audioctl_open_multiuser1_audio2");
   6105 	test_audioctl_open_multiuser(true, devaudioctl, devaudio);
   6106 }
   6107 DEF(audioctl_open_multiuser0_audioctl) {
   6108 	TEST("audioctl_open_multiuser0_audioctl");
   6109 	test_audioctl_open_multiuser(false, devaudioctl, devaudioctl);
   6110 }
   6111 DEF(audioctl_open_multiuser1_audioctl) {
   6112 	TEST("audioctl_open_multiuser1_audioctl");
   6113 	test_audioctl_open_multiuser(true, devaudioctl, devaudioctl);
   6114 }
   6115 
   6116 /*
   6117  * /dev/audioctl cannot be read/written regardless of its open mode.
   6118  */
   6119 void
   6120 test_audioctl_rw(int openmode)
   6121 {
   6122 	char buf[1];
   6123 	int fd;
   6124 	int r;
   6125 
   6126 	TEST("audioctl_rw_%s", openmode_str[openmode] + 2);
   6127 
   6128 	fd = OPEN(devaudioctl, openmode);
   6129 	REQUIRED_SYS_OK(fd);
   6130 
   6131 	if (mode2play(openmode)) {
   6132 		r = WRITE(fd, buf, sizeof(buf));
   6133 		XP_SYS_NG(ENODEV, r);
   6134 	}
   6135 
   6136 	if (mode2rec(openmode)) {
   6137 		r = READ(fd, buf, sizeof(buf));
   6138 		XP_SYS_NG(ENODEV, r);
   6139 	}
   6140 
   6141 	r = CLOSE(fd);
   6142 	XP_SYS_EQ(0, r);
   6143 }
   6144 DEF(audioctl_rw_RDONLY)	{ test_audioctl_rw(O_RDONLY); }
   6145 DEF(audioctl_rw_WRONLY)	{ test_audioctl_rw(O_WRONLY); }
   6146 DEF(audioctl_rw_RDWR)	{ test_audioctl_rw(O_RDWR); }
   6147 
   6148 /*
   6149  * poll(2) for /dev/audioctl should never raise.
   6150  * I'm not sure about consistency between poll(2) and kqueue(2) but
   6151  * anyway I follow it.
   6152  * XXX Omit checking each openmode
   6153  */
   6154 DEF(audioctl_poll)
   6155 {
   6156 	struct pollfd pfd;
   6157 	int fd;
   6158 	int r;
   6159 
   6160 	TEST("audioctl_poll");
   6161 
   6162 	fd = OPEN(devaudioctl, O_WRONLY);
   6163 	REQUIRED_SYS_OK(fd);
   6164 
   6165 	pfd.fd = fd;
   6166 	pfd.events = POLLOUT;
   6167 	r = POLL(&pfd, 1, 100);
   6168 	XP_SYS_EQ(0, r);
   6169 	XP_EQ(0, pfd.revents);
   6170 
   6171 	r = CLOSE(fd);
   6172 	XP_SYS_EQ(0, r);
   6173 }
   6174 
   6175 /*
   6176  * kqueue(2) for /dev/audioctl fails.
   6177  * I'm not sure about consistency between poll(2) and kqueue(2) but
   6178  * anyway I follow it.
   6179  * XXX Omit checking each openmode
   6180  */
   6181 DEF(audioctl_kqueue)
   6182 {
   6183 	struct kevent kev;
   6184 	int fd;
   6185 	int kq;
   6186 	int r;
   6187 
   6188 	TEST("audioctl_kqueue");
   6189 
   6190 	fd = OPEN(devaudioctl, O_WRONLY);
   6191 	REQUIRED_SYS_OK(fd);
   6192 
   6193 	kq = KQUEUE();
   6194 	XP_SYS_OK(kq);
   6195 
   6196 	EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
   6197 	r = KEVENT_SET(kq, &kev, 1);
   6198 	/*
   6199 	 * NetBSD7 has a bug.  It looks to wanted to treat it as successful
   6200 	 * but returned 1(== EPERM).
   6201 	 * On NetBSD9, I decided to return ENODEV.
   6202 	 */
   6203 	if (netbsd < 8) {
   6204 		XP_SYS_NG(1/*EPERM*/, r);
   6205 	} else {
   6206 		XP_SYS_NG(ENODEV, r);
   6207 	}
   6208 
   6209 	r = CLOSE(fd);
   6210 	XP_SYS_EQ(0, r);
   6211 }
   6212 
   6213 
   6214 /*
   6215  * This table is processed by t_audio.awk!
   6216  * Keep /^\tENT(testname),/ format in order to add to atf.
   6217  */
   6218 #define ENT(x) { #x, test__ ## x }
   6219 struct testentry testtable[] = {
   6220 	ENT(open_mode_RDONLY),
   6221 	ENT(open_mode_WRONLY),
   6222 	ENT(open_mode_RDWR),
   6223 	ENT(open_audio_RDONLY),
   6224 	ENT(open_audio_WRONLY),
   6225 	ENT(open_audio_RDWR),
   6226 	ENT(open_sound_RDONLY),
   6227 	ENT(open_sound_WRONLY),
   6228 	ENT(open_sound_RDWR),
   6229 	ENT(open_audioctl_RDONLY),
   6230 	ENT(open_audioctl_WRONLY),
   6231 	ENT(open_audioctl_RDWR),
   6232 	ENT(open_sound_sticky),
   6233 	ENT(open_audioctl_sticky),
   6234 	ENT(open_simul_RDONLY_RDONLY),
   6235 	ENT(open_simul_RDONLY_WRONLY),
   6236 	ENT(open_simul_RDONLY_RDWR),
   6237 	ENT(open_simul_WRONLY_RDONLY),
   6238 	ENT(open_simul_WRONLY_WRONLY),
   6239 	ENT(open_simul_WRONLY_RDWR),
   6240 	ENT(open_simul_RDWR_RDONLY),
   6241 	ENT(open_simul_RDWR_WRONLY),
   6242 	ENT(open_simul_RDWR_RDWR),
   6243 /**/	ENT(open_multiuser_0),		// XXX TODO sysctl
   6244 /**/	ENT(open_multiuser_1),		// XXX TODO sysctl
   6245 	ENT(write_PLAY_ALL),
   6246 	ENT(write_PLAY),
   6247 	ENT(read),
   6248 	ENT(rept_write),
   6249 	ENT(rept_read),
   6250 	ENT(rdwr_fallback_RDONLY),
   6251 	ENT(rdwr_fallback_WRONLY),
   6252 	ENT(rdwr_fallback_RDWR),
   6253 	ENT(rdwr_two_RDONLY_RDONLY),
   6254 	ENT(rdwr_two_RDONLY_WRONLY),
   6255 	ENT(rdwr_two_RDONLY_RDWR),
   6256 	ENT(rdwr_two_WRONLY_RDONLY),
   6257 	ENT(rdwr_two_WRONLY_WRONLY),
   6258 	ENT(rdwr_two_WRONLY_RDWR),
   6259 	ENT(rdwr_two_RDWR_RDONLY),
   6260 	ENT(rdwr_two_RDWR_WRONLY),
   6261 	ENT(rdwr_two_RDWR_RDWR),
   6262 	ENT(rdwr_simul),
   6263 	ENT(drain_incomplete),
   6264 	ENT(drain_pause),
   6265 	ENT(drain_onrec),
   6266 /**/	ENT(mmap_mode_RDONLY_NONE),	// XXX rump doesn't supprot mmap
   6267 /**/	ENT(mmap_mode_RDONLY_READ),	// XXX rump doesn't supprot mmap
   6268 /**/	ENT(mmap_mode_RDONLY_WRITE),	// XXX rump doesn't supprot mmap
   6269 /**/	ENT(mmap_mode_RDONLY_READWRITE),// XXX rump doesn't supprot mmap
   6270 /**/	ENT(mmap_mode_WRONLY_NONE),	// XXX rump doesn't supprot mmap
   6271 /**/	ENT(mmap_mode_WRONLY_READ),	// XXX rump doesn't supprot mmap
   6272 /**/	ENT(mmap_mode_WRONLY_WRITE),	// XXX rump doesn't supprot mmap
   6273 /**/	ENT(mmap_mode_WRONLY_READWRITE),// XXX rump doesn't supprot mmap
   6274 /**/	ENT(mmap_mode_RDWR_NONE),	// XXX rump doesn't supprot mmap
   6275 /**/	ENT(mmap_mode_RDWR_READ),	// XXX rump doesn't supprot mmap
   6276 /**/	ENT(mmap_mode_RDWR_WRITE),	// XXX rump doesn't supprot mmap
   6277 /**/	ENT(mmap_mode_RDWR_READWRITE),	// XXX rump doesn't supprot mmap
   6278 /**/	ENT(mmap_len),			// XXX rump doesn't supprot mmap
   6279 /**/	ENT(mmap_twice),		// XXX rump doesn't supprot mmap
   6280 /**/	ENT(mmap_multi),		// XXX rump doesn't supprot mmap
   6281 	ENT(poll_mode_RDONLY_IN),
   6282 	ENT(poll_mode_RDONLY_OUT),
   6283 	ENT(poll_mode_RDONLY_INOUT),
   6284 	ENT(poll_mode_WRONLY_IN),
   6285 	ENT(poll_mode_WRONLY_OUT),
   6286 	ENT(poll_mode_WRONLY_INOUT),
   6287 	ENT(poll_mode_RDWR_IN),
   6288 	ENT(poll_mode_RDWR_OUT),
   6289 	ENT(poll_mode_RDWR_INOUT),
   6290 	ENT(poll_out_empty),
   6291 	ENT(poll_out_full),
   6292 	ENT(poll_out_hiwat),
   6293 /**/	ENT(poll_out_unpause),		// XXX does not seem to work on rump
   6294 /**/	ENT(poll_out_simul),		// XXX does not seem to work on rump
   6295 	ENT(poll_in_open_audio),
   6296 	ENT(poll_in_open_sound),
   6297 	ENT(poll_in_open_audioctl),
   6298 	ENT(poll_in_simul),
   6299 	ENT(kqueue_mode_RDONLY_READ),
   6300 	ENT(kqueue_mode_RDONLY_WRITE),
   6301 	ENT(kqueue_mode_WRONLY_READ),
   6302 	ENT(kqueue_mode_WRONLY_WRITE),
   6303 	ENT(kqueue_mode_RDWR_READ),
   6304 	ENT(kqueue_mode_RDWR_WRITE),
   6305 	ENT(kqueue_empty),
   6306 	ENT(kqueue_full),
   6307 	ENT(kqueue_hiwat),
   6308 /**/	ENT(kqueue_unpause),		// XXX does not seem to work on rump
   6309 /**/	ENT(kqueue_simul),		// XXX does not seem to work on rump
   6310 	ENT(ioctl_while_write),
   6311 	ENT(FIOASYNC_reset),
   6312 	ENT(FIOASYNC_play_signal),
   6313 	ENT(FIOASYNC_rec_signal),
   6314 /**/	ENT(FIOASYNC_multi),		// XXX does not seem to work on rump
   6315 	ENT(AUDIO_WSEEK),
   6316 	ENT(AUDIO_SETFD_RDONLY),
   6317 	ENT(AUDIO_SETFD_WRONLY),
   6318 	ENT(AUDIO_SETFD_RDWR),
   6319 	ENT(AUDIO_GETINFO_eof),
   6320 	ENT(AUDIO_SETINFO_mode_RDONLY_0),
   6321 	ENT(AUDIO_SETINFO_mode_RDONLY_1),
   6322 	ENT(AUDIO_SETINFO_mode_RDONLY_2),
   6323 	ENT(AUDIO_SETINFO_mode_RDONLY_3),
   6324 	ENT(AUDIO_SETINFO_mode_RDONLY_4),
   6325 	ENT(AUDIO_SETINFO_mode_RDONLY_5),
   6326 	ENT(AUDIO_SETINFO_mode_RDONLY_6),
   6327 	ENT(AUDIO_SETINFO_mode_RDONLY_7),
   6328 	ENT(AUDIO_SETINFO_mode_RDONLY_8),
   6329 	ENT(AUDIO_SETINFO_mode_WRONLY_0),
   6330 	ENT(AUDIO_SETINFO_mode_WRONLY_1),
   6331 	ENT(AUDIO_SETINFO_mode_WRONLY_2),
   6332 	ENT(AUDIO_SETINFO_mode_WRONLY_3),
   6333 	ENT(AUDIO_SETINFO_mode_WRONLY_4),
   6334 	ENT(AUDIO_SETINFO_mode_WRONLY_5),
   6335 	ENT(AUDIO_SETINFO_mode_WRONLY_6),
   6336 	ENT(AUDIO_SETINFO_mode_WRONLY_7),
   6337 	ENT(AUDIO_SETINFO_mode_WRONLY_8),
   6338 	ENT(AUDIO_SETINFO_mode_RDWR_0),
   6339 	ENT(AUDIO_SETINFO_mode_RDWR_1),
   6340 	ENT(AUDIO_SETINFO_mode_RDWR_2),
   6341 	ENT(AUDIO_SETINFO_mode_RDWR_3),
   6342 	ENT(AUDIO_SETINFO_mode_RDWR_4),
   6343 	ENT(AUDIO_SETINFO_mode_RDWR_5),
   6344 	ENT(AUDIO_SETINFO_mode_RDWR_6),
   6345 	ENT(AUDIO_SETINFO_mode_RDWR_7),
   6346 	ENT(AUDIO_SETINFO_mode_RDWR_8),
   6347 	ENT(AUDIO_SETINFO_params_set_RDONLY_0),
   6348 	ENT(AUDIO_SETINFO_params_set_RDONLY_1),
   6349 	ENT(AUDIO_SETINFO_params_set_WRONLY_0),
   6350 	ENT(AUDIO_SETINFO_params_set_WRONLY_1),
   6351 	ENT(AUDIO_SETINFO_params_set_WRONLY_2),
   6352 	ENT(AUDIO_SETINFO_params_set_WRONLY_3),
   6353 	ENT(AUDIO_SETINFO_params_set_RDWR_0),
   6354 	ENT(AUDIO_SETINFO_params_set_RDWR_1),
   6355 	ENT(AUDIO_SETINFO_params_set_RDWR_2),
   6356 	ENT(AUDIO_SETINFO_params_set_RDWR_3),
   6357 	ENT(AUDIO_SETINFO_params_simul),
   6358 	ENT(AUDIO_SETINFO_channels),
   6359 	ENT(AUDIO_SETINFO_sample_rate),
   6360 	ENT(AUDIO_SETINFO_sample_rate_0),
   6361 	ENT(AUDIO_SETINFO_pause_RDONLY_0),
   6362 	ENT(AUDIO_SETINFO_pause_RDONLY_1),
   6363 	ENT(AUDIO_SETINFO_pause_WRONLY_0),
   6364 	ENT(AUDIO_SETINFO_pause_WRONLY_1),
   6365 	ENT(AUDIO_SETINFO_pause_WRONLY_2),
   6366 	ENT(AUDIO_SETINFO_pause_WRONLY_3),
   6367 	ENT(AUDIO_SETINFO_pause_RDWR_0),
   6368 	ENT(AUDIO_SETINFO_pause_RDWR_1),
   6369 	ENT(AUDIO_SETINFO_pause_RDWR_2),
   6370 	ENT(AUDIO_SETINFO_pause_RDWR_3),
   6371 	ENT(AUDIO_SETINFO_gain),
   6372 	ENT(AUDIO_GETENC_range),
   6373 	ENT(AUDIO_GETENC_error),
   6374 	ENT(AUDIO_ERROR_RDONLY),
   6375 	ENT(AUDIO_ERROR_WRONLY),
   6376 	ENT(AUDIO_ERROR_RDWR),
   6377 	ENT(audioctl_open_1_RDONLY_RDONLY),
   6378 	ENT(audioctl_open_1_RDONLY_RWONLY),
   6379 	ENT(audioctl_open_1_RDONLY_RDWR),
   6380 	ENT(audioctl_open_1_WRONLY_RDONLY),
   6381 	ENT(audioctl_open_1_WRONLY_RWONLY),
   6382 	ENT(audioctl_open_1_WRONLY_RDWR),
   6383 	ENT(audioctl_open_1_RDWR_RDONLY),
   6384 	ENT(audioctl_open_1_RDWR_RWONLY),
   6385 	ENT(audioctl_open_1_RDWR_RDWR),
   6386 	ENT(audioctl_open_2_RDONLY_RDONLY),
   6387 	ENT(audioctl_open_2_RDONLY_RWONLY),
   6388 	ENT(audioctl_open_2_RDONLY_RDWR),
   6389 	ENT(audioctl_open_2_WRONLY_RDONLY),
   6390 	ENT(audioctl_open_2_WRONLY_RWONLY),
   6391 	ENT(audioctl_open_2_WRONLY_RDWR),
   6392 	ENT(audioctl_open_2_RDWR_RDONLY),
   6393 	ENT(audioctl_open_2_RDWR_RWONLY),
   6394 	ENT(audioctl_open_2_RDWR_RDWR),
   6395 	ENT(audioctl_open_simul),
   6396 /**/	ENT(audioctl_open_multiuser0_audio1),	// XXX TODO sysctl
   6397 /**/	ENT(audioctl_open_multiuser1_audio1),	// XXX TODO sysctl
   6398 /**/	ENT(audioctl_open_multiuser0_audio2),	// XXX TODO sysctl
   6399 /**/	ENT(audioctl_open_multiuser1_audio2),	// XXX TODO sysctl
   6400 /**/	ENT(audioctl_open_multiuser0_audioctl),	// XXX TODO sysctl
   6401 /**/	ENT(audioctl_open_multiuser1_audioctl),	// XXX TODO sysctl
   6402 	ENT(audioctl_rw_RDONLY),
   6403 	ENT(audioctl_rw_WRONLY),
   6404 	ENT(audioctl_rw_RDWR),
   6405 	ENT(audioctl_poll),
   6406 	ENT(audioctl_kqueue),
   6407 	{.name = NULL},
   6408 };
   6409