audiotest.c revision 1.19       1 /*	$NetBSD: audiotest.c,v 1.19 2022/04/23 07:47:42 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.19 2022/04/23 07:47:42 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 (PR 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_AUDIO_GETIOFFS_one(int);
   1399 void test_AUDIO_GETOOFFS_one(int);
   1400 void test_AUDIO_GETOOFFS_wrap(int);
   1401 void test_AUDIO_GETOOFFS_flush(int);
   1402 void test_AUDIO_GETOOFFS_set(int);
   1403 void test_audioctl_open_1(int, int);
   1404 void test_audioctl_open_2(int, int);
   1405 void try_audioctl_open_multiuser(const char *, const char *);
   1406 void test_audioctl_open_multiuser(bool, const char *, const char *);
   1407 void test_audioctl_rw(int);
   1408 
   1409 #define DEF(name) \
   1410 	void test__ ## name (void); \
   1411 	void test__ ## name (void)
   1412 
   1413 /*
   1414  * Whether it can be open()ed with specified mode.
   1415  */
   1416 void
   1417 test_open_mode(int mode)
   1418 {
   1419 	int fd;
   1420 	int r;
   1421 
   1422 	TEST("open_mode_%s", openmode_str[mode] + 2);
   1423 
   1424 	fd = OPEN(devaudio, mode);
   1425 	if (mode2aumode(mode) != 0) {
   1426 		XP_SYS_OK(fd);
   1427 	} else {
   1428 		XP_SYS_NG(ENXIO, fd);
   1429 	}
   1430 
   1431 	if (fd >= 0) {
   1432 		r = CLOSE(fd);
   1433 		XP_SYS_EQ(0, r);
   1434 	}
   1435 }
   1436 DEF(open_mode_RDONLY)	{ test_open_mode(O_RDONLY); }
   1437 DEF(open_mode_WRONLY)	{ test_open_mode(O_WRONLY); }
   1438 DEF(open_mode_RDWR)	{ test_open_mode(O_RDWR);   }
   1439 
   1440 /*
   1441  * Check the initial parameters and stickiness.
   1442  * /dev/audio
   1443  *	The initial parameters are always the same whenever you open.
   1444  * /dev/sound and /dev/audioctl
   1445  *	The initial parameters are inherited from the last /dev/sound or
   1446  *	/dev/audio.
   1447  */
   1448 void
   1449 test_open(const char *devname, int mode)
   1450 {
   1451 	struct audio_info ai;
   1452 	struct audio_info ai0;
   1453 	char devfile[16];
   1454 	int fd;
   1455 	int r;
   1456 	int can_play;
   1457 	int can_rec;
   1458 	int exp_mode;
   1459 	int exp_encoding;
   1460 	int exp_precision;
   1461 	int exp_channels;
   1462 	int exp_sample_rate;
   1463 	int exp_pause;
   1464 	int exp_popen;
   1465 	int exp_ropen;
   1466 
   1467 	TEST("open_%s_%s", devname, openmode_str[mode] + 2);
   1468 
   1469 	snprintf(devfile, sizeof(devfile), "/dev/%s%d", devname, unit);
   1470 	can_play = mode2play(mode);
   1471 	can_rec  = mode2rec(mode);
   1472 	if (strcmp(devname, "audioctl") != 0) {
   1473 		if (can_play + can_rec == 0) {
   1474 			/* Check whether it cannot be opened */
   1475 			fd = OPEN(devaudio, mode);
   1476 			XP_SYS_NG(ENXIO, fd);
   1477 			return;
   1478 		}
   1479 	}
   1480 
   1481 	/* /dev/audio is always initialized */
   1482 	if (strcmp(devname, "audio") == 0) {
   1483 		exp_encoding = AUDIO_ENCODING_ULAW;
   1484 		exp_precision = 8;
   1485 		exp_channels = 1;
   1486 		exp_sample_rate = 8000;
   1487 		exp_pause = 0;
   1488 	} else {
   1489 		exp_encoding = AUDIO_ENCODING_SLINEAR_LE;
   1490 		exp_precision = 16;
   1491 		exp_channels = 2;
   1492 		exp_sample_rate = 11025;
   1493 		exp_pause = 1;
   1494 	}
   1495 
   1496 	/* /dev/audioctl is always "not opened" */
   1497 	if (strcmp(devname, "audioctl") == 0) {
   1498 		exp_mode = 0;
   1499 		exp_popen = 0;
   1500 		exp_ropen = 0;
   1501 	} else {
   1502 		exp_mode = mode2aumode(mode);
   1503 		exp_popen = can_play;
   1504 		exp_ropen = can_rec;
   1505 	}
   1506 
   1507 
   1508 	/*
   1509 	 * At first, initialize the sticky parameters both of play and rec.
   1510 	 * This uses /dev/audio to verify /dev/audio.  It's not good way but
   1511 	 * I don't have better one...
   1512 	 */
   1513 	fd = OPEN(devaudio, openable_mode());
   1514 	REQUIRED_SYS_OK(fd);
   1515 	r = CLOSE(fd);
   1516 	REQUIRED_SYS_EQ(0, r);
   1517 
   1518 	/*
   1519 	 * Open target device and check the initial parameters
   1520 	 * At this moment, all devices are initialized by default.
   1521 	 */
   1522 	fd = OPEN(devfile, mode);
   1523 	REQUIRED_SYS_OK(fd);
   1524 	memset(&ai, 0, sizeof(ai));
   1525 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1526 	REQUIRED_SYS_EQ(0, r);
   1527 
   1528 	XP_NE(0, ai.blocksize);
   1529 		/* hiwat/lowat */
   1530 	XP_EQ(exp_mode, ai.mode);
   1531 	/* ai.play */
   1532 	XP_EQ(8000, ai.play.sample_rate);
   1533 	XP_EQ(1, ai.play.channels);
   1534 	XP_EQ(8, ai.play.precision);
   1535 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1536 		/* gain */
   1537 		/* port */
   1538 	XP_EQ(0, ai.play.seek);
   1539 		/* avail_ports */
   1540 	XP_NE(0, ai.play.buffer_size);
   1541 	XP_EQ(0, ai.play.samples);
   1542 	XP_EQ(0, ai.play.eof);
   1543 	XP_EQ(0, ai.play.pause);
   1544 	XP_EQ(0, ai.play.error);
   1545 	XP_EQ(0, ai.play.waiting);
   1546 		/* balance */
   1547 	XP_EQ(exp_popen, ai.play.open);
   1548 	XP_EQ(0, ai.play.active);
   1549 	/* ai.record */
   1550 	XP_EQ(8000, ai.record.sample_rate);
   1551 	XP_EQ(1, ai.record.channels);
   1552 	XP_EQ(8, ai.record.precision);
   1553 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1554 		/* gain */
   1555 		/* port */
   1556 	XP_EQ(0, ai.record.seek);
   1557 		/* avail_ports */
   1558 	XP_NE(0, ai.record.buffer_size);
   1559 	XP_EQ(0, ai.record.samples);
   1560 	XP_EQ(0, ai.record.eof);
   1561 	XP_EQ(0, ai.record.pause);
   1562 	XP_EQ(0, ai.record.error);
   1563 	XP_EQ(0, ai.record.waiting);
   1564 		/* balance */
   1565 	XP_EQ(exp_ropen, ai.record.open);
   1566 	if (netbsd < 9 && strcmp(devname, "sound") == 0) {
   1567 		/*
   1568 		 * On NetBSD7/8, it doesn't seem to start recording on open
   1569 		 * for /dev/sound.  It should be a bug.
   1570 		 */
   1571 		XP_EQ(0, ai.record.active);
   1572 	} else {
   1573 		XP_EQ(exp_ropen, ai.record.active);
   1574 	}
   1575 	/* Save it */
   1576 	ai0 = ai;
   1577 
   1578 	/*
   1579 	 * Change much as possible
   1580 	 */
   1581 	AUDIO_INITINFO(&ai);
   1582 	ai.mode = ai0.mode ^ AUMODE_PLAY_ALL;
   1583 	ai.play.sample_rate = 11025;
   1584 	ai.play.channels = 2;
   1585 	ai.play.precision = 16;
   1586 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1587 	ai.play.pause = 1;
   1588 	ai.record.sample_rate = 11025;
   1589 	ai.record.channels = 2;
   1590 	ai.record.precision = 16;
   1591 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1592 	ai.record.pause = 1;
   1593 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "ai");
   1594 	REQUIRED_SYS_EQ(0, r);
   1595 	r = CLOSE(fd);
   1596 	REQUIRED_SYS_EQ(0, r);
   1597 
   1598 	/*
   1599 	 * Open the same target device again and check
   1600 	 */
   1601 	fd = OPEN(devfile, mode);
   1602 	REQUIRED_SYS_OK(fd);
   1603 	memset(&ai, 0, sizeof(ai));
   1604 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1605 	REQUIRED_SYS_EQ(0, r);
   1606 
   1607 	XP_NE(0, ai.blocksize);
   1608 		/* hiwat/lowat */
   1609 	if (netbsd < 8) {
   1610 		/*
   1611 		 * On NetBSD7, the behavior when changing ai.mode on
   1612 		 * /dev/audioctl can not be explained yet but I won't
   1613 		 * verify it more over.
   1614 		 */
   1615 	} else {
   1616 		/* On NetBSD9, changing mode never affects other fds */
   1617 		XP_EQ(exp_mode, ai.mode);
   1618 	}
   1619 	/* ai.play */
   1620 	XP_EQ(exp_sample_rate, ai.play.sample_rate);
   1621 	XP_EQ(exp_channels, ai.play.channels);
   1622 	XP_EQ(exp_precision, ai.play.precision);
   1623 	XP_EQ(exp_encoding, ai.play.encoding);
   1624 		/* gain */
   1625 		/* port */
   1626 	XP_EQ(0, ai.play.seek);
   1627 		/* avail_ports */
   1628 	XP_NE(0, ai.play.buffer_size);
   1629 	XP_EQ(0, ai.play.samples);
   1630 	XP_EQ(0, ai.play.eof);
   1631 	XP_EQ(exp_pause, ai.play.pause);
   1632 	XP_EQ(0, ai.play.error);
   1633 	XP_EQ(0, ai.play.waiting);
   1634 		/* balance */
   1635 	XP_EQ(exp_popen, ai.play.open);
   1636 	XP_EQ(0, ai.play.active);
   1637 	/* ai.record */
   1638 	XP_EQ(exp_sample_rate, ai.record.sample_rate);
   1639 	XP_EQ(exp_channels, ai.record.channels);
   1640 	XP_EQ(exp_precision, ai.record.precision);
   1641 	XP_EQ(exp_encoding, ai.record.encoding);
   1642 		/* gain */
   1643 		/* port */
   1644 	XP_EQ(0, ai.record.seek);
   1645 		/* avail_ports */
   1646 	XP_NE(0, ai.record.buffer_size);
   1647 	XP_EQ(0, ai.record.samples);
   1648 	XP_EQ(0, ai.record.eof);
   1649 	XP_EQ(exp_pause, ai.record.pause);
   1650 	XP_EQ(0, ai.record.error);
   1651 	XP_EQ(0, ai.record.waiting);
   1652 		/* balance */
   1653 	XP_EQ(exp_ropen, ai.record.open);
   1654 	if (netbsd < 9 && strcmp(devname, "sound") == 0) {
   1655 		/*
   1656 		 * On NetBSD7/8, it doesn't seem to start recording on open
   1657 		 * for /dev/sound.  It should be a bug.
   1658 		 */
   1659 		XP_EQ(0, ai.record.active);
   1660 	} else {
   1661 		XP_EQ(exp_ropen, ai.record.active);
   1662 	}
   1663 
   1664 	r = CLOSE(fd);
   1665 	REQUIRED_SYS_EQ(0, r);
   1666 }
   1667 DEF(open_audio_RDONLY)		{ test_open("audio", O_RDONLY); }
   1668 DEF(open_audio_WRONLY)		{ test_open("audio", O_WRONLY); }
   1669 DEF(open_audio_RDWR)		{ test_open("audio", O_RDWR);   }
   1670 DEF(open_sound_RDONLY)		{ test_open("sound", O_RDONLY); }
   1671 DEF(open_sound_WRONLY)		{ test_open("sound", O_WRONLY); }
   1672 DEF(open_sound_RDWR)		{ test_open("sound", O_RDWR);   }
   1673 DEF(open_audioctl_RDONLY)	{ test_open("audioctl", O_RDONLY); }
   1674 DEF(open_audioctl_WRONLY)	{ test_open("audioctl", O_WRONLY); }
   1675 DEF(open_audioctl_RDWR)		{ test_open("audioctl", O_RDWR);   }
   1676 
   1677 /*
   1678  * Open (1) /dev/sound -> (2) /dev/audio -> (3) /dev/sound,
   1679  * Both of /dev/audio and /dev/sound share the sticky parameters,
   1680  * /dev/sound inherits and use it but /dev/audio initialize and use it.
   1681  * So 2nd audio descriptor affects 3rd sound descriptor.
   1682  */
   1683 DEF(open_sound_sticky)
   1684 {
   1685 	struct audio_info ai;
   1686 	int fd;
   1687 	int r;
   1688 	int openmode;
   1689 
   1690 	TEST("open_sound_sticky");
   1691 
   1692 	openmode = openable_mode();
   1693 
   1694 	/* First, open /dev/sound and change encoding as a delegate */
   1695 	fd = OPEN(devsound, openmode);
   1696 	REQUIRED_SYS_OK(fd);
   1697 	AUDIO_INITINFO(&ai);
   1698 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1699 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1700 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   1701 	REQUIRED_SYS_EQ(0, r);
   1702 	r = CLOSE(fd);
   1703 	REQUIRED_SYS_EQ(0, r);
   1704 
   1705 	/* Next, open /dev/audio.  It makes the encoding mulaw */
   1706 	fd = OPEN(devaudio, openmode);
   1707 	REQUIRED_SYS_OK(fd);
   1708 	r = CLOSE(fd);
   1709 	REQUIRED_SYS_EQ(0, r);
   1710 
   1711 	/* And then, open /dev/sound again */
   1712 	fd = OPEN(devsound, openmode);
   1713 	REQUIRED_SYS_OK(fd);
   1714 	memset(&ai, 0, sizeof(ai));
   1715 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1716 	REQUIRED_SYS_EQ(0, r);
   1717 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1718 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1719 	r = CLOSE(fd);
   1720 	REQUIRED_SYS_EQ(0, r);
   1721 }
   1722 
   1723 /*
   1724  * /dev/audioctl has stickiness like /dev/sound.
   1725  */
   1726 DEF(open_audioctl_sticky)
   1727 {
   1728 	struct audio_info ai;
   1729 	int fd;
   1730 	int r;
   1731 	int openmode;
   1732 
   1733 	TEST("open_audioctl_sticky");
   1734 
   1735 	openmode = openable_mode();
   1736 
   1737 	/* First, open /dev/audio and change encoding */
   1738 	fd = OPEN(devaudio, openmode);
   1739 	REQUIRED_SYS_OK(fd);
   1740 	AUDIO_INITINFO(&ai);
   1741 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1742 	ai.play.precision = 16;
   1743 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
   1744 	ai.record.precision = 16;
   1745 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "SLINEAR_LE");
   1746 	REQUIRED_SYS_EQ(0, r);
   1747 	r = CLOSE(fd);
   1748 	REQUIRED_SYS_EQ(0, r);
   1749 
   1750 	/* Next, open /dev/audioctl.  It should be affected */
   1751 	fd = OPEN(devaudioctl, openmode);
   1752 	REQUIRED_SYS_OK(fd);
   1753 	memset(&ai, 0, sizeof(ai));
   1754 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1755 	REQUIRED_SYS_EQ(0, r);
   1756 	XP_EQ(AUDIO_ENCODING_SLINEAR_LE, ai.play.encoding);
   1757 	XP_EQ(16, ai.play.precision);
   1758 	XP_EQ(AUDIO_ENCODING_SLINEAR_LE, ai.record.encoding);
   1759 	XP_EQ(16, ai.record.precision);
   1760 
   1761 	/* Then, change /dev/audioctl */
   1762 	AUDIO_INITINFO(&ai);
   1763 	ai.play.encoding = AUDIO_ENCODING_ULAW;
   1764 	ai.play.precision = 8;
   1765 	ai.record.encoding = AUDIO_ENCODING_ULAW;
   1766 	ai.record.precision = 8;
   1767 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "ULAW");
   1768 	REQUIRED_SYS_EQ(0, r);
   1769 	r = CLOSE(fd);
   1770 	REQUIRED_SYS_EQ(0, r);
   1771 
   1772 	/* Finally, open /dev/sound.  It also should be affected  */
   1773 	fd = OPEN(devsound, openmode);
   1774 	REQUIRED_SYS_OK(fd);
   1775 	memset(&ai, 0, sizeof(ai));
   1776 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   1777 	REQUIRED_SYS_EQ(0, r);
   1778 	XP_EQ(AUDIO_ENCODING_ULAW, ai.play.encoding);
   1779 	XP_EQ(8, ai.play.precision);
   1780 	XP_EQ(AUDIO_ENCODING_ULAW, ai.record.encoding);
   1781 	XP_EQ(8, ai.record.precision);
   1782 	r = CLOSE(fd);
   1783 	REQUIRED_SYS_EQ(0, r);
   1784 }
   1785 
   1786 /*
   1787  * Open two descriptors simultaneously.
   1788  */
   1789 void
   1790 test_open_simul(int mode0, int mode1)
   1791 {
   1792 	struct audio_info ai;
   1793 	int fd0, fd1;
   1794 	int i;
   1795 	int r;
   1796 	int actmode;
   1797 #define AUMODE_BOTH (AUMODE_PLAY | AUMODE_RECORD)
   1798 	struct {
   1799 		int mode0;
   1800 		int mode1;
   1801 	} expfulltable[] = {
   1802 		/* expected fd0		expected fd1 (-errno expects error) */
   1803 		{ AUMODE_RECORD,	AUMODE_RECORD },	// REC, REC
   1804 		{ AUMODE_RECORD,	AUMODE_PLAY },		// REC, PLAY
   1805 		{ AUMODE_RECORD,	AUMODE_BOTH },		// REC, BOTH
   1806 		{ AUMODE_PLAY,		AUMODE_RECORD },	// PLAY, REC
   1807 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, PLAY
   1808 		{ AUMODE_PLAY,		AUMODE_BOTH },		// PLAY, BOTH
   1809 		{ AUMODE_BOTH,		AUMODE_RECORD },	// BOTH, REC
   1810 		{ AUMODE_BOTH,		AUMODE_PLAY },		// BOTH, PLAY
   1811 		{ AUMODE_BOTH,		AUMODE_BOTH },		// BOTH, BOTH
   1812 	},
   1813 	exphalftable[] = {
   1814 		/* expected fd0		expected fd1 (-errno expects error) */
   1815 		{ AUMODE_RECORD,	AUMODE_RECORD },	// REC, REC
   1816 		{ AUMODE_RECORD,	-ENODEV },		// REC, PLAY
   1817 		{ AUMODE_RECORD,	-ENODEV },		// REC, BOTH
   1818 		{ AUMODE_PLAY,		-ENODEV },		// PLAY, REC
   1819 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, PLAY
   1820 		{ AUMODE_PLAY,		AUMODE_PLAY },		// PLAY, BOTH
   1821 		{ AUMODE_PLAY,		-ENODEV },		// BOTH, REC
   1822 		{ AUMODE_PLAY,		AUMODE_PLAY },		// BOTH, PLAY
   1823 		{ AUMODE_PLAY,		AUMODE_PLAY },		// BOTH, BOTH
   1824 	}, *exptable;
   1825 
   1826 	/* The expected values are different in half-duplex or full-duplex */
   1827 	if (hw_fulldup()) {
   1828 		exptable = expfulltable;
   1829 	} else {
   1830 		exptable = exphalftable;
   1831 	}
   1832 
   1833 	TEST("open_simul_%s_%s",
   1834 	    openmode_str[mode0] + 2,
   1835 	    openmode_str[mode1] + 2);
   1836 
   1837 	if (netbsd < 8) {
   1838 		XP_SKIP("Multiple open is not supported");
   1839 		return;
   1840 	}
   1841 
   1842 	if (mode2aumode(mode0) == 0 || mode2aumode(mode1) == 0) {
   1843 		XP_SKIP("Operation not allowed on this hardware property");
   1844 		return;
   1845 	}
   1846 
   1847 	i = mode0 * 3 + mode1;
   1848 
   1849 	/* Open first one */
   1850 	fd0 = OPEN(devaudio, mode0);
   1851 	REQUIRED_SYS_OK(fd0);
   1852 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "");
   1853 	REQUIRED_SYS_EQ(0, r);
   1854 	actmode = ai.mode & AUMODE_BOTH;
   1855 	XP_EQ(exptable[i].mode0, actmode);
   1856 
   1857 	/* Open second one */
   1858 	fd1 = OPEN(devaudio, mode1);
   1859 	if (exptable[i].mode1 >= 0) {
   1860 		/* Case to expect to be able to open */
   1861 		REQUIRED_SYS_OK(fd1);
   1862 		r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   1863 		XP_SYS_EQ(0, r);
   1864 		if (r == 0) {
   1865 			actmode = ai.mode & AUMODE_BOTH;
   1866 			XP_EQ(exptable[i].mode1, actmode);
   1867 		}
   1868 	} else {
   1869 		/* Case to expect not to be able to open */
   1870 		XP_SYS_NG(ENODEV, fd1);
   1871 		if (fd1 == -1) {
   1872 			XP_EQ(-exptable[i].mode1, errno);
   1873 		} else {
   1874 			r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   1875 			XP_SYS_EQ(0, r);
   1876 			if (r == 0) {
   1877 				actmode = ai.mode & AUMODE_BOTH;
   1878 				XP_FAIL("expects error but %d", actmode);
   1879 			}
   1880 		}
   1881 	}
   1882 
   1883 	if (fd1 >= 0) {
   1884 		r = CLOSE(fd1);
   1885 		XP_SYS_EQ(0, r);
   1886 	}
   1887 
   1888 	r = CLOSE(fd0);
   1889 	XP_SYS_EQ(0, r);
   1890 }
   1891 DEF(open_simul_RDONLY_RDONLY)	{ test_open_simul(O_RDONLY, O_RDONLY);	}
   1892 DEF(open_simul_RDONLY_WRONLY)	{ test_open_simul(O_RDONLY, O_WRONLY);	}
   1893 DEF(open_simul_RDONLY_RDWR)	{ test_open_simul(O_RDONLY, O_RDWR);	}
   1894 DEF(open_simul_WRONLY_RDONLY)	{ test_open_simul(O_WRONLY, O_RDONLY);	}
   1895 DEF(open_simul_WRONLY_WRONLY)	{ test_open_simul(O_WRONLY, O_WRONLY);	}
   1896 DEF(open_simul_WRONLY_RDWR)	{ test_open_simul(O_WRONLY, O_RDWR);	}
   1897 DEF(open_simul_RDWR_RDONLY)	{ test_open_simul(O_RDWR, O_RDONLY);	}
   1898 DEF(open_simul_RDWR_WRONLY)	{ test_open_simul(O_RDWR, O_WRONLY);	}
   1899 DEF(open_simul_RDWR_RDWR)	{ test_open_simul(O_RDWR, O_RDWR);	}
   1900 
   1901 /*
   1902  * /dev/audio can be opened by other user who opens /dev/audio.
   1903  */
   1904 void
   1905 try_open_multiuser(bool multiuser)
   1906 {
   1907 	int fd0;
   1908 	int fd1;
   1909 	int r;
   1910 	uid_t ouid;
   1911 
   1912 	/*
   1913 	 * Test1: Open as root first and then unprivileged user.
   1914 	 */
   1915 
   1916 	/* At first, open as root */
   1917 	fd0 = OPEN(devaudio, openable_mode());
   1918 	REQUIRED_SYS_OK(fd0);
   1919 
   1920 	ouid = GETUID();
   1921 	r = SETEUID(1);
   1922 	REQUIRED_SYS_EQ(0, r);
   1923 
   1924 	/* Then, open as unprivileged user */
   1925 	fd1 = OPEN(devaudio, openable_mode());
   1926 	if (multiuser) {
   1927 		/* If multiuser, another user also can open */
   1928 		XP_SYS_OK(fd1);
   1929 	} else {
   1930 		/* If not multiuser, another user cannot open */
   1931 		XP_SYS_NG(EPERM, fd1);
   1932 	}
   1933 	if (fd1 != -1) {
   1934 		r = CLOSE(fd1);
   1935 		XP_SYS_EQ(0, r);
   1936 	}
   1937 
   1938 	r = SETEUID(ouid);
   1939 	REQUIRED_SYS_EQ(0, r);
   1940 
   1941 	r = CLOSE(fd0);
   1942 	XP_SYS_EQ(0, r);
   1943 
   1944 	/*
   1945 	 * Test2: Open as unprivileged user first and then root.
   1946 	 */
   1947 
   1948 	/* At first, open as unprivileged user */
   1949 	ouid = GETUID();
   1950 	r = SETEUID(1);
   1951 	REQUIRED_SYS_EQ(0, r);
   1952 
   1953 	fd0 = OPEN(devaudio, openable_mode());
   1954 	REQUIRED_SYS_OK(fd0);
   1955 
   1956 	/* Then open as root */
   1957 	r = SETEUID(ouid);
   1958 	REQUIRED_SYS_EQ(0, r);
   1959 
   1960 	/* root always can open */
   1961 	fd1 = OPEN(devaudio, openable_mode());
   1962 	XP_SYS_OK(fd1);
   1963 	if (fd1 != -1) {
   1964 		r = CLOSE(fd1);
   1965 		XP_SYS_EQ(0, r);
   1966 	}
   1967 
   1968 	/* Close first one as unprivileged user */
   1969 	r = SETEUID(1);
   1970 	REQUIRED_SYS_EQ(0, r);
   1971 	r = CLOSE(fd0);
   1972 	XP_SYS_EQ(0, r);
   1973 	r = SETEUID(ouid);
   1974 	REQUIRED_SYS_EQ(0, r);
   1975 }
   1976 /*
   1977  * This is a wrapper for open_multiuser.
   1978  * XXX XP_* macros are not compatible with on-error-goto, we need try-catch...
   1979  */
   1980 void
   1981 test_open_multiuser(bool multiuser)
   1982 {
   1983 	char mibname[32];
   1984 	bool oldval;
   1985 	size_t oldlen;
   1986 	int r;
   1987 
   1988 	TEST("open_multiuser_%d", multiuser);
   1989 	if (netbsd < 8) {
   1990 		XP_SKIP("Multiple open is not supported");
   1991 		return;
   1992 	}
   1993 	if (netbsd < 9) {
   1994 		/* NetBSD8 has no way (difficult) to determine device name */
   1995 		XP_SKIP("NetBSD8 cannot determine device name");
   1996 		return;
   1997 	}
   1998 	if (geteuid() != 0) {
   1999 		XP_SKIP("Must be run as a privileged user");
   2000 		return;
   2001 	}
   2002 
   2003 	/* Get current multiuser mode (and save it) */
   2004 	snprintf(mibname, sizeof(mibname), "hw.%s.multiuser", devicename);
   2005 	oldlen = sizeof(oldval);
   2006 	r = SYSCTLBYNAME(mibname, &oldval, &oldlen, NULL, 0);
   2007 	REQUIRED_SYS_EQ(0, r);
   2008 	DPRINTF("  > multiuser=%d\n", oldval);
   2009 
   2010 	/* Change if necessary */
   2011 	if (oldval != multiuser) {
   2012 		r = SYSCTLBYNAME(mibname, NULL, NULL, &multiuser,
   2013 		    sizeof(multiuser));
   2014 		REQUIRED_SYS_EQ(0, r);
   2015 		DPRINTF("  > new multiuser=%d\n", multiuser);
   2016 	}
   2017 
   2018 	/* Do test */
   2019 	try_open_multiuser(multiuser);
   2020 
   2021 	/* Restore multiuser mode */
   2022 	if (oldval != multiuser) {
   2023 		DPRINTF("  > restore multiuser to %d\n", oldval);
   2024 		r = SYSCTLBYNAME(mibname, NULL, NULL, &oldval, sizeof(oldval));
   2025 		REQUIRED_SYS_EQ(0, r);
   2026 	}
   2027 }
   2028 DEF(open_multiuser_0)	{ test_open_multiuser(false); }
   2029 DEF(open_multiuser_1)	{ test_open_multiuser(true); }
   2030 
   2031 /*
   2032  * Normal playback (with PLAY_ALL).
   2033  * It does not verify real playback data.
   2034  */
   2035 DEF(write_PLAY_ALL)
   2036 {
   2037 	char buf[8000];
   2038 	int fd;
   2039 	int r;
   2040 
   2041 	TEST("write_PLAY_ALL");
   2042 
   2043 	fd = OPEN(devaudio, O_WRONLY);
   2044 	if (hw_canplay()) {
   2045 		REQUIRED_SYS_OK(fd);
   2046 	} else {
   2047 		XP_SYS_NG(ENXIO, fd);
   2048 		return;
   2049 	}
   2050 
   2051 	/* mulaw 1sec silence */
   2052 	memset(buf, 0xff, sizeof(buf));
   2053 	r = WRITE(fd, buf, sizeof(buf));
   2054 	XP_SYS_EQ(sizeof(buf), r);
   2055 
   2056 	r = CLOSE(fd);
   2057 	XP_SYS_EQ(0, r);
   2058 }
   2059 
   2060 /*
   2061  * Normal playback (without PLAY_ALL).
   2062  * It does not verify real playback data.
   2063  */
   2064 DEF(write_PLAY)
   2065 {
   2066 	struct audio_info ai;
   2067 	char *wav;
   2068 	int wavsize;
   2069 	int totalsize;
   2070 	int fd;
   2071 	int r;
   2072 
   2073 	TEST("write_PLAY");
   2074 
   2075 	fd = OPEN(devaudio, O_WRONLY);
   2076 	if (hw_canplay()) {
   2077 		REQUIRED_SYS_OK(fd);
   2078 	} else {
   2079 		XP_SYS_NG(ENXIO, fd);
   2080 		return;
   2081 	}
   2082 
   2083 	/* Drop PLAY_ALL */
   2084 	AUDIO_INITINFO(&ai);
   2085 	ai.mode = AUMODE_PLAY;
   2086 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "mode");
   2087 	REQUIRED_SYS_EQ(0, r);
   2088 
   2089 	/* Check mode and get blocksize */
   2090 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2091 	REQUIRED_SYS_EQ(0, r);
   2092 	XP_EQ(AUMODE_PLAY, ai.mode);
   2093 
   2094 	wavsize = ai.blocksize;
   2095 	wav = (char *)malloc(wavsize);
   2096 	REQUIRED_IF(wav != NULL);
   2097 	memset(wav, 0xff, wavsize);
   2098 
   2099 	/* Write blocks until 1sec */
   2100 	for (totalsize = 0; totalsize < 8000; ) {
   2101 		r = WRITE(fd, wav, wavsize);
   2102 		XP_SYS_EQ(wavsize, r);
   2103 		if (r == -1)
   2104 			break;	/* XXX */
   2105 		totalsize += r;
   2106 	}
   2107 
   2108 	/* XXX What should I test it? */
   2109 	/* Check ai.play.error */
   2110 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2111 	REQUIRED_SYS_EQ(0, r);
   2112 	XP_EQ(0, ai.play.error);
   2113 
   2114 	/* Playback data is no longer necessary */
   2115 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   2116 	REQUIRED_SYS_EQ(0, r);
   2117 
   2118 	r = CLOSE(fd);
   2119 	REQUIRED_SYS_EQ(0, r);
   2120 
   2121 	free(wav);
   2122 }
   2123 
   2124 /*
   2125  * Normal recording.
   2126  * It does not verify real recorded data.
   2127  */
   2128 DEF(read)
   2129 {
   2130 	char buf[8000];
   2131 	int fd;
   2132 	int r;
   2133 
   2134 	TEST("read");
   2135 
   2136 	fd = OPEN(devaudio, O_RDONLY);
   2137 	if (hw_canrec()) {
   2138 		REQUIRED_SYS_OK(fd);
   2139 	} else {
   2140 		XP_SYS_NG(ENXIO, fd);
   2141 		return;
   2142 	}
   2143 
   2144 	/* mulaw 1sec */
   2145 	r = READ(fd, buf, sizeof(buf));
   2146 	XP_SYS_EQ(sizeof(buf), r);
   2147 
   2148 	r = CLOSE(fd);
   2149 	XP_SYS_EQ(0, r);
   2150 }
   2151 
   2152 /*
   2153  * Repeat open-write-close cycle.
   2154  */
   2155 DEF(rept_write)
   2156 {
   2157 	struct timeval start, end, result;
   2158 	double res;
   2159 	char buf[8000];	/* 1sec in 8bit-mulaw,1ch,8000Hz */
   2160 	int fd;
   2161 	int r;
   2162 	int n;
   2163 
   2164 	TEST("rept_write");
   2165 
   2166 	if (hw_canplay() == 0) {
   2167 		XP_SKIP("This test is only for playable device");
   2168 		return;
   2169 	}
   2170 
   2171 	/* XXX It may timeout on some hardware driver. */
   2172 	XP_SKIP("not yet");
   2173 	return;
   2174 
   2175 	memset(buf, 0xff, sizeof(buf));
   2176 	n = 3;
   2177 	gettimeofday(&start, NULL);
   2178 	for (int i = 0; i < n; i++) {
   2179 		fd = OPEN(devaudio, O_WRONLY);
   2180 		REQUIRED_SYS_OK(fd);
   2181 
   2182 		r = WRITE(fd, buf, sizeof(buf));
   2183 		XP_SYS_EQ(sizeof(buf), r);
   2184 
   2185 		r = CLOSE(fd);
   2186 		XP_SYS_EQ(0, r);
   2187 	}
   2188 	gettimeofday(&end, NULL);
   2189 	timersub(&end, &start, &result);
   2190 	res = (double)result.tv_sec + (double)result.tv_usec / 1000000;
   2191 	/* Make judgement but not too strict */
   2192 	if (res >= n * 1.5) {
   2193 		XP_FAIL("expects %d sec but %4.1f sec", n, res);
   2194 		return;
   2195 	}
   2196 }
   2197 
   2198 /*
   2199  * Repeat open-read-close cycle.
   2200  */
   2201 DEF(rept_read)
   2202 {
   2203 	struct timeval start, end, result;
   2204 	double res;
   2205 	char buf[8000];	/* 1sec in 8bit-mulaw,1ch,8000Hz */
   2206 	int fd;
   2207 	int r;
   2208 	int n;
   2209 
   2210 	TEST("rept_read");
   2211 
   2212 	if (hw_canrec() == 0) {
   2213 		XP_SKIP("This test is only for recordable device");
   2214 		return;
   2215 	}
   2216 
   2217 	/* XXX It may timeout on some hardware driver. */
   2218 	XP_SKIP("not yet");
   2219 	return;
   2220 
   2221 	n = 3;
   2222 	gettimeofday(&start, NULL);
   2223 	for (int i = 0; i < n; i++) {
   2224 		fd = OPEN(devaudio, O_RDONLY);
   2225 		REQUIRED_SYS_OK(fd);
   2226 
   2227 		r = READ(fd, buf, sizeof(buf));
   2228 		XP_SYS_EQ(sizeof(buf), r);
   2229 
   2230 		r = CLOSE(fd);
   2231 		XP_SYS_EQ(0, r);
   2232 	}
   2233 	gettimeofday(&end, NULL);
   2234 	timersub(&end, &start, &result);
   2235 	res = (double)result.tv_sec + (double)result.tv_usec / 1000000;
   2236 	/* Make judgement but not too strict */
   2237 	if (res >= n * 1.5) {
   2238 		XP_FAIL("expects %d sec but %4.1f sec", n, res);
   2239 		return;
   2240 	}
   2241 }
   2242 
   2243 /*
   2244  * Opening with O_RDWR on half-duplex hardware falls back to O_WRONLY.
   2245  * expwrite: expected to be able to play.
   2246  * expread : expected to be able to record.
   2247  */
   2248 void
   2249 test_rdwr_fallback(int openmode, bool expwrite, bool expread)
   2250 {
   2251 	struct audio_info ai;
   2252 	char buf[10];
   2253 	int fd;
   2254 	int r;
   2255 
   2256 	TEST("rdwr_fallback_%s", openmode_str[openmode] + 2);
   2257 
   2258 	if (hw_bidir() == 0) {
   2259 		XP_SKIP("This test is only for bi-directional device");
   2260 		return;
   2261 	}
   2262 
   2263 	AUDIO_INITINFO(&ai);
   2264 	ai.play.pause = 1;
   2265 	ai.record.pause = 1;
   2266 
   2267 	fd = OPEN(devaudio, openmode);
   2268 	REQUIRED_SYS_OK(fd);
   2269 
   2270 	/* Set pause not to play noise */
   2271 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   2272 	REQUIRED_SYS_EQ(0, r);
   2273 
   2274 	memset(buf, 0xff, sizeof(buf));
   2275 	r = WRITE(fd, buf, sizeof(buf));
   2276 	if (expwrite) {
   2277 		XP_SYS_EQ(sizeof(buf), r);
   2278 	} else {
   2279 		XP_SYS_NG(EBADF, r);
   2280 	}
   2281 
   2282 	r = READ(fd, buf, 0);
   2283 	if (expread) {
   2284 		XP_SYS_EQ(0, r);
   2285 	} else {
   2286 		XP_SYS_NG(EBADF, r);
   2287 	}
   2288 
   2289 	r = CLOSE(fd);
   2290 	REQUIRED_SYS_EQ(0, r);
   2291 }
   2292 DEF(rdwr_fallback_RDONLY) { test_rdwr_fallback(O_RDONLY, false, true); }
   2293 DEF(rdwr_fallback_WRONLY) { test_rdwr_fallback(O_WRONLY, true, false); }
   2294 DEF(rdwr_fallback_RDWR) {
   2295 	bool expread;
   2296 	/*
   2297 	 * On NetBSD7, O_RDWR on half-duplex is accepted. It's possible to
   2298 	 * read and write if they don't occur at the same time.
   2299 	 * On NetBSD9, O_RDWR on half-duplex falls back O_WRONLY.
   2300 	 */
   2301 	if (netbsd < 8) {
   2302 		expread = true;
   2303 	} else {
   2304 		expread = hw_fulldup() ? true : false;
   2305 	}
   2306 	test_rdwr_fallback(O_RDWR, true, expread);
   2307 }
   2308 
   2309 /*
   2310  * On full-duplex hardware, the second descriptor's readablity/writability
   2311  * is not depend on the first descriptor('s open mode).
   2312  * On half-duplex hardware, it depends on the first descriptor's open mode.
   2313  */
   2314 void
   2315 test_rdwr_two(int mode0, int mode1)
   2316 {
   2317 	struct audio_info ai;
   2318 	char wbuf[100];	/* 1/80sec in 8bit-mulaw,1ch,8000Hz */
   2319 	char rbuf[100];	/* 1/80sec in 8bit-mulaw,1ch,8000Hz */
   2320 	bool canopen;
   2321 	bool canwrite;
   2322 	bool canread;
   2323 	int fd0;
   2324 	int fd1;
   2325 	int r;
   2326 	struct {
   2327 		bool canopen;
   2328 		bool canwrite;
   2329 		bool canread;
   2330 	} exptable_full[] = {
   2331 	/*	open write read	   1st, 2nd mode */
   2332 		{ 1, 0, 1 },	/* REC, REC */
   2333 		{ 1, 1, 0 },	/* REC, PLAY */
   2334 		{ 1, 1, 1 },	/* REC, BOTH */
   2335 		{ 1, 0, 1 },	/* PLAY, REC */
   2336 		{ 1, 1, 0 },	/* PLAY, PLAY */
   2337 		{ 1, 1, 1 },	/* PLAY, BOTH */
   2338 		{ 1, 0, 1 },	/* BOTH, REC */
   2339 		{ 1, 1, 0 },	/* BOTH, PLAY */
   2340 		{ 1, 1, 1 },	/* BOTH, BOTH */
   2341 	},
   2342 	exptable_half[] = {
   2343 		{ 1, 0, 1 },	/* REC, REC */
   2344 		{ 0, 0, 0 },	/* REC, PLAY */
   2345 		{ 0, 0, 0 },	/* REC, BOTH */
   2346 		{ 0, 0, 0 },	/* PLAY, REC */
   2347 		{ 1, 1, 0 },	/* PLAY, PLAY */
   2348 		{ 1, 1, 0 },	/* PLAY, BOTH */
   2349 		{ 0, 0, 0 },	/* BOTH, REC */
   2350 		{ 1, 1, 0 },	/* BOTH, PLAY */
   2351 		{ 0, 0, 0 },	/* BOTH, BOTH */
   2352 	}, *exptable;
   2353 
   2354 	TEST("rdwr_two_%s_%s",
   2355 	    openmode_str[mode0] + 2,
   2356 	    openmode_str[mode1] + 2);
   2357 
   2358 	if (netbsd < 8) {
   2359 		XP_SKIP("Multiple open is not supported");
   2360 		return;
   2361 	}
   2362 	if (hw_bidir() == 0) {
   2363 		XP_SKIP("This test is only for bi-directional device");
   2364 		return;
   2365 	}
   2366 
   2367 	exptable = hw_fulldup() ? exptable_full : exptable_half;
   2368 
   2369 	canopen  = exptable[mode0 * 3 + mode1].canopen;
   2370 	canwrite = exptable[mode0 * 3 + mode1].canwrite;
   2371 	canread  = exptable[mode0 * 3 + mode1].canread;
   2372 
   2373 	if (!canopen) {
   2374 		XP_SKIP("This combination is not openable on half-duplex");
   2375 		return;
   2376 	}
   2377 
   2378 	fd0 = OPEN(devaudio, mode0);
   2379 	REQUIRED_SYS_OK(fd0);
   2380 
   2381 	fd1 = OPEN(devaudio, mode1);
   2382 	REQUIRED_SYS_OK(fd1);
   2383 
   2384 	/* Silent data to make no sound */
   2385 	memset(&wbuf, 0xff, sizeof(wbuf));
   2386 	/* Pause to make no sound */
   2387 	AUDIO_INITINFO(&ai);
   2388 	ai.play.pause = 1;
   2389 	r = IOCTL(fd0, AUDIO_SETINFO, &ai, "pause");
   2390 	XP_SYS_EQ(0, r);
   2391 
   2392 	/* write(fd1) */
   2393 	r = WRITE(fd1, wbuf, sizeof(wbuf));
   2394 	if (canwrite) {
   2395 		XP_SYS_EQ(100, r);
   2396 	} else {
   2397 		XP_SYS_NG(EBADF, r);
   2398 	}
   2399 
   2400 	/* read(fd1) */
   2401 	r = READ(fd1, rbuf, sizeof(rbuf));
   2402 	if (canread) {
   2403 		XP_SYS_EQ(100, r);
   2404 	} else {
   2405 		XP_SYS_NG(EBADF, r);
   2406 	}
   2407 
   2408 	r = CLOSE(fd0);
   2409 	XP_SYS_EQ(0, r);
   2410 	r = CLOSE(fd1);
   2411 	XP_SYS_EQ(0, r);
   2412 }
   2413 DEF(rdwr_two_RDONLY_RDONLY)	{ test_rdwr_two(O_RDONLY, O_RDONLY);	}
   2414 DEF(rdwr_two_RDONLY_WRONLY)	{ test_rdwr_two(O_RDONLY, O_WRONLY);	}
   2415 DEF(rdwr_two_RDONLY_RDWR)	{ test_rdwr_two(O_RDONLY, O_RDWR);	}
   2416 DEF(rdwr_two_WRONLY_RDONLY)	{ test_rdwr_two(O_WRONLY, O_RDONLY);	}
   2417 DEF(rdwr_two_WRONLY_WRONLY)	{ test_rdwr_two(O_WRONLY, O_WRONLY);	}
   2418 DEF(rdwr_two_WRONLY_RDWR)	{ test_rdwr_two(O_WRONLY, O_RDWR);	}
   2419 DEF(rdwr_two_RDWR_RDONLY)	{ test_rdwr_two(O_RDWR, O_RDONLY);	}
   2420 DEF(rdwr_two_RDWR_WRONLY)	{ test_rdwr_two(O_RDWR, O_WRONLY);	}
   2421 DEF(rdwr_two_RDWR_RDWR)		{ test_rdwr_two(O_RDWR, O_RDWR);	}
   2422 
   2423 /*
   2424  * Read and write different descriptors simultaneously.
   2425  * Only on full-duplex.
   2426  */
   2427 DEF(rdwr_simul)
   2428 {
   2429 	char wbuf[1000];	/* 1/8sec in mulaw,1ch,8kHz */
   2430 	char rbuf[1000];
   2431 	int fd0;
   2432 	int fd1;
   2433 	int r;
   2434 	int status;
   2435 	pid_t pid;
   2436 
   2437 	TEST("rdwr_simul");
   2438 	if (netbsd < 8) {
   2439 		XP_SKIP("Multiple open is not supported");
   2440 		return;
   2441 	}
   2442 	if (!hw_fulldup()) {
   2443 		XP_SKIP("This test is only for full-duplex device");
   2444 		return;
   2445 	}
   2446 
   2447 	/* Silence data to make no sound */
   2448 	memset(wbuf, 0xff, sizeof(wbuf));
   2449 
   2450 	fd0 = OPEN(devaudio, O_WRONLY);
   2451 	REQUIRED_SYS_OK(fd0);
   2452 	fd1 = OPEN(devaudio, O_RDONLY);
   2453 	REQUIRED_SYS_OK(fd1);
   2454 
   2455 	fflush(stdout);
   2456 	fflush(stderr);
   2457 	pid = fork();
   2458 	if (pid == -1)
   2459 		xp_err(1, __LINE__, "fork");
   2460 
   2461 	if (pid == 0) {
   2462 		/* child (read) */
   2463 		for (int i = 0; i < 10; i++) {
   2464 			r = READ(fd1, rbuf, sizeof(rbuf));
   2465 			if (r == -1)
   2466 				xp_err(1, __LINE__, "read(i=%d)", i);
   2467 		}
   2468 		exit(0);
   2469 	} else {
   2470 		/* parent (write) */
   2471 		for (int i = 0; i < 10; i++) {
   2472 			r = WRITE(fd0, wbuf, sizeof(wbuf));
   2473 			if (r == -1)
   2474 				xp_err(1, __LINE__, "write(i=%d)", i);
   2475 		}
   2476 		waitpid(pid, &status, 0);
   2477 	}
   2478 
   2479 	CLOSE(fd0);
   2480 	CLOSE(fd1);
   2481 	/* If you reach here, consider as success */
   2482 	XP_EQ(0, 0);
   2483 }
   2484 
   2485 /*
   2486  * DRAIN should work even on incomplete data left.
   2487  */
   2488 DEF(drain_incomplete)
   2489 {
   2490 	struct audio_info ai;
   2491 	int r;
   2492 	int fd;
   2493 
   2494 	TEST("drain_incomplete");
   2495 
   2496 	if (hw_canplay() == 0) {
   2497 		XP_SKIP("This test is only for playable device");
   2498 		return;
   2499 	}
   2500 
   2501 	fd = OPEN(devaudio, O_WRONLY);
   2502 	REQUIRED_SYS_OK(fd);
   2503 
   2504 	AUDIO_INITINFO(&ai);
   2505 	/* let precision > 8 */
   2506 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   2507 	ai.play.precision = 16;
   2508 	ai.mode = AUMODE_PLAY;
   2509 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   2510 	REQUIRED_SYS_EQ(0, r);
   2511 	/* Write one byte and then close */
   2512 	r = WRITE(fd, &r, 1);
   2513 	XP_SYS_EQ(1, r);
   2514 	r = CLOSE(fd);
   2515 	XP_SYS_EQ(0, r);
   2516 }
   2517 
   2518 /*
   2519  * DRAIN should work even in pause.
   2520  */
   2521 DEF(drain_pause)
   2522 {
   2523 	struct audio_info ai;
   2524 	int r;
   2525 	int fd;
   2526 
   2527 	TEST("drain_pause");
   2528 
   2529 	if (hw_canplay() == 0) {
   2530 		XP_SKIP("This test is only for playable device");
   2531 		return;
   2532 	}
   2533 
   2534 	fd = OPEN(devaudio, O_WRONLY);
   2535 	REQUIRED_SYS_OK(fd);
   2536 
   2537 	/* Set pause */
   2538 	AUDIO_INITINFO(&ai);
   2539 	ai.play.pause = 1;
   2540 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   2541 	XP_SYS_EQ(0, r);
   2542 	/* Write some data and then close */
   2543 	r = WRITE(fd, &r, 4);
   2544 	XP_SYS_EQ(4, r);
   2545 	r = CLOSE(fd);
   2546 	XP_SYS_EQ(0, r);
   2547 }
   2548 
   2549 /*
   2550  * DRAIN does not affect for record-only descriptor.
   2551  */
   2552 DEF(drain_onrec)
   2553 {
   2554 	int fd;
   2555 	int r;
   2556 
   2557 	TEST("drain_onrec");
   2558 
   2559 	if (hw_canrec() == 0) {
   2560 		XP_SKIP("This test is only for recordable device");
   2561 		return;
   2562 	}
   2563 
   2564 	fd = OPEN(devaudio, O_RDONLY);
   2565 	REQUIRED_SYS_OK(fd);
   2566 
   2567 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   2568 	XP_SYS_EQ(0, r);
   2569 
   2570 	r = CLOSE(fd);
   2571 	XP_SYS_EQ(0, r);
   2572 }
   2573 
   2574 /*
   2575  * Whether mmap() succeeds with specified parameter.
   2576  */
   2577 void
   2578 test_mmap_mode(int mode, int prot)
   2579 {
   2580 	char buf[10];
   2581 	struct audio_info ai;
   2582 	const char *protstr;
   2583 	int expected;
   2584 	int fd;
   2585 	int r;
   2586 	int len;
   2587 	void *ptr;
   2588 
   2589 	if (prot == PROT_NONE) {
   2590 		protstr = "NONE";
   2591 	} else if (prot == PROT_READ) {
   2592 		protstr = "READ";
   2593 	} else if (prot == PROT_WRITE) {
   2594 		protstr = "WRITE";
   2595 	} else if (prot == (PROT_READ | PROT_WRITE)) {
   2596 		protstr = "READWRITE";
   2597 	} else {
   2598 		xp_errx(1, __LINE__, "unknown prot %x\n", prot);
   2599 	}
   2600 	TEST("mmap_%s_%s", openmode_str[mode] + 2, protstr);
   2601 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2602 		XP_SKIP("This test is only for mmap-able device");
   2603 		return;
   2604 	}
   2605 	if (mode2aumode(mode) == 0) {
   2606 		XP_SKIP("Operation not allowed on this hardware property");
   2607 		return;
   2608 	}
   2609 #if !defined(NO_RUMP)
   2610 	if (use_rump) {
   2611 		XP_SKIP("rump doesn't support mmap");
   2612 		return;
   2613 	}
   2614 #endif
   2615 
   2616 	/*
   2617 	 * On NetBSD7 and 8, mmap() always succeeds regardless of open mode.
   2618 	 * On NetBSD9, mmap() succeeds only for writable descriptor.
   2619 	 */
   2620 	expected = mode2play(mode);
   2621 	if (netbsd < 9) {
   2622 		expected = true;
   2623 	}
   2624 
   2625 	fd = OPEN(devaudio, mode);
   2626 	REQUIRED_SYS_OK(fd);
   2627 
   2628 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "get");
   2629 	REQUIRED_SYS_EQ(0, r);
   2630 
   2631 	len = ai.play.buffer_size;
   2632 
   2633 	/* Make it pause */
   2634 	AUDIO_INITINFO(&ai);
   2635 	ai.play.pause = 1;
   2636 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   2637 	REQUIRED_SYS_EQ(0, r);
   2638 
   2639 	ptr = MMAP(NULL, len, prot, MAP_FILE, fd, 0);
   2640 	XP_SYS_PTR(expected ? 0 : EACCES, ptr);
   2641 	if (expected) {
   2642 		/* XXX Doing mmap(2) doesn't inhibit read(2) */
   2643 		if (mode2rec(mode)) {
   2644 			r = READ(fd, buf, 0);
   2645 			XP_SYS_EQ(0, r);
   2646 		}
   2647 		/* Doing mmap(2) inhibits write(2) */
   2648 		if (mode2play(mode)) {
   2649 			/* NetBSD9 changes errno */
   2650 			r = WRITE(fd, buf, 0);
   2651 			if (netbsd < 9) {
   2652 				XP_SYS_NG(EINVAL, r);
   2653 			} else {
   2654 				XP_SYS_NG(EPERM, r);
   2655 			}
   2656 		}
   2657 	}
   2658 	if (ptr != MAP_FAILED) {
   2659 		r = MUNMAP(ptr, len);
   2660 		XP_SYS_EQ(0, r);
   2661 	}
   2662 
   2663 	/* Whether the pause is still valid */
   2664 	if (mode2play(mode)) {
   2665 		r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2666 		XP_SYS_EQ(0, r);
   2667 		XP_EQ(1, ai.play.pause);
   2668 	}
   2669 
   2670 	r = CLOSE(fd);
   2671 	XP_SYS_EQ(0, r);
   2672 
   2673 	reset_after_mmap();
   2674 }
   2675 #define PROT_READWRITE	(PROT_READ | PROT_WRITE)
   2676 DEF(mmap_mode_RDONLY_NONE)	{ test_mmap_mode(O_RDONLY, PROT_NONE); }
   2677 DEF(mmap_mode_RDONLY_READ)	{ test_mmap_mode(O_RDONLY, PROT_READ); }
   2678 DEF(mmap_mode_RDONLY_WRITE)	{ test_mmap_mode(O_RDONLY, PROT_WRITE); }
   2679 DEF(mmap_mode_RDONLY_READWRITE)	{ test_mmap_mode(O_RDONLY, PROT_READWRITE); }
   2680 DEF(mmap_mode_WRONLY_NONE)	{ test_mmap_mode(O_WRONLY, PROT_NONE); }
   2681 DEF(mmap_mode_WRONLY_READ)	{ test_mmap_mode(O_WRONLY, PROT_READ); }
   2682 DEF(mmap_mode_WRONLY_WRITE)	{ test_mmap_mode(O_WRONLY, PROT_WRITE); }
   2683 DEF(mmap_mode_WRONLY_READWRITE)	{ test_mmap_mode(O_WRONLY, PROT_READWRITE); }
   2684 DEF(mmap_mode_RDWR_NONE)	{ test_mmap_mode(O_RDWR, PROT_NONE); }
   2685 DEF(mmap_mode_RDWR_READ)	{ test_mmap_mode(O_RDWR, PROT_READ); }
   2686 DEF(mmap_mode_RDWR_WRITE)	{ test_mmap_mode(O_RDWR, PROT_WRITE); }
   2687 DEF(mmap_mode_RDWR_READWRITE)	{ test_mmap_mode(O_RDWR, PROT_READWRITE); }
   2688 
   2689 /*
   2690  * Check mmap()'s length and offset.
   2691  */
   2692 DEF(mmap_len)
   2693 {
   2694 	struct audio_info ai;
   2695 	int fd;
   2696 	int r;
   2697 	size_t len;
   2698 	off_t offset;
   2699 	void *ptr;
   2700 	int bufsize;
   2701 	int pagesize;
   2702 	int lsize;
   2703 
   2704 	TEST("mmap_len");
   2705 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2706 		XP_SKIP("This test is only for mmap-able device");
   2707 		return;
   2708 	}
   2709 #if !defined(NO_RUMP)
   2710 	if (use_rump) {
   2711 		XP_SKIP("rump doesn't support mmap");
   2712 		return;
   2713 	}
   2714 #endif
   2715 
   2716 	len = sizeof(pagesize);
   2717 	r = SYSCTLBYNAME("hw.pagesize", &pagesize, &len, NULL, 0);
   2718 	REQUIRED_SYS_EQ(0, r);
   2719 
   2720 	fd = OPEN(devaudio, O_WRONLY);
   2721 	REQUIRED_SYS_OK(r);
   2722 
   2723 	/* Get buffer_size */
   2724 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   2725 	REQUIRED_SYS_EQ(0, r);
   2726 	bufsize = ai.play.buffer_size;
   2727 
   2728 	/*
   2729 	 * XXX someone refers bufsize and another one does pagesize.
   2730 	 * I'm not sure.
   2731 	 */
   2732 	lsize = roundup2(bufsize, pagesize);
   2733 	struct {
   2734 		size_t len;
   2735 		off_t offset;
   2736 		int exp;
   2737 	} table[] = {
   2738 		/* len offset	expected */
   2739 
   2740 		{ 0,	0,	0 },		/* len is 0  */
   2741 		{ 1,	0,	0 },		/* len is smaller than lsize */
   2742 		{ lsize, 0,	0 },		/* len is the same as lsize */
   2743 		{ lsize + 1, 0,	EOVERFLOW },	/* len is larger */
   2744 
   2745 		{ 0, -1,	EINVAL },	/* offset is negative */
   2746 		{ 0, lsize,	0 },		/* pointless param but ok */
   2747 		{ 0, lsize + 1,	EOVERFLOW },	/* exceed */
   2748 		{ 1, lsize,	EOVERFLOW },	/* exceed */
   2749 
   2750 		/*
   2751 		 * When you treat offset as 32bit, offset will be 0
   2752 		 * and thus it incorrectly succeeds.
   2753 		 */
   2754 		{ lsize,	1ULL<<32,	EOVERFLOW },
   2755 	};
   2756 
   2757 	for (int i = 0; i < (int)__arraycount(table); i++) {
   2758 		len = table[i].len;
   2759 		offset = table[i].offset;
   2760 		int exp = table[i].exp;
   2761 
   2762 		ptr = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, offset);
   2763 		if (exp == 0) {
   2764 			XP_SYS_PTR(0, ptr);
   2765 		} else {
   2766 			/* NetBSD8 introduces EOVERFLOW */
   2767 			if (netbsd < 8 && exp == EOVERFLOW)
   2768 				exp = EINVAL;
   2769 			XP_SYS_PTR(exp, ptr);
   2770 		}
   2771 
   2772 		if (ptr != MAP_FAILED) {
   2773 			r = MUNMAP(ptr, len);
   2774 			XP_SYS_EQ(0, r);
   2775 		}
   2776 	}
   2777 
   2778 	r = CLOSE(fd);
   2779 	XP_SYS_EQ(0, r);
   2780 
   2781 	reset_after_mmap();
   2782 }
   2783 
   2784 /*
   2785  * mmap() the same descriptor twice.
   2786  */
   2787 DEF(mmap_twice)
   2788 {
   2789 	struct audio_info ai;
   2790 	int fd;
   2791 	int r;
   2792 	int len;
   2793 	void *ptr1;
   2794 	void *ptr2;
   2795 
   2796 	TEST("mmap_twice");
   2797 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2798 		XP_SKIP("This test is only for mmap-able device");
   2799 		return;
   2800 	}
   2801 #if !defined(NO_RUMP)
   2802 	if (use_rump) {
   2803 		XP_SKIP("rump doesn't support mmap");
   2804 		return;
   2805 	}
   2806 #endif
   2807 
   2808 	fd = OPEN(devaudio, O_WRONLY);
   2809 	REQUIRED_SYS_OK(fd);
   2810 
   2811 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "get");
   2812 	REQUIRED_SYS_EQ(0, r);
   2813 	len = ai.play.buffer_size;
   2814 
   2815 	ptr1 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, 0);
   2816 	XP_SYS_PTR(0, ptr1);
   2817 
   2818 	/* XXX I'm not sure this sucess is intended.  Anyway I follow it */
   2819 	ptr2 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd, 0);
   2820 	XP_SYS_PTR(0, ptr2);
   2821 
   2822 	if (ptr2 != MAP_FAILED) {
   2823 		r = MUNMAP(ptr2, len);
   2824 		XP_SYS_EQ(0, r);
   2825 	}
   2826 	if (ptr1 != MAP_FAILED) {
   2827 		r = MUNMAP(ptr1, len);
   2828 		XP_SYS_EQ(0, r);
   2829 	}
   2830 
   2831 	r = CLOSE(fd);
   2832 	XP_SYS_EQ(0, r);
   2833 
   2834 	reset_after_mmap();
   2835 }
   2836 
   2837 /*
   2838  * mmap() different descriptors.
   2839  */
   2840 DEF(mmap_multi)
   2841 {
   2842 	struct audio_info ai;
   2843 	int fd0;
   2844 	int fd1;
   2845 	int r;
   2846 	int len;
   2847 	void *ptr0;
   2848 	void *ptr1;
   2849 
   2850 	TEST("mmap_multi");
   2851 	if (netbsd < 8) {
   2852 		XP_SKIP("Multiple open is not supported");
   2853 		return;
   2854 	}
   2855 	if ((props & AUDIO_PROP_MMAP) == 0) {
   2856 		XP_SKIP("This test is only for mmap-able device");
   2857 		return;
   2858 	}
   2859 #if !defined(NO_RUMP)
   2860 	if (use_rump) {
   2861 		XP_SKIP("rump doesn't support mmap");
   2862 		return;
   2863 	}
   2864 #endif
   2865 
   2866 	fd0 = OPEN(devaudio, O_WRONLY);
   2867 	REQUIRED_SYS_OK(fd0);
   2868 
   2869 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "get");
   2870 	REQUIRED_SYS_EQ(0, r);
   2871 	len = ai.play.buffer_size;
   2872 
   2873 	fd1 = OPEN(devaudio, O_WRONLY);
   2874 	REQUIRED_SYS_OK(fd1);
   2875 
   2876 	ptr0 = MMAP(NULL, len, PROT_WRITE, MAP_FILE, fd0, 0);
   2877 	XP_SYS_PTR(0, ptr0);
   2878 
   2879 	ptr1 = MMAP(NULL, len,  PROT_WRITE, MAP_FILE, fd1, 0);
   2880 	XP_SYS_PTR(0, ptr1);
   2881 
   2882 	if (ptr0 != MAP_FAILED) {
   2883 		r = MUNMAP(ptr1, len);
   2884 		XP_SYS_EQ(0, r);
   2885 	}
   2886 
   2887 	r = CLOSE(fd1);
   2888 	XP_SYS_EQ(0, r);
   2889 
   2890 	if (ptr1 != MAP_FAILED) {
   2891 		r = MUNMAP(ptr0, len);
   2892 		XP_SYS_EQ(0, r);
   2893 	}
   2894 
   2895 	r = CLOSE(fd0);
   2896 	XP_SYS_EQ(0, r);
   2897 
   2898 	reset_after_mmap();
   2899 }
   2900 
   2901 #define IN	POLLIN
   2902 #define OUT	POLLOUT
   2903 /*
   2904  * Whether poll() succeeds with specified mode.
   2905  */
   2906 void
   2907 test_poll_mode(int mode, int events, int expected_revents)
   2908 {
   2909 	struct pollfd pfd;
   2910 	const char *events_str;
   2911 	int fd;
   2912 	int r;
   2913 	int expected_r;
   2914 
   2915 	if (events == IN) {
   2916 		events_str = "IN";
   2917 	} else if (events == OUT) {
   2918 		events_str = "OUT";
   2919 	} else if (events == (IN | OUT)) {
   2920 		events_str = "INOUT";
   2921 	} else {
   2922 		events_str = "?";
   2923 	}
   2924 	TEST("poll_mode_%s_%s", openmode_str[mode] + 2, events_str);
   2925 	if (mode2aumode(mode) == 0) {
   2926 		XP_SKIP("Operation not allowed on this hardware property");
   2927 		return;
   2928 	}
   2929 
   2930 	expected_r = (expected_revents != 0) ? 1 : 0;
   2931 
   2932 	fd = OPEN(devaudio, mode);
   2933 	REQUIRED_SYS_OK(fd);
   2934 
   2935 	/* Wait a bit to be recorded. */
   2936 	usleep(100 * 1000);
   2937 
   2938 	memset(&pfd, 0, sizeof(pfd));
   2939 	pfd.fd = fd;
   2940 	pfd.events = events;
   2941 
   2942 	r = POLL(&pfd, 1, 100);
   2943 	/* It's a bit complicated..  */
   2944 	if (r < 0 || r > 1) {
   2945 		/*
   2946 		 * Check these two cases first:
   2947 		 * - system call fails.
   2948 		 * - poll() with one nfds returns >1.  It's strange.
   2949 		 */
   2950 		XP_SYS_EQ(expected_r, r);
   2951 	} else {
   2952 		/*
   2953 		 * Otherwise, poll() returned 0 or 1.
   2954 		 */
   2955 		DPRINTF("  > pfd.revents=%s\n", event_tostr(pfd.revents));
   2956 
   2957 		/* NetBSD7,8 have several strange behavior.  It must be bug. */
   2958 
   2959 		XP_SYS_EQ(expected_r, r);
   2960 		XP_EQ(expected_revents, pfd.revents);
   2961 	}
   2962 	r = CLOSE(fd);
   2963 	XP_SYS_EQ(0, r);
   2964 }
   2965 DEF(poll_mode_RDONLY_IN)	{ test_poll_mode(O_RDONLY, IN,     IN); }
   2966 DEF(poll_mode_RDONLY_OUT)	{ test_poll_mode(O_RDONLY, OUT,    0); }
   2967 DEF(poll_mode_RDONLY_INOUT)	{ test_poll_mode(O_RDONLY, IN|OUT, IN); }
   2968 DEF(poll_mode_WRONLY_IN)	{ test_poll_mode(O_WRONLY, IN,     0); }
   2969 DEF(poll_mode_WRONLY_OUT)	{ test_poll_mode(O_WRONLY, OUT,	   OUT); }
   2970 DEF(poll_mode_WRONLY_INOUT)	{ test_poll_mode(O_WRONLY, IN|OUT, OUT); }
   2971 DEF(poll_mode_RDWR_IN)		{
   2972 	/* On half-duplex, O_RDWR is the same as O_WRONLY. */
   2973 	if (hw_fulldup()) test_poll_mode(O_RDWR,   IN,     IN);
   2974 	else		  test_poll_mode(O_RDWR,   IN,     0);
   2975 }
   2976 DEF(poll_mode_RDWR_OUT)		{ test_poll_mode(O_RDWR,   OUT,	   OUT); }
   2977 DEF(poll_mode_RDWR_INOUT)	{
   2978 	/* On half-duplex, O_RDWR is the same as O_WRONLY. */
   2979 	if (hw_fulldup()) test_poll_mode(O_RDWR,   IN|OUT, IN|OUT);
   2980 	else		  test_poll_mode(O_RDWR,   IN|OUT,    OUT);
   2981 }
   2982 
   2983 /*
   2984  * Poll(OUT) when buffer is empty.
   2985  */
   2986 DEF(poll_out_empty)
   2987 {
   2988 	struct pollfd pfd;
   2989 	int fd;
   2990 	int r;
   2991 
   2992 	TEST("poll_out_empty");
   2993 
   2994 	fd = OPEN(devaudio, O_WRONLY);
   2995 	REQUIRED_SYS_OK(fd);
   2996 
   2997 	memset(&pfd, 0, sizeof(pfd));
   2998 	pfd.fd = fd;
   2999 	pfd.events = POLLOUT;
   3000 
   3001 	/* Check when empty.  It should succeed even if timeout == 0 */
   3002 	r = POLL(&pfd, 1, 0);
   3003 	XP_SYS_EQ(1, r);
   3004 	XP_EQ(POLLOUT, pfd.revents);
   3005 
   3006 	r = CLOSE(fd);
   3007 	XP_SYS_EQ(0, r);
   3008 }
   3009 
   3010 /*
   3011  * Poll(OUT) when buffer is full.
   3012  */
   3013 DEF(poll_out_full)
   3014 {
   3015 	struct audio_info ai;
   3016 	struct pollfd pfd;
   3017 	int fd;
   3018 	int r;
   3019 	char *buf;
   3020 	int buflen;
   3021 
   3022 	TEST("poll_out_full");
   3023 
   3024 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3025 	REQUIRED_SYS_OK(fd);
   3026 
   3027 	/* Pause */
   3028 	AUDIO_INITINFO(&ai);
   3029 	ai.play.pause = 1;
   3030 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   3031 	XP_SYS_EQ(0, r);
   3032 
   3033 	/* Get buffer size */
   3034 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3035 	XP_SYS_EQ(0, r);
   3036 
   3037 	/* Write until full */
   3038 	buflen = ai.play.buffer_size;
   3039 	buf = (char *)malloc(buflen);
   3040 	REQUIRED_IF(buf != NULL);
   3041 	memset(buf, 0xff, buflen);
   3042 	do {
   3043 		r = WRITE(fd, buf, buflen);
   3044 	} while (r == buflen);
   3045 	if (r == -1) {
   3046 		XP_SYS_NG(EAGAIN, r);
   3047 	}
   3048 
   3049 	/* Do poll */
   3050 	memset(&pfd, 0, sizeof(pfd));
   3051 	pfd.fd = fd;
   3052 	pfd.events = POLLOUT;
   3053 	r = POLL(&pfd, 1, 0);
   3054 	XP_SYS_EQ(0, r);
   3055 	XP_EQ(0, pfd.revents);
   3056 
   3057 	r = CLOSE(fd);
   3058 	XP_SYS_EQ(0, r);
   3059 	free(buf);
   3060 }
   3061 
   3062 /*
   3063  * Poll(OUT) when buffer is full but hiwat sets lower than full.
   3064  */
   3065 DEF(poll_out_hiwat)
   3066 {
   3067 	struct audio_info ai;
   3068 	struct pollfd pfd;
   3069 	int fd;
   3070 	int r;
   3071 	char *buf;
   3072 	int buflen;
   3073 	int newhiwat;
   3074 
   3075 	TEST("poll_out_hiwat");
   3076 
   3077 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3078 	REQUIRED_SYS_OK(fd);
   3079 
   3080 	/* Get buffer size and hiwat */
   3081 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3082 	XP_SYS_EQ(0, r);
   3083 	/* Change hiwat some different value */
   3084 	newhiwat = ai.lowat;
   3085 
   3086 	/* Set pause and hiwat */
   3087 	AUDIO_INITINFO(&ai);
   3088 	ai.play.pause = 1;
   3089 	ai.hiwat = newhiwat;
   3090 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1;hiwat");
   3091 	XP_SYS_EQ(0, r);
   3092 
   3093 	/* Get the set hiwat again */
   3094 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3095 	XP_SYS_EQ(0, r);
   3096 
   3097 	/* Write until full */
   3098 	buflen = ai.blocksize * ai.hiwat;
   3099 	buf = (char *)malloc(buflen);
   3100 	REQUIRED_IF(buf != NULL);
   3101 	memset(buf, 0xff, buflen);
   3102 	do {
   3103 		r = WRITE(fd, buf, buflen);
   3104 	} while (r == buflen);
   3105 	if (r == -1) {
   3106 		XP_SYS_NG(EAGAIN, r);
   3107 	}
   3108 
   3109 	/* Do poll */
   3110 	memset(&pfd, 0, sizeof(pfd));
   3111 	pfd.fd = fd;
   3112 	pfd.events = POLLOUT;
   3113 	r = POLL(&pfd, 1, 0);
   3114 	XP_SYS_EQ(0, r);
   3115 	XP_EQ(0, pfd.revents);
   3116 
   3117 	r = CLOSE(fd);
   3118 	XP_SYS_EQ(0, r);
   3119 	free(buf);
   3120 }
   3121 
   3122 /*
   3123  * Unpause from buffer full, POLLOUT should raise.
   3124  * XXX poll(2) on NetBSD7 is really incomplete and wierd.  I don't test it.
   3125  */
   3126 DEF(poll_out_unpause)
   3127 {
   3128 	struct audio_info ai;
   3129 	struct pollfd pfd;
   3130 	int fd;
   3131 	int r;
   3132 	char *buf;
   3133 	int buflen;
   3134 	u_int blocksize;
   3135 	int hiwat;
   3136 	int lowat;
   3137 
   3138 	TEST("poll_out_unpause");
   3139 	if (netbsd < 8) {
   3140 		XP_SKIP("NetBSD7's poll() is too incomplete to test.");
   3141 		return;
   3142 	}
   3143 
   3144 	/* Non-blocking open */
   3145 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3146 	REQUIRED_SYS_OK(fd);
   3147 
   3148 	/* Adjust block size and hiwat/lowat to make the test time 1sec */
   3149 	blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3150 	hiwat = 12;		/* 1.5sec */
   3151 	lowat = 4;		/* 0.5sec */
   3152 	AUDIO_INITINFO(&ai);
   3153 	ai.blocksize = blocksize;
   3154 	ai.hiwat = hiwat;
   3155 	ai.lowat = lowat;
   3156 	/* and also set encoding */
   3157 	/*
   3158 	 * XXX NetBSD7 has different results depending on whether the input
   3159 	 * encoding is emulated (AUDIO_ENCODINGFLAG_EMULATED) or not.  It's
   3160 	 * not easy to ensure this situation on all hardware environment.
   3161 	 * On NetBSD9, the result is the same regardless of input encoding.
   3162 	 */
   3163 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "blocksize=%d", blocksize);
   3164 	XP_SYS_EQ(0, r);
   3165 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3166 	if (ai.blocksize != blocksize) {
   3167 		/*
   3168 		 * NetBSD9 can not change the blocksize.  Then,
   3169 		 * adjust using hiwat/lowat.
   3170 		 */
   3171 		blocksize = ai.blocksize;
   3172 		hiwat = howmany(8000 * 1.5, blocksize);
   3173 		lowat = howmany(8000 * 0.5, blocksize);
   3174 	}
   3175 	/* Anyway, set the parameters */
   3176 	AUDIO_INITINFO(&ai);
   3177 	ai.blocksize = blocksize;
   3178 	ai.hiwat = hiwat;
   3179 	ai.lowat = lowat;
   3180 	ai.play.pause = 1;
   3181 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   3182 	XP_SYS_EQ(0, r);
   3183 
   3184 	/* Get the set parameters again */
   3185 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3186 	XP_SYS_EQ(0, r);
   3187 
   3188 	/* Write until full */
   3189 	buflen = ai.blocksize * ai.hiwat;
   3190 	buf = (char *)malloc(buflen);
   3191 	REQUIRED_IF(buf != NULL);
   3192 	memset(buf, 0xff, buflen);
   3193 	do {
   3194 		r = WRITE(fd, buf, buflen);
   3195 	} while (r == buflen);
   3196 	if (r == -1) {
   3197 		XP_SYS_NG(EAGAIN, r);
   3198 	}
   3199 
   3200 	/* At this time, POLLOUT should not be set because buffer is full */
   3201 	memset(&pfd, 0, sizeof(pfd));
   3202 	pfd.fd = fd;
   3203 	pfd.events = POLLOUT;
   3204 	r = POLL(&pfd, 1, 0);
   3205 	XP_SYS_EQ(0, r);
   3206 	XP_EQ(0, pfd.revents);
   3207 
   3208 	/* Unpause */
   3209 	AUDIO_INITINFO(&ai);
   3210 	ai.play.pause = 0;
   3211 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=0");
   3212 	XP_SYS_EQ(0, r);
   3213 
   3214 	/*
   3215 	 * When unpause occurs:
   3216 	 * - NetBSD7 (emul=0) -> the buffer remains.
   3217 	 * - NetBSD7 (emul=1) -> the buffer is cleared.
   3218 	 * - NetBSD8          -> the buffer remains.
   3219 	 * - NetBSD9          -> the buffer remains.
   3220 	 */
   3221 
   3222 	/* Check poll() up to 2sec */
   3223 	pfd.revents = 0;
   3224 	r = POLL(&pfd, 1, 2000);
   3225 	XP_SYS_EQ(1, r);
   3226 	XP_EQ(POLLOUT, pfd.revents);
   3227 
   3228 	/*
   3229 	 * Since POLLOUT is set, it should be writable.
   3230 	 * But at this time, no all buffer may be writable.
   3231 	 */
   3232 	r = WRITE(fd, buf, buflen);
   3233 	XP_SYS_OK(r);
   3234 
   3235 	/* Flush it because there is no need to play it */
   3236 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   3237 	XP_SYS_EQ(0, r);
   3238 
   3239 	r = CLOSE(fd);
   3240 	XP_SYS_EQ(0, r);
   3241 	free(buf);
   3242 }
   3243 
   3244 /*
   3245  * poll(2) must not be affected by playback of other descriptors.
   3246  */
   3247 DEF(poll_out_simul)
   3248 {
   3249 	struct audio_info ai;
   3250 	struct pollfd pfd[2];
   3251 	int fd[2];
   3252 	int r;
   3253 	char *buf;
   3254 	u_int blocksize;
   3255 	int hiwat;
   3256 	int lowat;
   3257 	int buflen;
   3258 	int time;
   3259 
   3260 	TEST("poll_out_simul");
   3261 	if (netbsd < 8) {
   3262 		XP_SKIP("Multiple open is not supported");
   3263 		return;
   3264 	}
   3265 
   3266 	/* Make sure that it's not affected by descriptor order */
   3267 	for (int i = 0; i < 2; i++) {
   3268 		int a = i;
   3269 		int b = 1 - i;
   3270 
   3271 		fd[0] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3272 		REQUIRED_SYS_OK(fd[0]);
   3273 		fd[1] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3274 		REQUIRED_SYS_OK(fd[1]);
   3275 
   3276 		/*
   3277 		 * Adjust block size and hiwat/lowat.
   3278 		 * I want to choice suitable blocksize (if possible).
   3279 		 */
   3280 		blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3281 		hiwat = 12;		/* 1.5sec */
   3282 		lowat = 4;		/* 0.5sec */
   3283 		AUDIO_INITINFO(&ai);
   3284 		ai.blocksize = blocksize;
   3285 		ai.hiwat = hiwat;
   3286 		ai.lowat = lowat;
   3287 		r = IOCTL(fd[0], AUDIO_SETINFO, &ai, "blocksize=1000");
   3288 		XP_SYS_EQ(0, r);
   3289 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "read back blocksize");
   3290 		if (ai.blocksize != blocksize) {
   3291 			/*
   3292 			 * NetBSD9 can not change the blocksize.  Then,
   3293 			 * adjust using hiwat/lowat.
   3294 			 */
   3295 			blocksize = ai.blocksize;
   3296 			hiwat = howmany(8000 * 1.5, blocksize);
   3297 			lowat = howmany(8000 * 0.5, blocksize);
   3298 		}
   3299 		/* Anyway, set the parameters */
   3300 		AUDIO_INITINFO(&ai);
   3301 		ai.blocksize = blocksize;
   3302 		ai.hiwat = hiwat;
   3303 		ai.lowat = lowat;
   3304 		/* Pause fdA */
   3305 		ai.play.pause = 1;
   3306 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=1");
   3307 		XP_SYS_EQ(0, r);
   3308 		/* Unpause fdB */
   3309 		ai.play.pause = 0;
   3310 		r = IOCTL(fd[b], AUDIO_SETINFO, &ai, "pause=0");
   3311 		XP_SYS_EQ(0, r);
   3312 
   3313 		/* Get again. XXX two individual ioctls are correct */
   3314 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "");
   3315 		XP_SYS_EQ(0, r);
   3316 		DPRINTF("  > blocksize=%d lowat=%d hiwat=%d\n",
   3317 			ai.blocksize, ai.lowat, ai.hiwat);
   3318 
   3319 		/* Enough long time than the playback time */
   3320 		time = (ai.hiwat - ai.lowat) * blocksize / 8;  /*[msec]*/
   3321 		time *= 2;
   3322 
   3323 		/* Write fdA full */
   3324 		buflen = blocksize * ai.lowat;
   3325 		buf = (char *)malloc(buflen);
   3326 		REQUIRED_IF(buf != NULL);
   3327 		memset(buf, 0xff, buflen);
   3328 		do {
   3329 			r = WRITE(fd[a], buf, buflen);
   3330 		} while (r == buflen);
   3331 		if (r == -1) {
   3332 			XP_SYS_NG(EAGAIN, r);
   3333 		}
   3334 
   3335 		/* POLLOUT should not be set, because fdA is buffer full */
   3336 		memset(pfd, 0, sizeof(pfd));
   3337 		pfd[0].fd = fd[a];
   3338 		pfd[0].events = POLLOUT;
   3339 		r = POLL(pfd, 1, 0);
   3340 		XP_SYS_EQ(0, r);
   3341 		XP_EQ(0, pfd[0].revents);
   3342 
   3343 		/* Write fdB at least lowat */
   3344 		r = WRITE(fd[b], buf, buflen);
   3345 		XP_SYS_EQ(buflen, r);
   3346 		r = WRITE(fd[b], buf, buflen);
   3347 		if (r == -1) {
   3348 			XP_SYS_NG(EAGAIN, r);
   3349 		}
   3350 
   3351 		/* Only fdB should become POLLOUT */
   3352 		memset(pfd, 0, sizeof(pfd));
   3353 		pfd[0].fd = fd[0];
   3354 		pfd[0].events = POLLOUT;
   3355 		pfd[1].fd = fd[1];
   3356 		pfd[1].events = POLLOUT;
   3357 		r = POLL(pfd, 2, time);
   3358 		XP_SYS_EQ(1, r);
   3359 		if (r != -1) {
   3360 			XP_EQ(0, pfd[a].revents);
   3361 			XP_EQ(POLLOUT, pfd[b].revents);
   3362 		}
   3363 
   3364 		/* Drop the rest */
   3365 		r = IOCTL(fd[0], AUDIO_FLUSH, NULL, "");
   3366 		XP_SYS_EQ(0, r);
   3367 		r = IOCTL(fd[1], AUDIO_FLUSH, NULL, "");
   3368 		XP_SYS_EQ(0, r);
   3369 
   3370 		r = CLOSE(fd[0]);
   3371 		XP_SYS_EQ(0, r);
   3372 		r = CLOSE(fd[1]);
   3373 		XP_SYS_EQ(0, r);
   3374 		free(buf);
   3375 
   3376 		xxx_close_wait();
   3377 	}
   3378 }
   3379 
   3380 /*
   3381  * Open with READ mode starts recording immediately.
   3382  * Of course, audioctl doesn't start.
   3383  */
   3384 void
   3385 test_poll_in_open(const char *devname)
   3386 {
   3387 	struct audio_info ai;
   3388 	struct pollfd pfd;
   3389 	char buf[4096];
   3390 	char devfile[16];
   3391 	int fd;
   3392 	int r;
   3393 	bool is_audioctl;
   3394 
   3395 	TEST("poll_in_open_%s", devname);
   3396 	if (hw_canrec() == 0) {
   3397 		XP_SKIP("This test is only for recordable device");
   3398 		return;
   3399 	}
   3400 
   3401 	snprintf(devfile, sizeof(devfile), "/dev/%s%d", devname, unit);
   3402 	is_audioctl = (strcmp(devname, "audioctl") == 0);
   3403 
   3404 	fd = OPEN(devfile, O_RDONLY);
   3405 	REQUIRED_SYS_OK(fd);
   3406 
   3407 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3408 	REQUIRED_SYS_EQ(0, r);
   3409 	if (is_audioctl) {
   3410 		/* opening /dev/audioctl doesn't start recording. */
   3411 		XP_EQ(0, ai.record.active);
   3412 	} else {
   3413 		/* opening /dev/{audio,sound} starts recording. */
   3414 		/*
   3415 		 * On NetBSD7/8, opening /dev/sound doesn't start recording.
   3416 		 * It must be a bug.
   3417 		 */
   3418 		XP_EQ(1, ai.record.active);
   3419 	}
   3420 
   3421 	memset(&pfd, 0, sizeof(pfd));
   3422 	pfd.fd = fd;
   3423 	pfd.events = POLLIN;
   3424 	r = POLL(&pfd, 1, 1000);
   3425 	if (is_audioctl) {
   3426 		/*
   3427 		 * poll-ing /dev/audioctl always fails.
   3428 		 * XXX Returning error instead of timeout should be better(?).
   3429 		 */
   3430 		REQUIRED_SYS_EQ(0, r);
   3431 	} else {
   3432 		/*
   3433 		 * poll-ing /dev/{audio,sound} will succeed when recorded
   3434 		 * data is arrived.
   3435 		 */
   3436 		/*
   3437 		 * On NetBSD7/8, opening /dev/sound doesn't start recording.
   3438 		 * It must be a bug.
   3439 		 */
   3440 		REQUIRED_SYS_EQ(1, r);
   3441 
   3442 		/* In this case, read() should succeed. */
   3443 		r = READ(fd, buf, sizeof(buf));
   3444 		XP_SYS_OK(r);
   3445 		XP_NE(0, r);
   3446 	}
   3447 
   3448 	r = CLOSE(fd);
   3449 	XP_SYS_EQ(0, r);
   3450 }
   3451 DEF(poll_in_open_audio)		{ test_poll_in_open("audio"); }
   3452 DEF(poll_in_open_sound)		{ test_poll_in_open("sound"); }
   3453 DEF(poll_in_open_audioctl)	{ test_poll_in_open("audioctl"); }
   3454 
   3455 /*
   3456  * poll(2) must not be affected by other recording descriptors even if
   3457  * playback descriptor waits with POLLIN (though it's not normal usage).
   3458  * In other words, two POLLIN must not interfere.
   3459  */
   3460 DEF(poll_in_simul)
   3461 {
   3462 	struct audio_info ai;
   3463 	struct pollfd pfd;
   3464 	int fd[2];
   3465 	int r;
   3466 	char *buf;
   3467 	int blocksize;
   3468 
   3469 	TEST("poll_in_simul");
   3470 	if (netbsd < 8) {
   3471 		XP_SKIP("Multiple open is not supported");
   3472 		return;
   3473 	}
   3474 	if (hw_fulldup() == 0) {
   3475 		XP_SKIP("This test is only for full-duplex device");
   3476 		return;
   3477 	}
   3478 
   3479 	int play = 0;
   3480 	int rec = 1;
   3481 
   3482 	fd[play] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3483 	REQUIRED_SYS_OK(fd[play]);
   3484 	fd[rec] = OPEN(devaudio, O_RDONLY);
   3485 	REQUIRED_SYS_OK(fd[rec]);
   3486 
   3487 	/* Get block size */
   3488 	r = IOCTL(fd[rec], AUDIO_GETBUFINFO, &ai, "");
   3489 	XP_SYS_EQ(0, r);
   3490 	blocksize = ai.blocksize;
   3491 
   3492 	buf = (char *)malloc(blocksize);
   3493 	REQUIRED_IF(buf != NULL);
   3494 
   3495 	/*
   3496 	 * At first, make sure the playback one doesn't return POLLIN.
   3497 	 */
   3498 	memset(&pfd, 0, sizeof(pfd));
   3499 	pfd.fd = fd[play];
   3500 	pfd.events = POLLIN;
   3501 	r = POLL(&pfd, 1, 0);
   3502 	if (r == 0 && pfd.revents == 0) {
   3503 		XP_SYS_EQ(0, r);
   3504 		XP_EQ(0, pfd.revents);
   3505 	} else {
   3506 		XP_FAIL("play fd returns POLLIN");
   3507 		goto abort;
   3508 	}
   3509 
   3510 	/* Start recording */
   3511 	r = READ(fd[rec], buf, blocksize);
   3512 	XP_SYS_EQ(blocksize, r);
   3513 
   3514 	/* Poll()ing playback descriptor with POLLIN should not raise */
   3515 	r = POLL(&pfd, 1, 1000);
   3516 	XP_SYS_EQ(0, r);
   3517 	XP_EQ(0, pfd.revents);
   3518 
   3519 	/* Poll()ing recording descriptor with POLLIN should raise */
   3520 	pfd.fd = fd[rec];
   3521 	r = POLL(&pfd, 1, 0);
   3522 	XP_SYS_EQ(1, r);
   3523 	XP_EQ(POLLIN, pfd.revents);
   3524 
   3525 abort:
   3526 	r = CLOSE(fd[play]);
   3527 	XP_SYS_EQ(0, r);
   3528 	r = CLOSE(fd[rec]);
   3529 	XP_SYS_EQ(0, r);
   3530 	free(buf);
   3531 }
   3532 
   3533 /*
   3534  * Whether kqueue() succeeds with specified mode.
   3535  */
   3536 void
   3537 test_kqueue_mode(int openmode, int filt, int expected)
   3538 {
   3539 	struct kevent kev;
   3540 	struct timespec ts;
   3541 	int fd;
   3542 	int kq;
   3543 	int r;
   3544 
   3545 	TEST("kqueue_mode_%s_%s",
   3546 	    openmode_str[openmode] + 2,
   3547 	    (filt == EVFILT_READ) ? "READ" : "WRITE");
   3548 	if (mode2aumode(openmode) == 0) {
   3549 		XP_SKIP("Operation not allowed on this hardware property");
   3550 		return;
   3551 	}
   3552 
   3553 	ts.tv_sec = 0;
   3554 	ts.tv_nsec = 100 * 1000 * 1000;	// 100msec
   3555 
   3556 	kq = KQUEUE();
   3557 	XP_SYS_OK(kq);
   3558 
   3559 	fd = OPEN(devaudio, openmode);
   3560 	REQUIRED_SYS_OK(fd);
   3561 
   3562 	/*
   3563 	 * Check whether the specified filter can be set.
   3564 	 * Any filters can always be set, even if pointless combination.
   3565 	 * For example, EVFILT_READ can be set on O_WRONLY descriptor
   3566 	 * though it will never raise.
   3567 	 * I will not mention about good or bad of this behavior here.
   3568 	 */
   3569 	EV_SET(&kev, fd, filt, EV_ADD, 0, 0, 0);
   3570 	r = KEVENT_SET(kq, &kev, 1);
   3571 	XP_SYS_EQ(0, r);
   3572 
   3573 	if (r == 0) {
   3574 		/* If the filter can be set, try kevent(poll) */
   3575 		r = KEVENT_POLL(kq, &kev, 1, &ts);
   3576 		XP_SYS_EQ(expected, r);
   3577 
   3578 		/* Delete it */
   3579 		EV_SET(&kev, fd, filt, EV_DELETE, 0, 0, 0);
   3580 		r = KEVENT_SET(kq, &kev, 1);
   3581 		XP_SYS_EQ(0, r);
   3582 	}
   3583 
   3584 	r = CLOSE(fd);
   3585 	XP_SYS_EQ(0, r);
   3586 	r = CLOSE(kq);
   3587 	XP_SYS_EQ(0, r);
   3588 }
   3589 DEF(kqueue_mode_RDONLY_READ) {
   3590 	/* Should raise */
   3591 	test_kqueue_mode(O_RDONLY, EVFILT_READ, 1);
   3592 }
   3593 DEF(kqueue_mode_RDONLY_WRITE) {
   3594 	/* Should never raise (NetBSD7 has bugs) */
   3595 	int expected = (netbsd < 8) ? 1 : 0;
   3596 	test_kqueue_mode(O_RDONLY, EVFILT_WRITE, expected);
   3597 }
   3598 DEF(kqueue_mode_WRONLY_READ) {
   3599 	/* Should never raise */
   3600 	test_kqueue_mode(O_WRONLY, EVFILT_READ, 0);
   3601 }
   3602 DEF(kqueue_mode_WRONLY_WRITE) {
   3603 	/* Should raise */
   3604 	test_kqueue_mode(O_WRONLY, EVFILT_WRITE, 1);
   3605 }
   3606 DEF(kqueue_mode_RDWR_READ) {
   3607 	/* Should raise on fulldup but not on halfdup, on NetBSD9 */
   3608 	int expected = hw_fulldup() ? 1 : 0;
   3609 	test_kqueue_mode(O_RDWR, EVFILT_READ, expected);
   3610 }
   3611 DEF(kqueue_mode_RDWR_WRITE) {
   3612 	/* Should raise */
   3613 	test_kqueue_mode(O_RDWR, EVFILT_WRITE, 1);
   3614 }
   3615 
   3616 /*
   3617  * kqueue(2) when buffer is empty.
   3618  */
   3619 DEF(kqueue_empty)
   3620 {
   3621 	struct audio_info ai;
   3622 	struct kevent kev;
   3623 	struct timespec ts;
   3624 	int kq;
   3625 	int fd;
   3626 	int r;
   3627 
   3628 	TEST("kqueue_empty");
   3629 
   3630 	fd = OPEN(devaudio, O_WRONLY);
   3631 	REQUIRED_SYS_OK(fd);
   3632 
   3633 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3634 	XP_SYS_EQ(0, r);
   3635 
   3636 	kq = KQUEUE();
   3637 	XP_SYS_OK(kq);
   3638 
   3639 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3640 	r = KEVENT_SET(kq, &kev, 1);
   3641 	XP_SYS_EQ(0, r);
   3642 
   3643 	/* When the buffer is empty, it should succeed even if timeout == 0 */
   3644 	memset(&ts, 0, sizeof(ts));
   3645 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3646 	XP_SYS_EQ(1, r);
   3647 	XP_EQ(fd, kev.ident);
   3648 	/*
   3649 	 * XXX According to kqueue(2) manpage, returned kev.data contains
   3650 	 * "the amount of space remaining in the write buffer".
   3651 	 * NetBSD7 returns buffer_size.  Shouldn't it be blocksize * hiwat?
   3652 	 */
   3653 	/* XP_EQ(ai.blocksize * ai.hiwat, kev.data); */
   3654 	XP_EQ(ai.play.buffer_size, kev.data);
   3655 
   3656 	r = CLOSE(fd);
   3657 	XP_SYS_EQ(0, r);
   3658 	r = CLOSE(kq);
   3659 	XP_SYS_EQ(0, r);
   3660 }
   3661 
   3662 /*
   3663  * kqueue(2) when buffer is full.
   3664  */
   3665 DEF(kqueue_full)
   3666 {
   3667 	struct audio_info ai;
   3668 	struct kevent kev;
   3669 	struct timespec ts;
   3670 	int kq;
   3671 	int fd;
   3672 	int r;
   3673 	char *buf;
   3674 	int buflen;
   3675 
   3676 	TEST("kqueue_full");
   3677 
   3678 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3679 	REQUIRED_SYS_OK(fd);
   3680 
   3681 	/* Pause */
   3682 	AUDIO_INITINFO(&ai);
   3683 	ai.play.pause = 1;
   3684 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   3685 	XP_SYS_EQ(0, r);
   3686 
   3687 	/* Get buffer size */
   3688 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3689 	XP_SYS_EQ(0, r);
   3690 
   3691 	/* Write until full */
   3692 	buflen = ai.play.buffer_size;
   3693 	buf = (char *)malloc(buflen);
   3694 	REQUIRED_IF(buf != NULL);
   3695 	memset(buf, 0xff, buflen);
   3696 	do {
   3697 		r = WRITE(fd, buf, buflen);
   3698 	} while (r == buflen);
   3699 	if (r == -1) {
   3700 		XP_SYS_NG(EAGAIN, r);
   3701 	}
   3702 
   3703 	kq = KQUEUE();
   3704 	XP_SYS_OK(kq);
   3705 
   3706 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3707 	r = KEVENT_SET(kq, &kev, 1);
   3708 	XP_SYS_EQ(0, r);
   3709 
   3710 	/* kevent() should not raise */
   3711 	ts.tv_sec = 0;
   3712 	ts.tv_nsec = 100L * 1000 * 1000;	/* 100msec */
   3713 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3714 	XP_SYS_EQ(0, r);
   3715 	if (r > 0) {
   3716 		XP_EQ(fd, kev.ident);
   3717 		XP_EQ(0, kev.data);
   3718 	}
   3719 
   3720 	r = CLOSE(fd);
   3721 	XP_SYS_EQ(0, r);
   3722 	r = CLOSE(kq);
   3723 	XP_SYS_EQ(0, r);
   3724 	free(buf);
   3725 }
   3726 
   3727 /*
   3728  * kqueue(2) when buffer is full but hiwat sets lower than full.
   3729  */
   3730 DEF(kqueue_hiwat)
   3731 {
   3732 	struct audio_info ai;
   3733 	struct kevent kev;
   3734 	struct timespec ts;
   3735 	int kq;
   3736 	int fd;
   3737 	int r;
   3738 	char *buf;
   3739 	int buflen;
   3740 	int newhiwat;
   3741 
   3742 	TEST("kqueue_hiwat");
   3743 
   3744 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3745 	REQUIRED_SYS_OK(fd);
   3746 
   3747 	/* Get buffer size and hiwat */
   3748 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "hiwat");
   3749 	XP_SYS_EQ(0, r);
   3750 	/* Change hiwat some different value */
   3751 	newhiwat = ai.hiwat - 1;
   3752 
   3753 	/* Set pause and hiwat */
   3754 	AUDIO_INITINFO(&ai);
   3755 	ai.play.pause = 1;
   3756 	ai.hiwat = newhiwat;
   3757 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1;hiwat");
   3758 	XP_SYS_EQ(0, r);
   3759 
   3760 	/* Get the set parameters again */
   3761 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3762 	XP_SYS_EQ(0, r);
   3763 	XP_EQ(1, ai.play.pause);
   3764 	XP_EQ(newhiwat, ai.hiwat);
   3765 
   3766 	/* Write until full */
   3767 	buflen = ai.blocksize * ai.hiwat;
   3768 	buf = (char *)malloc(buflen);
   3769 	REQUIRED_IF(buf != NULL);
   3770 	memset(buf, 0xff, buflen);
   3771 	do {
   3772 		r = WRITE(fd, buf, buflen);
   3773 	} while (r == buflen);
   3774 	if (r == -1) {
   3775 		XP_SYS_NG(EAGAIN, r);
   3776 	}
   3777 
   3778 	kq = KQUEUE();
   3779 	XP_SYS_OK(kq);
   3780 
   3781 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3782 	r = KEVENT_SET(kq, &kev, 1);
   3783 	XP_SYS_EQ(0, r);
   3784 
   3785 	/* Should not raise because it's not possible to write */
   3786 	ts.tv_sec = 0;
   3787 	ts.tv_nsec = 100L * 1000 * 1000;	/* 100msec */
   3788 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3789 	if (r > 0)
   3790 		DEBUG_KEV("kev", &kev);
   3791 	XP_SYS_EQ(0, r);
   3792 
   3793 	r = CLOSE(fd);
   3794 	XP_SYS_EQ(0, r);
   3795 	r = CLOSE(kq);
   3796 	XP_SYS_EQ(0, r);
   3797 	free(buf);
   3798 }
   3799 
   3800 /*
   3801  * Unpause from buffer full, kevent() should raise.
   3802  */
   3803 DEF(kqueue_unpause)
   3804 {
   3805 	struct audio_info ai;
   3806 	struct kevent kev;
   3807 	struct timespec ts;
   3808 	int fd;
   3809 	int r;
   3810 	int kq;
   3811 	char *buf;
   3812 	int buflen;
   3813 	u_int blocksize;
   3814 	int hiwat;
   3815 	int lowat;
   3816 
   3817 	TEST("kqueue_unpause");
   3818 
   3819 	/* Non-blocking open */
   3820 	fd = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3821 	REQUIRED_SYS_OK(fd);
   3822 
   3823 	/* Adjust block size and hiwat/lowat to make the test time 1sec */
   3824 	blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3825 	hiwat = 12;		/* 1.5sec */
   3826 	lowat = 4;		/* 0.5sec */
   3827 	AUDIO_INITINFO(&ai);
   3828 	ai.blocksize = blocksize;
   3829 	ai.hiwat = hiwat;
   3830 	ai.lowat = lowat;
   3831 	/* and also set encoding */
   3832 	/*
   3833 	 * XXX NetBSD7 has different results depending on whether the input
   3834 	 * encoding is emulated (AUDIO_ENCODINGFLAG_EMULATED) or not.  It's
   3835 	 * not easy to ensure this situation on all hardware environment.
   3836 	 * On NetBSD9, the result is the same regardless of input encoding.
   3837 	 */
   3838 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "blocksize=%d", blocksize);
   3839 	XP_SYS_EQ(0, r);
   3840 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3841 	if (ai.blocksize != blocksize) {
   3842 		/*
   3843 		 * NetBSD9 can not change the blocksize.  Then,
   3844 		 * adjust using hiwat/lowat.
   3845 		 */
   3846 		blocksize = ai.blocksize;
   3847 		hiwat = howmany(8000 * 1.5, blocksize);
   3848 		lowat = howmany(8000 * 0.5, blocksize);
   3849 	}
   3850 	/* Anyway, set the parameters */
   3851 	AUDIO_INITINFO(&ai);
   3852 	ai.blocksize = blocksize;
   3853 	ai.hiwat = hiwat;
   3854 	ai.lowat = lowat;
   3855 	ai.play.pause = 1;
   3856 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   3857 	XP_SYS_EQ(0, r);
   3858 
   3859 	/* Get the set parameters again */
   3860 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   3861 	XP_SYS_EQ(0, r);
   3862 	DPRINTF("  > blocksize=%d hiwat=%d lowat=%d buffer_size=%d\n",
   3863 	    ai.blocksize, ai.hiwat, ai.lowat, ai.play.buffer_size);
   3864 
   3865 	/* Write until full */
   3866 	buflen = ai.blocksize * ai.hiwat;
   3867 	buf = (char *)malloc(buflen);
   3868 	REQUIRED_IF(buf != NULL);
   3869 	memset(buf, 0xff, buflen);
   3870 	do {
   3871 		r = WRITE(fd, buf, buflen);
   3872 	} while (r == buflen);
   3873 	if (r == -1) {
   3874 		XP_SYS_NG(EAGAIN, r);
   3875 	}
   3876 
   3877 	kq = KQUEUE();
   3878 	XP_SYS_OK(kq);
   3879 
   3880 	EV_SET(&kev, fd, EV_ADD, EVFILT_WRITE, 0, 0, 0);
   3881 	r = KEVENT_SET(kq, &kev, 1);
   3882 	XP_SYS_EQ(0, r);
   3883 
   3884 	/* Unpause */
   3885 	AUDIO_INITINFO(&ai);
   3886 	ai.play.pause = 0;
   3887 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=0");
   3888 	XP_SYS_EQ(0, r);
   3889 
   3890 	/* Check kevent() up to 2sec */
   3891 	ts.tv_sec = 2;
   3892 	ts.tv_nsec = 0;
   3893 	r = KEVENT_POLL(kq, &kev, 1, &ts);
   3894 	if (r >= 1)
   3895 		DEBUG_KEV("kev", &kev);
   3896 	if (netbsd < 8) {
   3897 		/*
   3898 		 * NetBSD7 with EMULATED_FLAG unset has bugs.  Unpausing
   3899 		 * unintentionally clears buffer (and therefore it becomes
   3900 		 * writable) but it doesn't raise EVFILT_WRITE.
   3901 		 */
   3902 	} else {
   3903 		XP_SYS_EQ(1, r);
   3904 	}
   3905 
   3906 	/* Flush it because there is no need to play it */
   3907 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   3908 	XP_SYS_EQ(0, r);
   3909 
   3910 	r = CLOSE(fd);
   3911 	XP_SYS_EQ(0, r);
   3912 	r = CLOSE(kq);
   3913 	XP_SYS_EQ(0, r);
   3914 	free(buf);
   3915 }
   3916 
   3917 /*
   3918  * kevent(2) must not be affected by other audio descriptors.
   3919  */
   3920 DEF(kqueue_simul)
   3921 {
   3922 	struct audio_info ai;
   3923 	struct audio_info ai2;
   3924 	struct kevent kev[2];
   3925 	struct timespec ts;
   3926 	int fd[2];
   3927 	int r;
   3928 	int kq;
   3929 	u_int blocksize;
   3930 	int hiwat;
   3931 	int lowat;
   3932 	char *buf;
   3933 	int buflen;
   3934 
   3935 	TEST("kqueue_simul");
   3936 	if (netbsd < 8) {
   3937 		XP_SKIP("Multiple open is not supported");
   3938 		return;
   3939 	}
   3940 
   3941 	memset(&ts, 0, sizeof(ts));
   3942 
   3943 	/* Make sure that it's not affected by descriptor order */
   3944 	for (int i = 0; i < 2; i++) {
   3945 		int a = i;
   3946 		int b = 1 - i;
   3947 
   3948 		fd[0] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3949 		REQUIRED_SYS_OK(fd[0]);
   3950 		fd[1] = OPEN(devaudio, O_WRONLY | O_NONBLOCK);
   3951 		REQUIRED_SYS_OK(fd[1]);
   3952 
   3953 		/*
   3954 		 * Adjust block size and hiwat/lowat.
   3955 		 * I want to choice suitable blocksize (if possible).
   3956 		 */
   3957 		blocksize = 1000;	/* 1/8 sec in mulaw,1ch,8000Hz */
   3958 		hiwat = 12;		/* 1.5sec */
   3959 		lowat = 4;		/* 0.5sec */
   3960 		AUDIO_INITINFO(&ai);
   3961 		ai.blocksize = blocksize;
   3962 		ai.hiwat = hiwat;
   3963 		ai.lowat = lowat;
   3964 		r = IOCTL(fd[0], AUDIO_SETINFO, &ai, "blocksize=1000");
   3965 		XP_SYS_EQ(0, r);
   3966 		r = IOCTL(fd[0], AUDIO_GETBUFINFO, &ai, "read back blocksize");
   3967 		if (ai.blocksize != blocksize) {
   3968 			/*
   3969 			 * NetBSD9 can not change the blocksize.  Then,
   3970 			 * adjust using hiwat/lowat.
   3971 			 */
   3972 			blocksize = ai.blocksize;
   3973 			hiwat = howmany(8000 * 1.5, blocksize);
   3974 			lowat = howmany(8000 * 0.5, blocksize);
   3975 		}
   3976 		/* Anyway, set the parameters to both */
   3977 		AUDIO_INITINFO(&ai);
   3978 		ai.blocksize = blocksize;
   3979 		ai.hiwat = hiwat;
   3980 		ai.lowat = lowat;
   3981 		ai.play.pause = 1;
   3982 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=1");
   3983 		XP_SYS_EQ(0, r);
   3984 		r = IOCTL(fd[b], AUDIO_SETINFO, &ai, "pause=1");
   3985 		XP_SYS_EQ(0, r);
   3986 
   3987 		/* Write both until full */
   3988 		buflen = ai.blocksize * ai.hiwat;
   3989 		buf = (char *)malloc(buflen);
   3990 		REQUIRED_IF(buf != NULL);
   3991 		memset(buf, 0xff, buflen);
   3992 		/* Write fdA */
   3993 		do {
   3994 			r = WRITE(fd[a], buf, buflen);
   3995 		} while (r == buflen);
   3996 		if (r == -1) {
   3997 			XP_SYS_NG(EAGAIN, r);
   3998 		}
   3999 		/* Write fdB */
   4000 		do {
   4001 			r = WRITE(fd[b], buf, buflen);
   4002 		} while (r == buflen);
   4003 		if (r == -1) {
   4004 			XP_SYS_NG(EAGAIN, r);
   4005 		}
   4006 
   4007 		/* Get fdB's initial seek for later */
   4008 		r = IOCTL(fd[b], AUDIO_GETBUFINFO, &ai2, "");
   4009 		XP_SYS_EQ(0, r);
   4010 
   4011 		kq = KQUEUE();
   4012 		XP_SYS_OK(kq);
   4013 
   4014 		/* Both aren't raised at this point */
   4015 		EV_SET(&kev[0], fd[a], EV_ADD, EVFILT_WRITE, 0, 0, 0);
   4016 		EV_SET(&kev[1], fd[b], EV_ADD, EVFILT_WRITE, 0, 0, 0);
   4017 		r = KEVENT_SET(kq, kev, 2);
   4018 		XP_SYS_EQ(0, r);
   4019 
   4020 		/* Unpause only fdA */
   4021 		AUDIO_INITINFO(&ai);
   4022 		ai.play.pause = 0;
   4023 		r = IOCTL(fd[a], AUDIO_SETINFO, &ai, "pause=0");
   4024 		XP_SYS_EQ(0, r);
   4025 
   4026 		/* kevent() up to 2sec */
   4027 		ts.tv_sec = 2;
   4028 		ts.tv_nsec = 0;
   4029 		r = KEVENT_POLL(kq, &kev[0], 1, &ts);
   4030 		if (r >= 1)
   4031 			DEBUG_KEV("kev", &kev[0]);
   4032 		/* fdA should raise */
   4033 		XP_SYS_EQ(1, r);
   4034 		XP_EQ(fd[a], kev[0].ident);
   4035 
   4036 		/* Make sure that fdB keeps whole data */
   4037 		r = IOCTL(fd[b], AUDIO_GETBUFINFO, &ai, "");
   4038 		XP_EQ(ai2.play.seek, ai.play.seek);
   4039 
   4040 		/* Flush it because there is no need to play it */
   4041 		r = IOCTL(fd[0], AUDIO_FLUSH, NULL, "");
   4042 		XP_SYS_EQ(0, r);
   4043 		r = IOCTL(fd[1], AUDIO_FLUSH, NULL, "");
   4044 		XP_SYS_EQ(0, r);
   4045 
   4046 		r = CLOSE(fd[0]);
   4047 		XP_SYS_EQ(0, r);
   4048 		r = CLOSE(fd[1]);
   4049 		XP_SYS_EQ(0, r);
   4050 		r = CLOSE(kq);
   4051 		XP_SYS_EQ(0, r);
   4052 		free(buf);
   4053 
   4054 		xxx_close_wait();
   4055 	}
   4056 }
   4057 
   4058 /* Shared data between threads for ioctl_while_write */
   4059 struct ioctl_while_write_data {
   4060 	int fd;
   4061 	struct timeval start;
   4062 	int terminated;
   4063 };
   4064 
   4065 /* Test thread for ioctl_while_write */
   4066 void *thread_ioctl_while_write(void *);
   4067 void *
   4068 thread_ioctl_while_write(void *arg)
   4069 {
   4070 	struct ioctl_while_write_data *data = arg;
   4071 	struct timeval now, res;
   4072 	struct audio_info ai;
   4073 	int r;
   4074 
   4075 	/* If 0.5 seconds have elapsed since writing, assume it's blocked */
   4076 	do {
   4077 		usleep(100);
   4078 		gettimeofday(&now, NULL);
   4079 		timersub(&now, &data->start, &res);
   4080 	} while (res.tv_usec < 500000);
   4081 
   4082 	/* Then, do ioctl() */
   4083 	r = IOCTL(data->fd, AUDIO_GETBUFINFO, &ai, "");
   4084 	XP_SYS_EQ(0, r);
   4085 
   4086 	/* Terminate */
   4087 	data->terminated = 1;
   4088 
   4089 	/* Resume write() by unpause */
   4090 	AUDIO_INITINFO(&ai);
   4091 	if (netbsd < 8) {
   4092 		/*
   4093 		 * XXX NetBSD7 has bugs and it cannot be unpaused.
   4094 		 * However, it also has another bug and it clears buffer
   4095 		 * when encoding is changed.  I use it. :-P
   4096 		 */
   4097 		ai.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
   4098 	}
   4099 	ai.play.pause = 0;
   4100 	r = IOCTL(data->fd, AUDIO_SETINFO, &ai, "pause=0");
   4101 	XP_SYS_EQ(0, r);
   4102 
   4103 	return NULL;
   4104 }
   4105 
   4106 /*
   4107  * ioctl(2) can be issued while write(2)-ing.
   4108  */
   4109 DEF(ioctl_while_write)
   4110 {
   4111 	struct audio_info ai;
   4112 	struct ioctl_while_write_data data0, *data;
   4113 	char buf[8000];	/* 1sec in mulaw,1ch,8000Hz */
   4114 	pthread_t tid;
   4115 	int r;
   4116 
   4117 	TEST("ioctl_while_write");
   4118 
   4119 	data = &data0;
   4120 	memset(data, 0, sizeof(*data));
   4121 	memset(buf, 0xff, sizeof(buf));
   4122 
   4123 	data->fd = OPEN(devaudio, O_WRONLY);
   4124 	REQUIRED_SYS_OK(data->fd);
   4125 
   4126 	/* Pause to block write(2)ing */
   4127 	AUDIO_INITINFO(&ai);
   4128 	ai.play.pause = 1;
   4129 	r = IOCTL(data->fd, AUDIO_SETINFO, &ai, "pause=1");
   4130 	XP_SYS_EQ(0, r);
   4131 
   4132 	gettimeofday(&data->start, NULL);
   4133 
   4134 	pthread_create(&tid, NULL, thread_ioctl_while_write, data);
   4135 
   4136 	/* Write until blocking */
   4137 	for (;;) {
   4138 		r = WRITE(data->fd, buf, sizeof(buf));
   4139 		if (data->terminated)
   4140 			break;
   4141 		XP_SYS_EQ(sizeof(buf), r);
   4142 
   4143 		/* Update written time */
   4144 		gettimeofday(&data->start, NULL);
   4145 	}
   4146 
   4147 	pthread_join(tid, NULL);
   4148 
   4149 	/* Flush */
   4150 	r = IOCTL(data->fd, AUDIO_FLUSH, NULL, "");
   4151 	XP_SYS_EQ(0, r);
   4152 	r = CLOSE(data->fd);
   4153 	XP_SYS_EQ(0, r);
   4154 }
   4155 
   4156 volatile int sigio_caught;
   4157 void
   4158 signal_FIOASYNC(int signo)
   4159 {
   4160 	if (signo == SIGIO) {
   4161 		sigio_caught = 1;
   4162 		DPRINTF("  > %d: pid %d got SIGIO\n", __LINE__, (int)getpid());
   4163 	}
   4164 }
   4165 
   4166 /*
   4167  * FIOASYNC between two descriptors should be split.
   4168  */
   4169 DEF(FIOASYNC_reset)
   4170 {
   4171 	int fd0, fd1;
   4172 	int r;
   4173 	int val;
   4174 
   4175 	TEST("FIOASYNC_reset");
   4176 	if (netbsd < 8) {
   4177 		XP_SKIP("Multiple open is not supported");
   4178 		return;
   4179 	}
   4180 
   4181 	/* The first one opens */
   4182 	fd0 = OPEN(devaudio, O_WRONLY);
   4183 	REQUIRED_SYS_OK(fd0);
   4184 
   4185 	/* The second one opens, enables ASYNC, and closes */
   4186 	fd1 = OPEN(devaudio, O_WRONLY);
   4187 	REQUIRED_SYS_OK(fd1);
   4188 	val = 1;
   4189 	r = IOCTL(fd1, FIOASYNC, &val, "on");
   4190 	XP_SYS_EQ(0, r);
   4191 	r = CLOSE(fd1);
   4192 	XP_SYS_EQ(0, r);
   4193 
   4194 	/* Again, the second one opens and enables ASYNC */
   4195 	fd1 = OPEN(devaudio, O_WRONLY);
   4196 	REQUIRED_SYS_OK(fd1);
   4197 	val = 1;
   4198 	r = IOCTL(fd1, FIOASYNC, &val, "on");
   4199 	XP_SYS_EQ(0, r);	/* NetBSD8 fails */
   4200 	r = CLOSE(fd1);
   4201 	XP_SYS_EQ(0, r);
   4202 	r = CLOSE(fd0);
   4203 	XP_SYS_EQ(0, r);
   4204 }
   4205 
   4206 /*
   4207  * Whether SIGIO is emitted on plyaback.
   4208  * XXX I don't understand conditions that NetBSD7 emits signal.
   4209  */
   4210 DEF(FIOASYNC_play_signal)
   4211 {
   4212 	struct audio_info ai;
   4213 	int r;
   4214 	int fd;
   4215 	int val;
   4216 	char *data;
   4217 	int i;
   4218 
   4219 	TEST("FIOASYNC_play_signal");
   4220 	if (hw_canplay() == 0) {
   4221 		XP_SKIP("This test is only for playable device");
   4222 		return;
   4223 	}
   4224 
   4225 	signal(SIGIO, signal_FIOASYNC);
   4226 	sigio_caught = 0;
   4227 
   4228 	fd = OPEN(devaudio, O_WRONLY);
   4229 	REQUIRED_SYS_OK(fd);
   4230 
   4231 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4232 	REQUIRED_SYS_EQ(0, r);
   4233 	REQUIRED_IF(ai.blocksize != 0);
   4234 	data = (char *)malloc(ai.blocksize);
   4235 	REQUIRED_IF(data != NULL);
   4236 	memset(data, 0xff, ai.blocksize);
   4237 
   4238 	val = 1;
   4239 	r = IOCTL(fd, FIOASYNC, &val, "on");
   4240 	XP_SYS_EQ(0, r);
   4241 
   4242 	r = WRITE(fd, data, ai.blocksize);
   4243 	XP_SYS_EQ(ai.blocksize, r);
   4244 
   4245 	/* Waits signal until 1sec */
   4246 	for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4247 		usleep(10000);
   4248 	}
   4249 	signal(SIGIO, SIG_IGN);
   4250 	XP_EQ(1, sigio_caught);
   4251 
   4252 	r = CLOSE(fd);
   4253 	XP_SYS_EQ(0, r);
   4254 
   4255 	free(data);
   4256 	signal(SIGIO, SIG_IGN);
   4257 	sigio_caught = 0;
   4258 }
   4259 
   4260 /*
   4261  * Whether SIGIO is emitted on recording.
   4262  */
   4263 DEF(FIOASYNC_rec_signal)
   4264 {
   4265 	char buf[10];
   4266 	int r;
   4267 	int fd;
   4268 	int val;
   4269 	int i;
   4270 
   4271 	TEST("FIOASYNC_rec_signal");
   4272 	if (hw_canrec() == 0) {
   4273 		XP_SKIP("This test is only for recordable device");
   4274 		return;
   4275 	}
   4276 
   4277 	signal(SIGIO, signal_FIOASYNC);
   4278 	sigio_caught = 0;
   4279 
   4280 	fd = OPEN(devaudio, O_RDONLY);
   4281 	REQUIRED_SYS_OK(fd);
   4282 
   4283 	val = 1;
   4284 	r = IOCTL(fd, FIOASYNC, &val, "on");
   4285 	XP_SYS_EQ(0, r);
   4286 
   4287 	r = READ(fd, buf, sizeof(buf));
   4288 	XP_SYS_EQ(sizeof(buf), r);
   4289 
   4290 	/* Wait signal until 1sec */
   4291 	for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4292 		usleep(10000);
   4293 	}
   4294 	signal(SIGIO, SIG_IGN);
   4295 	XP_EQ(1, sigio_caught);
   4296 
   4297 	r = CLOSE(fd);
   4298 	XP_SYS_EQ(0, r);
   4299 
   4300 	signal(SIGIO, SIG_IGN);
   4301 	sigio_caught = 0;
   4302 }
   4303 
   4304 /*
   4305  * FIOASYNC doesn't affect other descriptor.
   4306  * For simplify, test only for playback...
   4307  */
   4308 DEF(FIOASYNC_multi)
   4309 {
   4310 	struct audio_info ai;
   4311 	char *buf;
   4312 	char pipebuf[1];
   4313 	int r;
   4314 	int i;
   4315 	int fd1;
   4316 	int fd2;
   4317 	int pd[2];
   4318 	int val;
   4319 	pid_t pid;
   4320 	int status;
   4321 
   4322 	TEST("FIOASYNC_multi");
   4323 	if (netbsd < 8) {
   4324 		XP_SKIP("Multiple open is not supported");
   4325 		return;
   4326 	}
   4327 	if (hw_canplay() == 0) {
   4328 		XP_SKIP("This test is only for playable device");
   4329 		return;
   4330 	}
   4331 
   4332 	/* Pipe used between parent and child */
   4333 	r = pipe(pd);
   4334 	REQUIRED_SYS_EQ(0, r);
   4335 
   4336 	fd1 = OPEN(devaudio, O_WRONLY);
   4337 	REQUIRED_SYS_OK(fd1);
   4338 	fd2 = OPEN(devaudio, O_WRONLY);
   4339 	REQUIRED_SYS_OK(fd2);
   4340 
   4341 	/* Pause fd2 */
   4342 	AUDIO_INITINFO(&ai);
   4343 	ai.play.pause = 1;
   4344 	r = IOCTL(fd2, AUDIO_SETINFO, &ai, "pause");
   4345 	REQUIRED_SYS_EQ(0, r);
   4346 
   4347 	/* Fill both */
   4348 	r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   4349 	REQUIRED_SYS_EQ(0, r);
   4350 	REQUIRED_IF(ai.blocksize != 0);
   4351 	buf = (char *)malloc(ai.blocksize);
   4352 	REQUIRED_IF(buf != NULL);
   4353 	memset(buf, 0xff, ai.blocksize);
   4354 	r = WRITE(fd1, buf, ai.blocksize);
   4355 	XP_SYS_EQ(ai.blocksize, r);
   4356 
   4357 	sigio_caught = 0;
   4358 	val = 1;
   4359 
   4360 	fflush(stdout);
   4361 	fflush(stderr);
   4362 	pid = fork();
   4363 	if (pid == -1) {
   4364 		REQUIRED_SYS_OK(pid);
   4365 	}
   4366 	if (pid == 0) {
   4367 		/* Child */
   4368 		close(fd1);
   4369 
   4370 		/* Child enables ASYNC on fd2 */
   4371 		signal(SIGIO, signal_FIOASYNC);
   4372 		r = IOCTL(fd2, FIOASYNC, &val, "on");
   4373 		/* It cannot count errors because here is a child process */
   4374 		/* XP_SYS_EQ(0, r); */
   4375 
   4376 		/*
   4377 		 * Waits signal until 1sec.
   4378 		 * But fd2 is paused so it should never raise.
   4379 		 */
   4380 		for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4381 			usleep(10000);
   4382 		}
   4383 		signal(SIGIO, SIG_IGN);
   4384 		pipebuf[0] = sigio_caught;
   4385 		/* This is not WRITE() macro here */
   4386 		write(pd[1], pipebuf, sizeof(pipebuf));
   4387 
   4388 		/* XXX? */
   4389 		close(fd2);
   4390 		sleep(1);
   4391 		exit(0);
   4392 	} else {
   4393 		/* Parent */
   4394 		DPRINTF("  > fork() = %d\n", (int)pid);
   4395 
   4396 		/* Parent enables ASYNC on fd1 */
   4397 		signal(SIGIO, signal_FIOASYNC);
   4398 		r = IOCTL(fd1, FIOASYNC, &val, "on");
   4399 		XP_SYS_EQ(0, r);
   4400 
   4401 		/* Waits signal until 1sec */
   4402 		for (i = 0; i < 100 && sigio_caught == 0; i++) {
   4403 			usleep(10000);
   4404 		}
   4405 		signal(SIGIO, SIG_IGN);
   4406 		XP_EQ(1, sigio_caught);
   4407 
   4408 		/* Then read child's result from pipe */
   4409 		r = read(pd[0], pipebuf, sizeof(pipebuf));
   4410 		if (r != 1) {
   4411 			XP_FAIL("reading from child failed");
   4412 		}
   4413 		DPRINTF("  > child's sigio_cauht = %d\n", pipebuf[0]);
   4414 		XP_EQ(0, pipebuf[0]);
   4415 
   4416 		waitpid(pid, &status, 0);
   4417 	}
   4418 
   4419 	r = CLOSE(fd1);
   4420 	XP_SYS_EQ(0, r);
   4421 	r = CLOSE(fd2);
   4422 	XP_SYS_EQ(0, r);
   4423 
   4424 	signal(SIGIO, SIG_IGN);
   4425 	sigio_caught = 0;
   4426 	free(buf);
   4427 }
   4428 
   4429 /*
   4430  * Check AUDIO_WSEEK behavior.
   4431  */
   4432 DEF(AUDIO_WSEEK)
   4433 {
   4434 	char buf[4];
   4435 	struct audio_info ai;
   4436 	int r;
   4437 	int fd;
   4438 	u_long n;
   4439 
   4440 	TEST("AUDIO_WSEEK");
   4441 
   4442 	fd = OPEN(devaudio, O_WRONLY);
   4443 	REQUIRED_SYS_OK(fd);
   4444 
   4445 	/* Pause to count sample data */
   4446 	AUDIO_INITINFO(&ai);
   4447 	ai.play.pause = 1;
   4448 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause=1");
   4449 	REQUIRED_SYS_EQ(0, r);
   4450 
   4451 	/* On the initial state, it should be 0 bytes */
   4452 	n = 0;
   4453 	r = IOCTL(fd, AUDIO_WSEEK, &n, "");
   4454 	XP_SYS_EQ(0, r);
   4455 	XP_EQ(0, n);
   4456 
   4457 	/* When writing 4 bytes, it should be 4 bytes */
   4458 	memset(buf, 0xff, sizeof(buf));
   4459 	r = WRITE(fd, buf, sizeof(buf));
   4460 	REQUIRED_EQ(sizeof(buf), r);
   4461 	r = IOCTL(fd, AUDIO_WSEEK, &n, "");
   4462 	XP_SYS_EQ(0, r);
   4463 	if (netbsd < 9) {
   4464 		/*
   4465 		 * On NetBSD7, it will return 0.
   4466 		 * Perhaps, WSEEK returns the number of pustream bytes but
   4467 		 * data has already advanced...
   4468 		 */
   4469 		XP_EQ(0, n);
   4470 	} else {
   4471 		/* Data less than one block remains here */
   4472 		XP_EQ(4, n);
   4473 	}
   4474 
   4475 	r = CLOSE(fd);
   4476 	XP_SYS_EQ(0, r);
   4477 }
   4478 
   4479 /*
   4480  * Check AUDIO_SETFD behavior for O_*ONLY descriptor.
   4481  * On NetBSD7, SETFD modify audio layer's state (and MD driver's state)
   4482  * regardless of open mode.  GETFD obtains audio layer's duplex.
   4483  * On NetBSD9, SETFD is obsoleted.  GETFD obtains hardware's duplex.
   4484  */
   4485 void
   4486 test_AUDIO_SETFD_xxONLY(int openmode)
   4487 {
   4488 	struct audio_info ai;
   4489 	int r;
   4490 	int fd;
   4491 	int n;
   4492 
   4493 	TEST("AUDIO_SETFD_%s", openmode_str[openmode] + 2);
   4494 	if (openmode == O_RDONLY && hw_canrec() == 0) {
   4495 		XP_SKIP("This test is for recordable device");
   4496 		return;
   4497 	}
   4498 	if (openmode == O_WRONLY && hw_canplay() == 0) {
   4499 		XP_SKIP("This test is for playable device");
   4500 		return;
   4501 	}
   4502 
   4503 	fd = OPEN(devaudio, openmode);
   4504 	REQUIRED_SYS_OK(fd);
   4505 
   4506 	/*
   4507 	 * Just after open(2),
   4508 	 * - On NetBSD7, it's always half-duplex.
   4509 	 * - On NetBSD9, it's the same as hardware one regardless of openmode.
   4510 	 */
   4511 	n = 0;
   4512 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4513 	XP_SYS_EQ(0, r);
   4514 	if (netbsd < 9) {
   4515 		XP_EQ(0, n);
   4516 	} else {
   4517 		XP_EQ(hw_fulldup(), n);
   4518 	}
   4519 
   4520 	/*
   4521 	 * When trying to set to full-duplex,
   4522 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4523 	 *   will fail if the hardware is half-duplex.
   4524 	 * - On NetBSD9, it will always succeed but will not be modified.
   4525 	 */
   4526 	n = 1;
   4527 	r = IOCTL(fd, AUDIO_SETFD, &n, "on");
   4528 	if (netbsd < 8) {
   4529 		if (hw_fulldup()) {
   4530 			XP_SYS_EQ(0, r);
   4531 		} else {
   4532 			XP_SYS_NG(ENOTTY, r);
   4533 		}
   4534 	} else if (netbsd == 8) {
   4535 		XP_FAIL("expected result is unknown");
   4536 	} else {
   4537 		XP_SYS_EQ(0, r);
   4538 	}
   4539 
   4540 	/*
   4541 	 * When obtain it,
   4542 	 * - On NetBSD7, it will be 1 if the hardware is full-duplex or
   4543 	 *   0 if half-duplex.
   4544 	 * - On NetBSD9, it will never be changed because it's the hardware
   4545 	 *   property.
   4546 	 */
   4547 	n = 0;
   4548 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4549 	XP_SYS_EQ(0, r);
   4550 	if (netbsd < 8) {
   4551 		XP_EQ(hw_fulldup(), n);
   4552 	} else if (netbsd == 8) {
   4553 		XP_FAIL("expected result is unknown");
   4554 	} else {
   4555 		XP_EQ(hw_fulldup(), n);
   4556 	}
   4557 
   4558 	/* Some track parameters like ai.*.open should not change */
   4559 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4560 	XP_SYS_EQ(0, r);
   4561 	XP_EQ(mode2play(openmode), ai.play.open);
   4562 	XP_EQ(mode2rec(openmode), ai.record.open);
   4563 
   4564 	/*
   4565 	 * When trying to set to half-duplex,
   4566 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4567 	 *   it will succeed with nothing happens.
   4568 	 * - On NetBSD9, it will always succeed but nothing happens.
   4569 	 */
   4570 	n = 0;
   4571 	r = IOCTL(fd, AUDIO_SETFD, &n, "off");
   4572 	XP_SYS_EQ(0, r);
   4573 
   4574 	/*
   4575 	 * When obtain it again,
   4576 	 * - On NetBSD7, it will be 0 if the hardware is full-duplex, or
   4577 	 *   still 0 if half-duplex.
   4578 	 * - On NetBSD9, it should not change.
   4579 	 */
   4580 	n = 0;
   4581 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4582 	XP_SYS_EQ(0, r);
   4583 	if (netbsd < 9) {
   4584 		XP_EQ(0, n);
   4585 	} else {
   4586 		XP_EQ(hw_fulldup(), n);
   4587 	}
   4588 
   4589 	/* Some track parameters like ai.*.open should not change */
   4590 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4591 	XP_SYS_EQ(0, r);
   4592 	XP_EQ(mode2play(openmode), ai.play.open);
   4593 	XP_EQ(mode2rec(openmode), ai.record.open);
   4594 
   4595 	r = CLOSE(fd);
   4596 	XP_SYS_EQ(0, r);
   4597 }
   4598 DEF(AUDIO_SETFD_RDONLY)	{ test_AUDIO_SETFD_xxONLY(O_RDONLY); }
   4599 DEF(AUDIO_SETFD_WRONLY)	{ test_AUDIO_SETFD_xxONLY(O_WRONLY); }
   4600 
   4601 /*
   4602  * Check AUDIO_SETFD behavior for O_RDWR descriptor.
   4603  */
   4604 DEF(AUDIO_SETFD_RDWR)
   4605 {
   4606 	struct audio_info ai;
   4607 	int r;
   4608 	int fd;
   4609 	int n;
   4610 
   4611 	TEST("AUDIO_SETFD_RDWR");
   4612 	if (!hw_fulldup()) {
   4613 		XP_SKIP("This test is only for full-duplex device");
   4614 		return;
   4615 	}
   4616 
   4617 	fd = OPEN(devaudio, O_RDWR);
   4618 	REQUIRED_SYS_OK(fd);
   4619 
   4620 	/*
   4621 	 * - audio(4) manpage until NetBSD7 said "If a full-duplex capable
   4622 	 *   audio device is opened for both reading and writing it will
   4623 	 *   start in half-duplex play mode", but implementation doesn't
   4624 	 *   seem to follow it.  It returns full-duplex.
   4625 	 * - On NetBSD9, it should return full-duplex on full-duplex, or
   4626 	 *   half-duplex on half-duplex.
   4627 	 */
   4628 	n = 0;
   4629 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4630 	XP_SYS_EQ(0, r);
   4631 	XP_EQ(hw_fulldup(), n);
   4632 
   4633 	/*
   4634 	 * When trying to set to full-duplex,
   4635 	 * - On NetBSD7, it will succeed with nothing happens if full-duplex,
   4636 	 *   or will fail if half-duplex.
   4637 	 * - On NetBSD9, it will always succeed with nothing happens.
   4638 	 */
   4639 	n = 1;
   4640 	r = IOCTL(fd, AUDIO_SETFD, &n, "on");
   4641 	if (netbsd < 9) {
   4642 		if (hw_fulldup()) {
   4643 			XP_SYS_EQ(0, r);
   4644 		} else {
   4645 			XP_SYS_NG(ENOTTY, r);
   4646 		}
   4647 	} else {
   4648 		XP_SYS_EQ(0, r);
   4649 	}
   4650 
   4651 	/* When obtains it, it retuns half/full-duplex as is */
   4652 	n = 0;
   4653 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4654 	XP_SYS_EQ(0, r);
   4655 	XP_EQ(hw_fulldup(), n);
   4656 
   4657 	/* Some track parameters like ai.*.open should not change */
   4658 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4659 	XP_SYS_EQ(0, r);
   4660 	XP_EQ(1, ai.play.open);
   4661 	XP_EQ(mode2rec(O_RDWR), ai.record.open);
   4662 
   4663 	/*
   4664 	 * When trying to set to half-duplex,
   4665 	 * - On NetBSD7, it will succeed if the hardware is full-duplex, or
   4666 	 *   it will succeed with nothing happens.
   4667 	 * - On NetBSD9, it will always succeed but nothing happens.
   4668 	 */
   4669 	n = 0;
   4670 	r = IOCTL(fd, AUDIO_SETFD, &n, "off");
   4671 	if (netbsd < 8) {
   4672 		XP_SYS_EQ(0, r);
   4673 	} else if (netbsd == 8) {
   4674 		XP_FAIL("expected result is unknown");
   4675 	} else {
   4676 		XP_SYS_EQ(0, r);
   4677 	}
   4678 
   4679 	/*
   4680 	 * When obtain it again,
   4681 	 * - On NetBSD7, it will be 0 if the hardware is full-duplex, or
   4682 	 *   still 0 if half-duplex.
   4683 	 * - On NetBSD9, it should be 1 if the hardware is full-duplex, or
   4684 	 *   0 if half-duplex.
   4685 	 */
   4686 	n = 0;
   4687 	r = IOCTL(fd, AUDIO_GETFD, &n, "");
   4688 	XP_SYS_EQ(0, r);
   4689 	if (netbsd < 8) {
   4690 		XP_EQ(0, n);
   4691 	} else if (netbsd == 8) {
   4692 		XP_FAIL("expected result is unknown");
   4693 	} else {
   4694 		XP_EQ(hw_fulldup(), n);
   4695 	}
   4696 
   4697 	/* Some track parameters like ai.*.open should not change */
   4698 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4699 	XP_SYS_EQ(0, r);
   4700 	XP_EQ(1, ai.play.open);
   4701 	XP_EQ(mode2rec(O_RDWR), ai.record.open);
   4702 
   4703 	r = CLOSE(fd);
   4704 	XP_SYS_EQ(0, r);
   4705 }
   4706 
   4707 /*
   4708  * Check AUDIO_GETINFO.eof behavior.
   4709  */
   4710 DEF(AUDIO_GETINFO_eof)
   4711 {
   4712 	struct audio_info ai;
   4713 	char buf[4];
   4714 	int r;
   4715 	int fd, fd1;
   4716 
   4717 	TEST("AUDIO_GETINFO_eof");
   4718 	if (hw_canplay() == 0) {
   4719 		XP_SKIP("This test is for playable device");
   4720 		return;
   4721 	}
   4722 
   4723 	fd = OPEN(devaudio, O_RDWR);
   4724 	REQUIRED_SYS_OK(fd);
   4725 
   4726 	/* Pause to make no sound */
   4727 	AUDIO_INITINFO(&ai);
   4728 	ai.play.pause = 1;
   4729 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "pause");
   4730 	REQUIRED_SYS_EQ(0, r);
   4731 
   4732 	/* It should be 0 initially */
   4733 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4734 	XP_SYS_EQ(0, r);
   4735 	XP_EQ(0, ai.play.eof);
   4736 	XP_EQ(0, ai.record.eof);
   4737 
   4738 	/* Writing zero bytes should increment it */
   4739 	r = WRITE(fd, &r, 0);
   4740 	REQUIRED_SYS_OK(r);
   4741 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4742 	XP_SYS_EQ(0, r);
   4743 	XP_EQ(1, ai.play.eof);
   4744 	XP_EQ(0, ai.record.eof);
   4745 
   4746 	/* Writing one ore more bytes should noto increment it */
   4747 	memset(buf, 0xff, sizeof(buf));
   4748 	r = WRITE(fd, buf, sizeof(buf));
   4749 	REQUIRED_SYS_OK(r);
   4750 	memset(&ai, 0, sizeof(ai));
   4751 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4752 	XP_SYS_EQ(0, r);
   4753 	XP_EQ(1, ai.play.eof);
   4754 	XP_EQ(0, ai.record.eof);
   4755 
   4756 	/* Writing zero bytes again should increment it */
   4757 	r = WRITE(fd, buf, 0);
   4758 	REQUIRED_SYS_OK(r);
   4759 	memset(&ai, 0, sizeof(ai));
   4760 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4761 	XP_SYS_EQ(0, r);
   4762 	XP_EQ(2, ai.play.eof);
   4763 	XP_EQ(0, ai.record.eof);
   4764 
   4765 	/* Reading zero bytes should not increment it */
   4766 	if (hw_fulldup()) {
   4767 		r = READ(fd, buf, 0);
   4768 		REQUIRED_SYS_OK(r);
   4769 		memset(&ai, 0, sizeof(ai));
   4770 		r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4771 		XP_SYS_EQ(0, r);
   4772 		XP_EQ(2, ai.play.eof);
   4773 		XP_EQ(0, ai.record.eof);
   4774 	}
   4775 
   4776 	/* should not interfere with other descriptor */
   4777 	if (netbsd >= 8) {
   4778 		fd1 = OPEN(devaudio, O_RDWR);
   4779 		REQUIRED_SYS_OK(fd1);
   4780 		memset(&ai, 0, sizeof(ai));
   4781 		r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   4782 		XP_SYS_EQ(0, r);
   4783 		XP_EQ(0, ai.play.eof);
   4784 		XP_EQ(0, ai.record.eof);
   4785 		r = CLOSE(fd1);
   4786 		XP_SYS_EQ(0, r);
   4787 	}
   4788 
   4789 	r = CLOSE(fd);
   4790 	XP_SYS_EQ(0, r);
   4791 
   4792 	xxx_close_wait();
   4793 
   4794 	/* When reopen, it should reset the counter */
   4795 	fd = OPEN(devaudio, O_RDWR);
   4796 	REQUIRED_SYS_OK(fd);
   4797 
   4798 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   4799 	XP_SYS_EQ(0, r);
   4800 	XP_EQ(0, ai.play.eof);
   4801 	XP_EQ(0, ai.record.eof);
   4802 
   4803 	r = CLOSE(fd);
   4804 	XP_SYS_EQ(0, r);
   4805 }
   4806 
   4807 /*
   4808  * Check relationship between openmode and mode set by AUDIO_SETINFO.
   4809  */
   4810 void
   4811 test_AUDIO_SETINFO_mode(int openmode, int index, int setmode, int expected)
   4812 {
   4813 	struct audio_info ai;
   4814 	char buf[10];
   4815 	int inimode;
   4816 	int r;
   4817 	int fd;
   4818 	bool canwrite;
   4819 	bool canread;
   4820 
   4821 	/* index was passed only for displaying here */
   4822 	TEST("AUDIO_SETINFO_mode_%s_%d", openmode_str[openmode] + 2, index);
   4823 	if (mode2aumode(openmode) == 0) {
   4824 		XP_SKIP("Operation not allowed on this hardware property");
   4825 		return;
   4826 	}
   4827 
   4828 	inimode = mode2aumode(openmode);
   4829 
   4830 	fd = OPEN(devaudio, openmode);
   4831 	REQUIRED_SYS_OK(fd);
   4832 
   4833 	/* When just after opening */
   4834 	memset(&ai, 0, sizeof(ai));
   4835 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   4836 	REQUIRED_SYS_EQ(0, r);
   4837 	XP_EQ(inimode, ai.mode);
   4838 	XP_EQ(mode2play(openmode), ai.play.open);
   4839 	XP_EQ(mode2rec(openmode),  ai.record.open);
   4840 	XP_NE(0, ai.play.buffer_size);
   4841 	XP_NE(0, ai.record.buffer_size);
   4842 
   4843 	/* Change mode (and pause here) */
   4844 	ai.mode = setmode;
   4845 	ai.play.pause = 1;
   4846 	ai.record.pause = 1;
   4847 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "mode");
   4848 	XP_SYS_EQ(0, r);
   4849 	if (r == 0) {
   4850 		r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   4851 		XP_SYS_EQ(0, r);
   4852 		XP_EQ(expected, ai.mode);
   4853 
   4854 		/* It seems to keep the initial openmode regardless of mode */
   4855 		XP_EQ(mode2play(openmode), ai.play.open);
   4856 		XP_EQ(mode2rec(openmode), ai.record.open);
   4857 		XP_NE(0, ai.play.buffer_size);
   4858 		XP_NE(0, ai.record.buffer_size);
   4859 	}
   4860 
   4861 	/*
   4862 	 * On NetBSD7, whether writable depends openmode when open.
   4863 	 * On NetBSD9, whether writable should depend inimode when open.
   4864 	 * Modifying after open should not affect this mode.
   4865 	 */
   4866 	if (netbsd < 9) {
   4867 		canwrite = (openmode != O_RDONLY);
   4868 	} else {
   4869 		canwrite = ((inimode & AUMODE_PLAY) != 0);
   4870 	}
   4871 	r = WRITE(fd, buf, 0);
   4872 	if (canwrite) {
   4873 		XP_SYS_EQ(0, r);
   4874 	} else {
   4875 		XP_SYS_NG(EBADF, r);
   4876 	}
   4877 
   4878 	/*
   4879 	 * On NetBSD7, whether readable depends openmode when open.
   4880 	 * On NetBSD9, whether readable should depend inimode when open.
   4881 	 * Modifying after open should not affect this mode.
   4882 	 */
   4883 	if (netbsd < 9) {
   4884 		canread = (openmode != O_WRONLY);
   4885 	} else {
   4886 		canread = ((inimode & AUMODE_RECORD) != 0);
   4887 	}
   4888 	r = READ(fd, buf, 0);
   4889 	if (canread) {
   4890 		XP_SYS_EQ(0, r);
   4891 	} else {
   4892 		XP_SYS_NG(EBADF, r);
   4893 	}
   4894 
   4895 	r = CLOSE(fd);
   4896 	XP_SYS_EQ(0, r);
   4897 }
   4898 /*
   4899  * XXX hmm... it's too complex
   4900  */
   4901 /* shortcut for table form */
   4902 #define P	AUMODE_PLAY
   4903 #define A	AUMODE_PLAY_ALL
   4904 #define R	AUMODE_RECORD
   4905 struct setinfo_mode_t {
   4906 	int setmode;	/* mode used in SETINFO */
   4907 	int expmode7;	/* expected mode on NetBSD7 */
   4908 	int expmode9;	/* expected mode on NetBSD9 */
   4909 };
   4910 /*
   4911  * The following tables show this operation on NetBSD7 is almost 'undefined'.
   4912  * In contrast, NetBSD9 never changes mode by AUDIO_SETINFO except
   4913  * AUMODE_PLAY_ALL.
   4914  *
   4915  * setmode == 0 and 8 are out of range and invalid input samples.
   4916  * But NetBSD7 seems to accept it as is.
   4917  */
   4918 struct setinfo_mode_t table_SETINFO_mode_O_RDONLY[] = {
   4919 	/* setmode	expmode7	expmode9 */
   4920 	{     0,	     0,		 R    },
   4921 	{     P,	     P,		 R    },
   4922 	{   A  ,	   A|P,		 R    },
   4923 	{   A|P,	   A|P,		 R    },
   4924 	{ R    ,	 R    ,		 R    },
   4925 	{ R|  P,	     P,		 R    },
   4926 	{ R|A  ,	   A|P,		 R    },
   4927 	{ R|A|P,	   A|P,		 R    },
   4928 	{     8,	     8,		 R    },
   4929 };
   4930 struct setinfo_mode_t table_SETINFO_mode_O_WRONLY[] = {
   4931 	/* setmode	expmode7	expmode9 */
   4932 	{     0,	     0,		     P },
   4933 	{     P,	     P,		     P },
   4934 	{   A  ,	   A|P,		   A|P },
   4935 	{   A|P,	   A|P,		   A|P },
   4936 	{ R    ,	 R    ,		     P },
   4937 	{ R|  P,	     P,		     P },
   4938 	{ R|A  ,	   A|P,		   A|P },
   4939 	{ R|A|P,	   A|P,		   A|P },
   4940 	{     8,	     8,		     P },
   4941 };
   4942 #define f(openmode, index)	do {					\
   4943 	struct setinfo_mode_t *table = table_SETINFO_mode_##openmode;	\
   4944 	int setmode = table[index].setmode;				\
   4945 	int expected = (netbsd < 9)					\
   4946 	    ? table[index].expmode7					\
   4947 	    : table[index].expmode9;					\
   4948 	test_AUDIO_SETINFO_mode(openmode, index, setmode, expected);	\
   4949 } while (0)
   4950 DEF(AUDIO_SETINFO_mode_RDONLY_0) { f(O_RDONLY, 0); }
   4951 DEF(AUDIO_SETINFO_mode_RDONLY_1) { f(O_RDONLY, 1); }
   4952 DEF(AUDIO_SETINFO_mode_RDONLY_2) { f(O_RDONLY, 2); }
   4953 DEF(AUDIO_SETINFO_mode_RDONLY_3) { f(O_RDONLY, 3); }
   4954 DEF(AUDIO_SETINFO_mode_RDONLY_4) { f(O_RDONLY, 4); }
   4955 DEF(AUDIO_SETINFO_mode_RDONLY_5) { f(O_RDONLY, 5); }
   4956 DEF(AUDIO_SETINFO_mode_RDONLY_6) { f(O_RDONLY, 6); }
   4957 DEF(AUDIO_SETINFO_mode_RDONLY_7) { f(O_RDONLY, 7); }
   4958 DEF(AUDIO_SETINFO_mode_RDONLY_8) { f(O_RDONLY, 8); }
   4959 DEF(AUDIO_SETINFO_mode_WRONLY_0) { f(O_WRONLY, 0); }
   4960 DEF(AUDIO_SETINFO_mode_WRONLY_1) { f(O_WRONLY, 1); }
   4961 DEF(AUDIO_SETINFO_mode_WRONLY_2) { f(O_WRONLY, 2); }
   4962 DEF(AUDIO_SETINFO_mode_WRONLY_3) { f(O_WRONLY, 3); }
   4963 DEF(AUDIO_SETINFO_mode_WRONLY_4) { f(O_WRONLY, 4); }
   4964 DEF(AUDIO_SETINFO_mode_WRONLY_5) { f(O_WRONLY, 5); }
   4965 DEF(AUDIO_SETINFO_mode_WRONLY_6) { f(O_WRONLY, 6); }
   4966 DEF(AUDIO_SETINFO_mode_WRONLY_7) { f(O_WRONLY, 7); }
   4967 DEF(AUDIO_SETINFO_mode_WRONLY_8) { f(O_WRONLY, 8); }
   4968 #undef f
   4969 /*
   4970  * The following tables also show that NetBSD7's behavior is almost
   4971  * 'undefined'.
   4972  */
   4973 struct setinfo_mode_t table_SETINFO_mode_O_RDWR_full[] = {
   4974 	/* setmode	expmode7	expmode9 */
   4975 	{     0,	    0,		R|  P },
   4976 	{     P,	    P,		R|  P },
   4977 	{   A  ,	  A|P,		R|A|P },
   4978 	{   A|P,	  A|P,		R|A|P },
   4979 	{ R    ,	R    ,		R|  P },
   4980 	{ R|  P,	R|  P,		R|  P },
   4981 	{ R|A  ,	R|A|P,		R|A|P },
   4982 	{ R|A|P,	R|A|P,		R|A|P },
   4983 	{     8,	    8,		R|  P },
   4984 };
   4985 struct setinfo_mode_t table_SETINFO_mode_O_RDWR_half[] = {
   4986 	/* setmode	expmode7	expmode9 */
   4987 	{     0,	    0,		    P },
   4988 	{     P,	    P,		    P },
   4989 	{   A  ,	  A|P,		  A|P },
   4990 	{   A|P,	  A|P,		  A|P },
   4991 	{ R    ,	R    ,		    P },
   4992 	{ R|  P,	    P,		    P },
   4993 	{ R|A  ,	  A|P,		  A|P },
   4994 	{ R|A|P,	  A|P,		  A|P },
   4995 	{     8,	    8,		    P },
   4996 };
   4997 #define f(index)	do {						\
   4998 	struct setinfo_mode_t *table = (hw_fulldup())			\
   4999 	    ? table_SETINFO_mode_O_RDWR_full				\
   5000 	    : table_SETINFO_mode_O_RDWR_half;				\
   5001 	int setmode = table[index].setmode;				\
   5002 	int expected = (netbsd < 9)					\
   5003 	    ? table[index].expmode7					\
   5004 	    : table[index].expmode9;					\
   5005 	test_AUDIO_SETINFO_mode(O_RDWR, index, setmode, expected);	\
   5006 } while (0)
   5007 DEF(AUDIO_SETINFO_mode_RDWR_0) { f(0); }
   5008 DEF(AUDIO_SETINFO_mode_RDWR_1) { f(1); }
   5009 DEF(AUDIO_SETINFO_mode_RDWR_2) { f(2); }
   5010 DEF(AUDIO_SETINFO_mode_RDWR_3) { f(3); }
   5011 DEF(AUDIO_SETINFO_mode_RDWR_4) { f(4); }
   5012 DEF(AUDIO_SETINFO_mode_RDWR_5) { f(5); }
   5013 DEF(AUDIO_SETINFO_mode_RDWR_6) { f(6); }
   5014 DEF(AUDIO_SETINFO_mode_RDWR_7) { f(7); }
   5015 DEF(AUDIO_SETINFO_mode_RDWR_8) { f(8); }
   5016 #undef f
   5017 #undef P
   5018 #undef A
   5019 #undef R
   5020 
   5021 /*
   5022  * Check whether encoding params can be set.
   5023  */
   5024 void
   5025 test_AUDIO_SETINFO_params_set(int openmode, int aimode, int pause)
   5026 {
   5027 	struct audio_info ai;
   5028 	int r;
   5029 	int fd;
   5030 
   5031 	/*
   5032 	 * aimode is bool value that indicates whether to change ai.mode.
   5033 	 * pause is bool value that indicates whether to change ai.*.pause.
   5034 	 */
   5035 
   5036 	TEST("AUDIO_SETINFO_params_%s_%d_%d",
   5037 	    openmode_str[openmode] + 2, aimode, pause);
   5038 	if (mode2aumode(openmode) == 0) {
   5039 		XP_SKIP("Operation not allowed on this hardware property");
   5040 		return;
   5041 	}
   5042 
   5043 	/* On half-duplex, O_RDWR is the same as O_WRONLY, so skip it */
   5044 	if (!hw_fulldup() && openmode == O_RDWR) {
   5045 		XP_SKIP("This is the same with O_WRONLY on half-duplex");
   5046 		return;
   5047 	}
   5048 
   5049 	fd = OPEN(devaudio, openmode);
   5050 	REQUIRED_SYS_OK(fd);
   5051 
   5052 	AUDIO_INITINFO(&ai);
   5053 	/*
   5054 	 * It takes time and effort to check all parameters independently,
   5055 	 * so that use sample_rate as a representative.
   5056 	 */
   5057 	ai.play.sample_rate = 11025;
   5058 	ai.record.sample_rate = 11025;
   5059 	if (aimode)
   5060 		ai.mode = mode2aumode(openmode) & ~AUMODE_PLAY_ALL;
   5061 	if (pause) {
   5062 		ai.play.pause = 1;
   5063 		ai.record.pause = 1;
   5064 	}
   5065 
   5066 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5067 	XP_SYS_EQ(0, r);
   5068 
   5069 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5070 	XP_SYS_EQ(0, r);
   5071 	int expmode = (aimode)
   5072 	    ? (mode2aumode(openmode) & ~AUMODE_PLAY_ALL)
   5073 	    : mode2aumode(openmode);
   5074 	XP_EQ(expmode, ai.mode);
   5075 	XP_EQ(11025, ai.play.sample_rate);
   5076 	XP_EQ(pause, ai.play.pause);
   5077 	XP_EQ(11025, ai.record.sample_rate);
   5078 	XP_EQ(pause, ai.record.pause);
   5079 
   5080 	r = CLOSE(fd);
   5081 	XP_SYS_EQ(0, r);
   5082 }
   5083 #define f(a,b,c) test_AUDIO_SETINFO_params_set(a, b, c)
   5084 DEF(AUDIO_SETINFO_params_set_RDONLY_0)	{ f(O_RDONLY, 0, 0); }
   5085 DEF(AUDIO_SETINFO_params_set_RDONLY_1)	{ f(O_RDONLY, 0, 1); }
   5086 /* On RDONLY, ai.mode is not changable
   5087  *  AUDIO_SETINFO_params_set_RDONLY_2)	{ f(O_RDONLY, 1, 0); }
   5088  *  AUDIO_SETINFO_params_set_RDONLY_3)	{ f(O_RDONLY, 1, 1); }
   5089  */
   5090 DEF(AUDIO_SETINFO_params_set_WRONLY_0)	{ f(O_WRONLY, 0, 0); }
   5091 DEF(AUDIO_SETINFO_params_set_WRONLY_1)	{ f(O_WRONLY, 0, 1); }
   5092 DEF(AUDIO_SETINFO_params_set_WRONLY_2)	{ f(O_WRONLY, 1, 0); }
   5093 DEF(AUDIO_SETINFO_params_set_WRONLY_3)	{ f(O_WRONLY, 1, 1); }
   5094 DEF(AUDIO_SETINFO_params_set_RDWR_0)	{ f(O_RDWR, 0, 0); }
   5095 DEF(AUDIO_SETINFO_params_set_RDWR_1)	{ f(O_RDWR, 0, 1); }
   5096 DEF(AUDIO_SETINFO_params_set_RDWR_2)	{ f(O_RDWR, 1, 0); }
   5097 DEF(AUDIO_SETINFO_params_set_RDWR_3)	{ f(O_RDWR, 1, 1); }
   5098 #undef f
   5099 
   5100 /*
   5101  * AUDIO_SETINFO for existing track should not be interfered by other
   5102  * descriptor.
   5103  * AUDIO_SETINFO for non-existing track affects/is affected sticky parameters
   5104  * for backward compatibility.
   5105  */
   5106 DEF(AUDIO_SETINFO_params_simul)
   5107 {
   5108 	struct audio_info ai;
   5109 	int fd0;
   5110 	int fd1;
   5111 	int r;
   5112 
   5113 	TEST("AUDIO_SETINFO_params_simul");
   5114 	if (netbsd < 8) {
   5115 		XP_SKIP("Multiple open is not supported");
   5116 		return;
   5117 	}
   5118 	if (hw_canplay() == 0) {
   5119 		XP_SKIP("This test is for playable device");
   5120 		return;
   5121 	}
   5122 
   5123 	/* Open the 1st one as playback only */
   5124 	fd0 = OPEN(devaudio, O_WRONLY);
   5125 	REQUIRED_SYS_OK(fd0);
   5126 
   5127 	/* Open the 2nd one as both of playback and recording */
   5128 	fd1 = OPEN(devaudio, O_RDWR);
   5129 	REQUIRED_SYS_OK(fd1);
   5130 
   5131 	/* Change some parameters of both track on the 2nd one */
   5132 	AUDIO_INITINFO(&ai);
   5133 	ai.play.sample_rate = 11025;
   5134 	ai.record.sample_rate = 11025;
   5135 	r = IOCTL(fd1, AUDIO_SETINFO, &ai, "");
   5136 	XP_SYS_EQ(0, r);
   5137 
   5138 	/*
   5139 	 * The 1st one doesn't have recording track so that only recording
   5140 	 * parameter is affected by sticky parameter.
   5141 	 */
   5142 	memset(&ai, 0, sizeof(ai));
   5143 	r = IOCTL(fd0, AUDIO_GETBUFINFO, &ai, "");
   5144 	XP_SYS_EQ(0, r);
   5145 	XP_EQ(8000, ai.play.sample_rate);
   5146 	XP_EQ(11025, ai.record.sample_rate);
   5147 
   5148 	/* Next, change some parameters of both track on the 1st one */
   5149 	AUDIO_INITINFO(&ai);
   5150 	ai.play.sample_rate = 16000;
   5151 	ai.record.sample_rate = 16000;
   5152 	r = IOCTL(fd0, AUDIO_SETINFO, &ai, "");
   5153 	XP_SYS_EQ(0, r);
   5154 
   5155 	/*
   5156 	 * On full-duplex device, the 2nd one has both track so that
   5157 	 * both track are not affected by sticky parameter.
   5158 	 * Otherwise, the 2nd one has only playback track so that
   5159 	 * playback track is not affected by sticky parameter.
   5160 	 */
   5161 	memset(&ai, 0, sizeof(ai));
   5162 	r = IOCTL(fd1, AUDIO_GETBUFINFO, &ai, "");
   5163 	XP_SYS_EQ(0, r);
   5164 	XP_EQ(11025, ai.play.sample_rate);
   5165 	if (hw_fulldup()) {
   5166 		XP_EQ(11025, ai.record.sample_rate);
   5167 	} else {
   5168 		XP_EQ(16000, ai.record.sample_rate);
   5169 	}
   5170 
   5171 	r = CLOSE(fd0);
   5172 	XP_SYS_EQ(0, r);
   5173 	r = CLOSE(fd1);
   5174 	XP_SYS_EQ(0, r);
   5175 }
   5176 
   5177 /*
   5178  * AUDIO_SETINFO(encoding/precision) is tested in AUDIO_GETENC_range below.
   5179  */
   5180 
   5181 /*
   5182  * Check whether the number of channels can be set.
   5183  */
   5184 DEF(AUDIO_SETINFO_channels)
   5185 {
   5186 	struct audio_info hwinfo;
   5187 	struct audio_info ai;
   5188 	int mode;
   5189 	int r;
   5190 	int fd;
   5191 	int i;
   5192 	unsigned int ch;
   5193 	struct {
   5194 		int ch;
   5195 		bool expected;
   5196 	} table[] = {
   5197 		{  0,	false },
   5198 		{  1,	true },	/* monaural */
   5199 		{  2,	true },	/* stereo */
   5200 	};
   5201 
   5202 	TEST("AUDIO_SETINFO_channels");
   5203 	if (netbsd < 8) {
   5204 		/*
   5205 		 * On NetBSD7, the result depends the hardware and there is
   5206 		 * no way to know it.
   5207 		 */
   5208 		XP_SKIP("The test doesn't make sense on NetBSD7");
   5209 		return;
   5210 	}
   5211 
   5212 	mode = openable_mode();
   5213 	fd = OPEN(devaudio, mode);
   5214 	REQUIRED_SYS_OK(fd);
   5215 
   5216 	/*
   5217 	 * The audio layer always supports monaural and stereo regardless of
   5218 	 * the hardware capability.
   5219 	 */
   5220 	for (i = 0; i < (int)__arraycount(table); i++) {
   5221 		ch = table[i].ch;
   5222 		bool expected = table[i].expected;
   5223 
   5224 		AUDIO_INITINFO(&ai);
   5225 		if (mode != O_RDONLY)
   5226 			ai.play.channels = ch;
   5227 		if (mode != O_WRONLY)
   5228 			ai.record.channels = ch;
   5229 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5230 		if (expected) {
   5231 			/* Expects to succeed */
   5232 			XP_SYS_EQ(0, r);
   5233 
   5234 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5235 			XP_SYS_EQ(0, r);
   5236 			if (mode != O_RDONLY)
   5237 				XP_EQ(ch, ai.play.channels);
   5238 			if (mode != O_WRONLY)
   5239 				XP_EQ(ch, ai.record.channels);
   5240 		} else {
   5241 			/* Expects to fail */
   5242 			XP_SYS_NG(EINVAL, r);
   5243 		}
   5244 	}
   5245 
   5246 	/*
   5247 	 * The maximum number of supported channels depends the hardware.
   5248 	 */
   5249 	/* Get the number of channels that the hardware supports */
   5250 	r = IOCTL(fd, AUDIO_GETFORMAT, &hwinfo, "");
   5251 	REQUIRED_SYS_EQ(0, r);
   5252 
   5253 	if ((hwinfo.mode & AUMODE_PLAY)) {
   5254 		DPRINTF("  > hwinfo.play.channels = %d\n",
   5255 		    hwinfo.play.channels);
   5256 		for (ch = 3; ch <= hwinfo.play.channels; ch++) {
   5257 			AUDIO_INITINFO(&ai);
   5258 			ai.play.channels = ch;
   5259 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5260 			XP_SYS_EQ(0, r);
   5261 
   5262 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5263 			XP_SYS_EQ(0, r);
   5264 			XP_EQ(ch, ai.play.channels);
   5265 		}
   5266 
   5267 		AUDIO_INITINFO(&ai);
   5268 		ai.play.channels = ch;
   5269 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5270 		XP_SYS_NG(EINVAL, r);
   5271 	}
   5272 	if ((hwinfo.mode & AUMODE_RECORD)) {
   5273 		DPRINTF("  > hwinfo.record.channels = %d\n",
   5274 		    hwinfo.record.channels);
   5275 		for (ch = 3; ch <= hwinfo.record.channels; ch++) {
   5276 			AUDIO_INITINFO(&ai);
   5277 			ai.record.channels = ch;
   5278 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5279 			XP_SYS_EQ(0, r);
   5280 
   5281 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5282 			XP_SYS_EQ(0, r);
   5283 			XP_EQ(ch, ai.record.channels);
   5284 		}
   5285 
   5286 		AUDIO_INITINFO(&ai);
   5287 		ai.record.channels = ch;
   5288 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "channels=%d", ch);
   5289 		XP_SYS_NG(EINVAL, r);
   5290 	}
   5291 
   5292 	r = CLOSE(fd);
   5293 	XP_SYS_EQ(0, r);
   5294 }
   5295 
   5296 /*
   5297  * Check whether the sample rate can be set.
   5298  */
   5299 DEF(AUDIO_SETINFO_sample_rate)
   5300 {
   5301 	struct audio_info ai;
   5302 	int mode;
   5303 	int r;
   5304 	int fd;
   5305 	int i;
   5306 	struct {
   5307 		int freq;
   5308 		bool expected;
   5309 	} table[] = {
   5310 		{    999,	false },
   5311 		{   1000,	true },	/* lower limit */
   5312 		{  48000,	true },
   5313 		{ 192000,	true },	/* upper limit */
   5314 		{ 192001,	false },
   5315 	};
   5316 
   5317 	TEST("AUDIO_SETINFO_sample_rate");
   5318 	if (netbsd < 8) {
   5319 		/*
   5320 		 * On NetBSD7, the result depends the hardware and there is
   5321 		 * no way to know it.
   5322 		 */
   5323 		XP_SKIP("The test doesn't make sense on NetBSD7");
   5324 		return;
   5325 	}
   5326 
   5327 	mode = openable_mode();
   5328 	fd = OPEN(devaudio, mode);
   5329 	REQUIRED_SYS_OK(fd);
   5330 
   5331 	for (i = 0; i < (int)__arraycount(table); i++) {
   5332 		int freq = table[i].freq;
   5333 		bool expected = table[i].expected;
   5334 
   5335 		AUDIO_INITINFO(&ai);
   5336 		if (mode != O_RDONLY)
   5337 			ai.play.sample_rate = freq;
   5338 		if (mode != O_WRONLY)
   5339 			ai.record.sample_rate = freq;
   5340 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "sample_rate=%d", freq);
   5341 		if (expected) {
   5342 			/* Expects to succeed */
   5343 			XP_SYS_EQ(0, r);
   5344 
   5345 			r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5346 			XP_SYS_EQ(0, r);
   5347 			if (mode != O_RDONLY)
   5348 				XP_EQ(freq, ai.play.sample_rate);
   5349 			if (mode != O_WRONLY)
   5350 				XP_EQ(freq, ai.record.sample_rate);
   5351 		} else {
   5352 			/* Expects to fail */
   5353 			XP_SYS_NG(EINVAL, r);
   5354 		}
   5355 	}
   5356 
   5357 	r = CLOSE(fd);
   5358 	XP_SYS_EQ(0, r);
   5359 }
   5360 
   5361 /*
   5362  * SETINFO(sample_rate = 0) should fail correctly.
   5363  */
   5364 DEF(AUDIO_SETINFO_sample_rate_0)
   5365 {
   5366 	struct audio_info ai;
   5367 	int mode;
   5368 	int r;
   5369 	int fd;
   5370 
   5371 	TEST("AUDIO_SETINFO_sample_rate_0");
   5372 	if (netbsd < 9) {
   5373 		/*
   5374 		 * On NetBSD7,8 this will block system call and you will not
   5375 		 * even be able to shutdown...
   5376 		 */
   5377 		XP_SKIP("This will cause an infinate loop in the kernel");
   5378 		return;
   5379 	}
   5380 
   5381 	mode = openable_mode();
   5382 	fd = OPEN(devaudio, mode);
   5383 	REQUIRED_SYS_OK(fd);
   5384 
   5385 	AUDIO_INITINFO(&ai);
   5386 	ai.play.sample_rate = 0;
   5387 	ai.record.sample_rate = 0;
   5388 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "sample_rate=0");
   5389 	/* Expects to fail */
   5390 	XP_SYS_NG(EINVAL, r);
   5391 
   5392 	r = CLOSE(fd);
   5393 	XP_SYS_EQ(0, r);
   5394 }
   5395 
   5396 /*
   5397  * Check whether the pause/unpause works.
   5398  */
   5399 void
   5400 test_AUDIO_SETINFO_pause(int openmode, int aimode, int param)
   5401 {
   5402 	struct audio_info ai;
   5403 	int r;
   5404 	int fd;
   5405 
   5406 	/*
   5407 	 * aimode is bool value that indicates whether to change ai.mode.
   5408 	 * param is bool value that indicates whether to change encoding
   5409 	 * parameters of ai.{play,record}.*.
   5410 	 */
   5411 
   5412 	TEST("AUDIO_SETINFO_pause_%s_%d_%d",
   5413 	    openmode_str[openmode] + 2, aimode, param);
   5414 	if (mode2aumode(openmode) == 0) {
   5415 		XP_SKIP("Operation not allowed on this hardware property");
   5416 		return;
   5417 	}
   5418 
   5419 	/* On half-duplex, O_RDWR is the same as O_WRONLY, so skip it */
   5420 	if (!hw_fulldup() && openmode == O_RDWR) {
   5421 		XP_SKIP("This is the same with O_WRONLY on half-duplex");
   5422 		return;
   5423 	}
   5424 
   5425 	fd = OPEN(devaudio, openmode);
   5426 	REQUIRED_SYS_OK(fd);
   5427 
   5428 	/* Set pause */
   5429 	AUDIO_INITINFO(&ai);
   5430 	ai.play.pause = 1;
   5431 	ai.record.pause = 1;
   5432 	if (aimode)
   5433 		ai.mode = mode2aumode(openmode) & ~AUMODE_PLAY_ALL;
   5434 	if (param) {
   5435 		ai.play.sample_rate = 11025;
   5436 		ai.record.sample_rate = 11025;
   5437 	}
   5438 
   5439 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5440 	XP_SYS_EQ(0, r);
   5441 
   5442 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5443 	XP_SYS_EQ(0, r);
   5444 	int expmode = (aimode)
   5445 	    ? (mode2aumode(openmode) & ~AUMODE_PLAY_ALL)
   5446 	    : mode2aumode(openmode);
   5447 	XP_EQ(expmode, ai.mode);
   5448 	XP_EQ(1, ai.play.pause);
   5449 	XP_EQ(param ? 11025 : 8000, ai.play.sample_rate);
   5450 	XP_EQ(1, ai.record.pause);
   5451 	XP_EQ(param ? 11025 : 8000, ai.record.sample_rate);
   5452 
   5453 	/* Set unpause (?) */
   5454 	AUDIO_INITINFO(&ai);
   5455 	ai.play.pause = 0;
   5456 	ai.record.pause = 0;
   5457 	if (aimode)
   5458 		ai.mode = mode2aumode(openmode);
   5459 	if (param) {
   5460 		ai.play.sample_rate = 16000;
   5461 		ai.record.sample_rate = 16000;
   5462 	}
   5463 
   5464 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   5465 	XP_SYS_EQ(0, r);
   5466 
   5467 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   5468 	XP_SYS_EQ(0, r);
   5469 	XP_EQ(mode2aumode(openmode), ai.mode);
   5470 	XP_EQ(0, ai.play.pause);
   5471 	XP_EQ(0, ai.record.pause);
   5472 	if (openmode != O_RDONLY)
   5473 		XP_EQ(param ? 16000 : 8000, ai.play.sample_rate);
   5474 	if (openmode != O_WRONLY)
   5475 		XP_EQ(param ? 16000 : 8000, ai.record.sample_rate);
   5476 
   5477 	r = CLOSE(fd);
   5478 	XP_SYS_EQ(0, r);
   5479 }
   5480 DEF(AUDIO_SETINFO_pause_RDONLY_0) { test_AUDIO_SETINFO_pause(O_RDONLY, 0, 0); }
   5481 DEF(AUDIO_SETINFO_pause_RDONLY_1) { test_AUDIO_SETINFO_pause(O_RDONLY, 0, 1); }
   5482 /* On RDONLY, ai.mode is not changable
   5483  *  AUDIO_SETINFO_pause_RDONLY_2) { test_AUDIO_SETINFO_pause(O_RDONLY, 1, 0); }
   5484  *  AUDIO_SETINFO_pause_RDONLY_3) { test_AUDIO_SETINFO_pause(O_RDONLY, 1, 1); }
   5485  */
   5486 DEF(AUDIO_SETINFO_pause_WRONLY_0) { test_AUDIO_SETINFO_pause(O_WRONLY, 0, 0); }
   5487 DEF(AUDIO_SETINFO_pause_WRONLY_1) { test_AUDIO_SETINFO_pause(O_WRONLY, 0, 1); }
   5488 DEF(AUDIO_SETINFO_pause_WRONLY_2) { test_AUDIO_SETINFO_pause(O_WRONLY, 1, 0); }
   5489 DEF(AUDIO_SETINFO_pause_WRONLY_3) { test_AUDIO_SETINFO_pause(O_WRONLY, 1, 1); }
   5490 DEF(AUDIO_SETINFO_pause_RDWR_0)   { test_AUDIO_SETINFO_pause(O_RDWR, 0, 0); }
   5491 DEF(AUDIO_SETINFO_pause_RDWR_1)   { test_AUDIO_SETINFO_pause(O_RDWR, 0, 1); }
   5492 DEF(AUDIO_SETINFO_pause_RDWR_2)   { test_AUDIO_SETINFO_pause(O_RDWR, 1, 0); }
   5493 DEF(AUDIO_SETINFO_pause_RDWR_3)   { test_AUDIO_SETINFO_pause(O_RDWR, 1, 1); }
   5494 
   5495 /*
   5496  * Check whether gain can be obtained/set.
   5497  * And the gain should work with rich mixer.
   5498  * PR kern/52781
   5499  */
   5500 DEF(AUDIO_SETINFO_gain)
   5501 {
   5502 	struct audio_info ai;
   5503 	mixer_ctrl_t m;
   5504 	int index;
   5505 	int master;
   5506 	int master_backup;
   5507 	int gain;
   5508 	int fd;
   5509 	int mixerfd;
   5510 	int r;
   5511 
   5512 	TEST("AUDIO_SETINFO_gain");
   5513 
   5514 	/* Open /dev/mixer */
   5515 	mixerfd = OPEN(devmixer, O_RDWR);
   5516 	REQUIRED_SYS_OK(mixerfd);
   5517 	index = mixer_get_outputs_master(mixerfd);
   5518 	if (index == -1) {
   5519 		XP_SKIP("Hardware has no outputs.master");
   5520 		CLOSE(mixerfd);
   5521 		return;
   5522 	}
   5523 
   5524 	/*
   5525 	 * Get current outputs.master.
   5526 	 * auich(4) requires class type (m.type) and number of channels
   5527 	 * (un.value.num_channels) in addition to the index (m.dev)...
   5528 	 * What is the index...?
   5529 	 */
   5530 	memset(&m, 0, sizeof(m));
   5531 	m.dev = index;
   5532 	m.type = AUDIO_MIXER_VALUE;
   5533 	m.un.value.num_channels = 1; /* dummy */
   5534 	r = IOCTL(mixerfd, AUDIO_MIXER_READ, &m, "m.dev=%d", m.dev);
   5535 	REQUIRED_SYS_EQ(0, r);
   5536 	master = m.un.value.level[0];
   5537 	DPRINTF("  > outputs.master = %d\n", master);
   5538 	master_backup = master;
   5539 
   5540 	/* Open /dev/audio */
   5541 	fd = OPEN(devaudio, O_WRONLY);
   5542 	REQUIRED_SYS_OK(fd);
   5543 
   5544 	/* Check ai.play.gain */
   5545 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "");
   5546 	XP_SYS_EQ(0, r);
   5547 	XP_EQ(master, ai.play.gain);
   5548 
   5549 	/* Change it some different value */
   5550 	AUDIO_INITINFO(&ai);
   5551 	if (master == 0)
   5552 		gain = 255;
   5553 	else
   5554 		gain = 0;
   5555 	ai.play.gain = gain;
   5556 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "play.gain=%d", ai.play.gain);
   5557 	XP_SYS_EQ(0, r);
   5558 
   5559 	/* Check gain has changed */
   5560 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "play.gain");
   5561 	XP_SYS_EQ(0, r);
   5562 	XP_NE(master, ai.play.gain);
   5563 
   5564 	/* Check whether outputs.master work with gain */
   5565 	r = IOCTL(mixerfd, AUDIO_MIXER_READ, &m, "");
   5566 	XP_SYS_EQ(0, r);
   5567 	XP_EQ(ai.play.gain, m.un.value.level[0]);
   5568 
   5569 	/* Restore outputs.master */
   5570 	AUDIO_INITINFO(&ai);
   5571 	ai.play.gain = master_backup;
   5572 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "play.gain=%d", ai.play.gain);
   5573 	XP_SYS_EQ(0, r);
   5574 
   5575 	r = CLOSE(fd);
   5576 	XP_SYS_EQ(0, r);
   5577 	r = CLOSE(mixerfd);
   5578 	XP_SYS_EQ(0, r);
   5579 }
   5580 
   5581 /*
   5582  * Look if there are any (non-zero) gain values that can be changed.
   5583  * If any gain can be set, it is set to gain[0].
   5584  * If another gain can be set, it is set to gain[1], otherwise gain[1] = -1.
   5585  * This is for AUDIO_SETINFO_gain_balance.
   5586  */
   5587 static void
   5588 get_changeable_gain(int fd, int *gain, const char *dir, int offset)
   5589 {
   5590 	struct audio_info ai;
   5591 	int *ai_gain;
   5592 	int hi;
   5593 	int lo;
   5594 	int r;
   5595 
   5596 	/* A hack to handle ai.{play,record}.gain in the same code.. */
   5597 	ai_gain = (int *)(((char *)&ai) + offset);
   5598 
   5599 	/* Try to set the maximum gain */
   5600 	AUDIO_INITINFO(&ai);
   5601 	*ai_gain = AUDIO_MAX_GAIN;
   5602 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s.gain=%d", dir, *ai_gain);
   5603 	XP_SYS_EQ(0, r);
   5604 	/* Get again.  The value you set is not always used as is. */
   5605 	AUDIO_INITINFO(&ai);
   5606 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5607 	XP_SYS_EQ(0, r);
   5608 	hi = *ai_gain;
   5609 
   5610 	/* Look for next configurable value. */
   5611 	for (lo = hi - 1; lo >= 0; lo--) {
   5612 		AUDIO_INITINFO(&ai);
   5613 		*ai_gain = lo;
   5614 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s.gain=%d", dir, *ai_gain);
   5615 		XP_SYS_EQ(0, r);
   5616 		/* Get again */
   5617 		r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5618 		XP_SYS_EQ(0, r);
   5619 		if (*ai_gain != hi) {
   5620 			lo = *ai_gain;
   5621 			break;
   5622 		}
   5623 	}
   5624 
   5625 	/* Now gain is lo(=gain[0]). */
   5626 
   5627 	/*
   5628 	 * hi  lo
   5629 	 * --- ---
   5630 	 *  <0  <0          : not available.
   5631 	 * >=0  <0          : available but not changeable.
   5632 	 * >=0 >=0 (hi!=lo) : available and changeable.
   5633 	 */
   5634 	if (hi < 0) {
   5635 		gain[0] = -1;
   5636 		gain[1] = -1;
   5637 		DPRINTF("  > %s.gain cannot be set\n", dir);
   5638 	} else if (lo < 0) {
   5639 		gain[0] = hi;
   5640 		gain[1] = -1;
   5641 		DPRINTF("  > %s.gain can only be set %d\n", dir, gain[0]);
   5642 	} else {
   5643 		gain[0] = lo;
   5644 		gain[1] = hi;
   5645 		DPRINTF("  > %s.gain can be set %d, %d\n",
   5646 		    dir, gain[0], gain[1]);
   5647 	}
   5648 }
   5649 
   5650 /*
   5651  * Look if there are any balance values that can be changed.
   5652  * If any balance value can be set, it is set to balance[0].
   5653  * If another balance value can be set, it is set to balance[1],
   5654  * otherwise balance[1] = -1.
   5655  * This is for AUDIO_SETINFO_gain_balance.
   5656  */
   5657 static void
   5658 get_changeable_balance(int fd, int *balance, const char *dir, int offset)
   5659 {
   5660 	struct audio_info ai;
   5661 	u_char *ai_balance;
   5662 	u_char left;
   5663 	u_char right;
   5664 	int r;
   5665 
   5666 	/* A hack to handle ai.{play,record}.balance in the same code.. */
   5667 	ai_balance = ((u_char *)&ai) + offset;
   5668 
   5669 	/* Look for the right side configurable value. */
   5670 	AUDIO_INITINFO(&ai);
   5671 	*ai_balance = AUDIO_RIGHT_BALANCE;
   5672 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s.balance=%d", dir, *ai_balance);
   5673 	XP_SYS_EQ(0, r);
   5674 	/* Get again.  The value you set is not always used as is. */
   5675 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5676 	XP_SYS_EQ(0, r);
   5677 	right = *ai_balance;
   5678 
   5679 	/* Look for the left side configurable value. */
   5680 	AUDIO_INITINFO(&ai);
   5681 	*ai_balance = AUDIO_LEFT_BALANCE;
   5682 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s.balance=%d", dir, *ai_balance);
   5683 	XP_SYS_EQ(0, r);
   5684 	/* Get again */
   5685 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5686 	XP_SYS_EQ(0, r);
   5687 	left = *ai_balance;
   5688 
   5689 	/* Now balance is the left(=balance[0]). */
   5690 
   5691 	if (left == right) {
   5692 		/* The driver has no balance feature. */
   5693 		balance[0] = left;
   5694 		balance[1] = -1;
   5695 		DPRINTF("  > %s.balance can only be set %d\n",
   5696 		    dir, balance[0]);
   5697 	} else {
   5698 		balance[0] = left;
   5699 		balance[1] = right;
   5700 		DPRINTF("  > %s.balance can be set %d, %d\n",
   5701 		    dir, balance[0], balance[1]);
   5702 	}
   5703 }
   5704 
   5705 /*
   5706  * Check whether gain and balance can be set at the same time.
   5707  * PR kern/56308
   5708  */
   5709 DEF(AUDIO_SETINFO_gain_balance)
   5710 {
   5711 	struct audio_info oai;
   5712 	struct audio_info ai;
   5713 	int i;
   5714 	int mode;
   5715 	int fd;
   5716 	int r;
   5717 	int pgain[2];
   5718 	int pbalance[2];
   5719 	int rgain[2];
   5720 	int rbalance[2];
   5721 	bool ptest;
   5722 	bool rtest;
   5723 
   5724 	TEST("AUDIO_SETINFO_gain_balance");
   5725 
   5726 	mode = openable_mode();
   5727 	fd = OPEN(devaudio, mode);
   5728 	REQUIRED_SYS_OK(fd);
   5729 
   5730 	/* Backup current gain and balance */
   5731 	r = IOCTL(fd, AUDIO_GETINFO, &oai, "&oai");
   5732 	XP_SYS_EQ(0, r);
   5733 
   5734 	if (debug) {
   5735 		printf("  > old play.gain      = %d\n", oai.play.gain);
   5736 		printf("  > old play.balance   = %d\n", oai.play.balance);
   5737 		printf("  > old record.gain    = %d\n", oai.record.gain);
   5738 		printf("  > old record.balance = %d\n", oai.record.balance);
   5739 	}
   5740 
   5741 	for (i = 0; i < 2; i++) {
   5742 		pgain[i]    = -1;
   5743 		pbalance[i] = -1;
   5744 		rgain[i]    = -1;
   5745 		rbalance[i] = -1;
   5746 	}
   5747 
   5748 	/*
   5749 	 * First, check each one separately can be changed.
   5750 	 *
   5751 	 * The simplest two different gain values are zero and non-zero.
   5752 	 * But some device drivers seem to process balance differently
   5753 	 * when the gain is high enough and when the gain is zero or near.
   5754 	 * So I needed to select two different "non-zero (and high if
   5755 	 * possible)" gains.
   5756 	 */
   5757 	if (hw_canplay()) {
   5758 		get_changeable_gain(fd, pgain, "play",
   5759 		    offsetof(struct audio_info, play.gain));
   5760 		get_changeable_balance(fd, pbalance, "play",
   5761 		    offsetof(struct audio_info, play.balance));
   5762 	}
   5763 	if (hw_canrec()) {
   5764 		get_changeable_gain(fd, rgain, "record",
   5765 		    offsetof(struct audio_info, record.gain));
   5766 		get_changeable_balance(fd, rbalance, "record",
   5767 		    offsetof(struct audio_info, record.balance));
   5768 	}
   5769 
   5770 	/*
   5771 	 * [0] [1]
   5772 	 * --- ---
   5773 	 *  -1  *  : not available.
   5774 	 * >=0  -1 : available but not changeable.
   5775 	 * >=0 >=0 : available and changeable.  It can be tested.
   5776 	 */
   5777 	ptest = (pgain[0]    >= 0 && pgain[1]    >= 0 &&
   5778 	         pbalance[0] >= 0 && pbalance[1] >= 0);
   5779 	rtest = (rgain[0]    >= 0 && rgain[1]    >= 0 &&
   5780 	         rbalance[0] >= 0 && rbalance[1] >= 0);
   5781 
   5782 	if (ptest == false && rtest == false) {
   5783 		XP_SKIP(
   5784 		    "The test requires changeable gain and changeable balance");
   5785 
   5786 		/* Restore as possible */
   5787 		AUDIO_INITINFO(&ai);
   5788 		ai.play.gain      = oai.play.gain;
   5789 		ai.play.balance   = oai.play.balance;
   5790 		ai.record.gain    = oai.record.gain;
   5791 		ai.record.balance = oai.record.balance;
   5792 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "restore all");
   5793 		XP_SYS_EQ(0, r);
   5794 
   5795 		r = CLOSE(fd);
   5796 		XP_SYS_EQ(0, r);
   5797 		return;
   5798 	}
   5799 
   5800 	/*
   5801 	 * If both play.gain and play.balance are changeable,
   5802 	 * it should be able to set both at the same time.
   5803 	 */
   5804 	if (ptest) {
   5805 		AUDIO_INITINFO(&ai);
   5806 		ai.play.gain    = pgain[1];
   5807 		ai.play.balance = pbalance[1];
   5808 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "play.gain=%d/balance=%d",
   5809 		    ai.play.gain, ai.play.balance);
   5810 		XP_SYS_EQ(0, r);
   5811 
   5812 		AUDIO_INITINFO(&ai);
   5813 		r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5814 		XP_SYS_EQ(0, r);
   5815 
   5816 		DPRINTF("  > setting play.gain=%d/balance=%d: "
   5817 		    "result gain=%d/balance=%d\n",
   5818 		    pgain[1], pbalance[1], ai.play.gain, ai.play.balance);
   5819 		XP_EQ(ai.play.gain,    pgain[1]);
   5820 		XP_EQ(ai.play.balance, pbalance[1]);
   5821 	}
   5822 	/*
   5823 	 * If both record.gain and record.balance are changeable,
   5824 	 * it should be able to set both at the same time.
   5825 	 */
   5826 	if (rtest) {
   5827 		AUDIO_INITINFO(&ai);
   5828 		ai.record.gain    = rgain[1];
   5829 		ai.record.balance = rbalance[1];
   5830 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "record.gain=%d/balance=%d",
   5831 		    ai.record.gain, ai.record.balance);
   5832 		XP_SYS_EQ(0, r);
   5833 
   5834 		AUDIO_INITINFO(&ai);
   5835 		r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5836 		XP_SYS_EQ(0, r);
   5837 
   5838 		DPRINTF("  > setting record.gain=%d/balance=%d: "
   5839 		    "result gain=%d/balance=%d\n",
   5840 		    rgain[1], rbalance[1], ai.record.gain, ai.record.balance);
   5841 		XP_EQ(ai.record.gain,    rgain[1]);
   5842 		XP_EQ(ai.record.balance, rbalance[1]);
   5843 	}
   5844 
   5845 	/*
   5846 	 * Restore all values as possible at the same time.
   5847 	 * This restore is also a test.
   5848 	 */
   5849 	AUDIO_INITINFO(&ai);
   5850 	ai.play.gain      = oai.play.gain;
   5851 	ai.play.balance   = oai.play.balance;
   5852 	ai.record.gain    = oai.record.gain;
   5853 	ai.record.balance = oai.record.balance;
   5854 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "restore all");
   5855 	XP_SYS_EQ(0, r);
   5856 
   5857 	AUDIO_INITINFO(&ai);
   5858 	r = IOCTL(fd, AUDIO_GETINFO, &ai, "&ai");
   5859 	XP_SYS_EQ(0, r);
   5860 	XP_EQ(oai.play.gain,      ai.play.gain);
   5861 	XP_EQ(oai.play.balance,   ai.play.balance);
   5862 	XP_EQ(oai.record.gain,    ai.record.gain);
   5863 	XP_EQ(oai.record.balance, ai.record.balance);
   5864 
   5865 	r = CLOSE(fd);
   5866 	XP_SYS_EQ(0, r);
   5867 }
   5868 
   5869 #define NENC	(AUDIO_ENCODING_AC3 + 1)
   5870 #define NPREC	(5)
   5871 /*
   5872  * Make table of encoding+precision supported by this device.
   5873  * Return last used index .
   5874  * This function is called from test_AUDIO_GETENC_*()
   5875  */
   5876 int
   5877 getenc_make_table(int fd, int expected[][5])
   5878 {
   5879 	audio_encoding_t ae;
   5880 	int idx;
   5881 	int p;
   5882 	int r;
   5883 
   5884 	/*
   5885 	 * expected[][] is two dimensional table.
   5886 	 * encoding \ precision| 4  8  16  24  32
   5887 	 * --------------------+-----------------
   5888 	 * AUDIO_ENCODING_NONE |
   5889 	 * AUDIO_ENCODING_ULAW |
   5890 	 *  :
   5891 	 *
   5892 	 * Each cell has expected behavior.
   5893 	 *  0: the hardware doesn't support this encoding/precision.
   5894 	 *  1: the hardware supports this encoding/precision.
   5895 	 *  2: the hardware doesn't support this encoding/precision but
   5896 	 *     audio layer will respond as supported for compatibility.
   5897 	 */
   5898 	for (idx = 0; ; idx++) {
   5899 		memset(&ae, 0, sizeof(ae));
   5900 		ae.index = idx;
   5901 		r = IOCTL(fd, AUDIO_GETENC, &ae, "index=%d", idx);
   5902 		if (r != 0) {
   5903 			XP_SYS_NG(EINVAL, r);
   5904 			break;
   5905 		}
   5906 
   5907 		XP_EQ(idx, ae.index);
   5908 		if (0 <= ae.encoding && ae.encoding <= AUDIO_ENCODING_AC3) {
   5909 			XP_EQ_STR(encoding_names[ae.encoding], ae.name);
   5910 		} else {
   5911 			XP_FAIL("ae.encoding %d", ae.encoding);
   5912 		}
   5913 
   5914 		if (ae.precision != 4 &&
   5915 		    ae.precision != 8 &&
   5916 		    ae.precision != 16 &&
   5917 		    ae.precision != 24 &&
   5918 		    ae.precision != 32)
   5919 		{
   5920 			XP_FAIL("ae.precision %d", ae.precision);
   5921 		}
   5922 		/* Other bits should not be set */
   5923 		XP_EQ(0, (ae.flags & ~AUDIO_ENCODINGFLAG_EMULATED));
   5924 
   5925 		expected[ae.encoding][ae.precision / 8] = 1;
   5926 		DPRINTF("  > encoding=%s precision=%d\n",
   5927 		    encoding_names[ae.encoding], ae.precision);
   5928 	}
   5929 
   5930 	/*
   5931 	 * Backward compatibility bandaid.
   5932 	 *
   5933 	 * - Some encoding/precision pairs are obviously inconsistent
   5934 	 *   (e.g., encoding=AUDIO_ENCODING_PCM8, precision=16) but
   5935 	 *   it's due to historical reasons.
   5936 	 * - It's incomplete for NetBSD7 and NetBSD8.  I don't really
   5937 	 *   understand their rule...  This is just memo, not specification.
   5938 	 */
   5939 #define SET(x) do {	\
   5940 	if ((x) == 0)	\
   5941 		x = 2;	\
   5942  } while (0)
   5943 #define p4 (0)
   5944 #define p8 (1)
   5945 #define p16 (2)
   5946 #define p24 (3)
   5947 #define p32 (4)
   5948 
   5949 	if (expected[AUDIO_ENCODING_SLINEAR][p8]) {
   5950 		SET(expected[AUDIO_ENCODING_SLINEAR_LE][p8]);
   5951 		SET(expected[AUDIO_ENCODING_SLINEAR_BE][p8]);
   5952 	}
   5953 	if (expected[AUDIO_ENCODING_ULINEAR][p8]) {
   5954 		SET(expected[AUDIO_ENCODING_ULINEAR_LE][p8]);
   5955 		SET(expected[AUDIO_ENCODING_ULINEAR_BE][p8]);
   5956 		SET(expected[AUDIO_ENCODING_PCM8][p8]);
   5957 		SET(expected[AUDIO_ENCODING_PCM16][p8]);
   5958 	}
   5959 	for (p = p16; p <= p32; p++) {
   5960 #if !defined(AUDIO_SUPPORT_LINEAR24)
   5961 		if (p == p24)
   5962 			continue;
   5963 #endif
   5964 		if (expected[AUDIO_ENCODING_SLINEAR_NE][p]) {
   5965 			SET(expected[AUDIO_ENCODING_SLINEAR][p]);
   5966 			SET(expected[AUDIO_ENCODING_PCM16][p]);
   5967 		}
   5968 		if (expected[AUDIO_ENCODING_ULINEAR_NE][p]) {
   5969 			SET(expected[AUDIO_ENCODING_ULINEAR][p]);
   5970 		}
   5971 	}
   5972 
   5973 	if (netbsd < 9) {
   5974 		if (expected[AUDIO_ENCODING_SLINEAR_LE][p16] ||
   5975 		    expected[AUDIO_ENCODING_SLINEAR_BE][p16] ||
   5976 		    expected[AUDIO_ENCODING_ULINEAR_LE][p16] ||
   5977 		    expected[AUDIO_ENCODING_ULINEAR_BE][p16])
   5978 		{
   5979 			SET(expected[AUDIO_ENCODING_PCM8][p8]);
   5980 			SET(expected[AUDIO_ENCODING_PCM16][p8]);
   5981 			SET(expected[AUDIO_ENCODING_SLINEAR_LE][p8]);
   5982 			SET(expected[AUDIO_ENCODING_SLINEAR_BE][p8]);
   5983 			SET(expected[AUDIO_ENCODING_ULINEAR_LE][p8]);
   5984 			SET(expected[AUDIO_ENCODING_ULINEAR_BE][p8]);
   5985 			SET(expected[AUDIO_ENCODING_SLINEAR][p8]);
   5986 			SET(expected[AUDIO_ENCODING_ULINEAR][p8]);
   5987 		}
   5988 	}
   5989 
   5990 	/* Return last used index */
   5991 	return idx;
   5992 #undef SET
   5993 #undef p4
   5994 #undef p8
   5995 #undef p16
   5996 #undef p24
   5997 #undef p32
   5998 }
   5999 
   6000 /*
   6001  * This function is called from test_AUDIO_GETENC below.
   6002  */
   6003 void
   6004 xp_getenc(int expected[][5], int enc, int j, int r, struct audio_prinfo *pr)
   6005 {
   6006 	int prec = (j == 0) ? 4 : j * 8;
   6007 
   6008 	if (expected[enc][j]) {
   6009 		/* expect to succeed */
   6010 		XP_SYS_EQ(0, r);
   6011 
   6012 		XP_EQ(enc, pr->encoding);
   6013 		XP_EQ(prec, pr->precision);
   6014 	} else {
   6015 		/* expect to fail */
   6016 		XP_SYS_NG(EINVAL, r);
   6017 	}
   6018 }
   6019 
   6020 /*
   6021  * This function is called from test_AUDIO_GETENC below.
   6022  */
   6023 void
   6024 getenc_check_encodings(int openmode, int expected[][5])
   6025 {
   6026 	struct audio_info ai;
   6027 	int fd;
   6028 	int i, j;
   6029 	int r;
   6030 
   6031 	fd = OPEN(devaudio, openmode);
   6032 	REQUIRED_SYS_OK(fd);
   6033 
   6034 	for (i = 0; i < NENC; i++) {
   6035 		for (j = 0; j < NPREC; j++) {
   6036 			/* precisions are 4 and 8, 16, 24, 32 */
   6037 			int prec = (j == 0) ? 4 : j * 8;
   6038 
   6039 			/*
   6040 			 * AUDIO_GETENC has no way to know range of
   6041 			 * supported channels and sample_rate.
   6042 			 */
   6043 			AUDIO_INITINFO(&ai);
   6044 			ai.play.encoding = i;
   6045 			ai.play.precision = prec;
   6046 			ai.record.encoding = i;
   6047 			ai.record.precision = prec;
   6048 
   6049 			r = IOCTL(fd, AUDIO_SETINFO, &ai, "%s:%d",
   6050 			    encoding_names[i], prec);
   6051 			if (mode2play(openmode))
   6052 				xp_getenc(expected, i, j, r, &ai.play);
   6053 			if (mode2rec(openmode))
   6054 				xp_getenc(expected, i, j, r, &ai.record);
   6055 		}
   6056 	}
   6057 	r = CLOSE(fd);
   6058 	XP_SYS_EQ(0, r);
   6059 }
   6060 
   6061 /*
   6062  * Check whether encoding+precision obtained by AUDIO_GETENC can be set.
   6063  */
   6064 DEF(AUDIO_GETENC_range)
   6065 {
   6066 	audio_encoding_t ae;
   6067 	int fd;
   6068 	int r;
   6069 	int expected[NENC][NPREC];
   6070 	int i, j;
   6071 
   6072 	TEST("AUDIO_GETENC_range");
   6073 
   6074 	fd = OPEN(devaudio, openable_mode());
   6075 	REQUIRED_SYS_OK(fd);
   6076 
   6077 	memset(&expected, 0, sizeof(expected));
   6078 	i = getenc_make_table(fd, expected);
   6079 
   6080 	/* When error has occurred, the next index should also occur error */
   6081 	ae.index = i + 1;
   6082 	r = IOCTL(fd, AUDIO_GETENC, &ae, "index=%d", ae.index);
   6083 	XP_SYS_NG(EINVAL, r);
   6084 
   6085 	r = CLOSE(fd);
   6086 	XP_SYS_EQ(0, r);
   6087 
   6088 	/* For debug */
   6089 	if (debug) {
   6090 		for (i = 0; i < NENC; i++) {
   6091 			printf("expected[%2d] %15s", i, encoding_names[i]);
   6092 			for (j = 0; j < NPREC; j++) {
   6093 				printf(" %d", expected[i][j]);
   6094 			}
   6095 			printf("\n");
   6096 		}
   6097 	}
   6098 
   6099 	/* Whether obtained encodings can be actually set */
   6100 	if (hw_fulldup()) {
   6101 		/* Test both R/W at once using single descriptor */
   6102 		getenc_check_encodings(O_RDWR, expected);
   6103 	} else {
   6104 		/* Test playback and recording if available */
   6105 		if (hw_canplay()) {
   6106 			getenc_check_encodings(O_WRONLY, expected);
   6107 		}
   6108 		if (hw_canplay() && hw_canrec()) {
   6109 			xxx_close_wait();
   6110 		}
   6111 		if (hw_canrec()) {
   6112 			getenc_check_encodings(O_RDONLY, expected);
   6113 		}
   6114 	}
   6115 }
   6116 #undef NENC
   6117 #undef NPREC
   6118 
   6119 /*
   6120  * Check AUDIO_GETENC out of range.
   6121  */
   6122 DEF(AUDIO_GETENC_error)
   6123 {
   6124 	audio_encoding_t e;
   6125 	int fd;
   6126 	int r;
   6127 
   6128 	TEST("AUDIO_GETENC_error");
   6129 
   6130 	fd = OPEN(devaudio, openable_mode());
   6131 	REQUIRED_SYS_OK(fd);
   6132 
   6133 	memset(&e, 0, sizeof(e));
   6134 	e.index = -1;
   6135 	r = IOCTL(fd, AUDIO_GETENC, &e, "index=-1");
   6136 	/* NetBSD7 may not fail depending on hardware driver */
   6137 	XP_SYS_NG(EINVAL, r);
   6138 
   6139 	r = CLOSE(fd);
   6140 	XP_SYS_EQ(0, r);
   6141 }
   6142 
   6143 /*
   6144  * AUDIO_[PR]ERROR should be zero on the initial state even on non-existent
   6145  * track.
   6146  */
   6147 void
   6148 test_AUDIO_ERROR(int openmode)
   6149 {
   6150 	int fd;
   6151 	int r;
   6152 	int errors;
   6153 
   6154 	TEST("AUDIO_ERROR_%s", openmode_str[openmode] + 2);
   6155 	if (mode2aumode(openmode) == 0) {
   6156 		XP_SKIP("Operation not allowed on this hardware property");
   6157 		return;
   6158 	}
   6159 
   6160 	fd = OPEN(devaudio, openmode);
   6161 	REQUIRED_SYS_OK(fd);
   6162 
   6163 	/* Check PERROR */
   6164 	errors = 0xdeadbeef;
   6165 	r = IOCTL(fd, AUDIO_PERROR, &errors, "");
   6166 	XP_SYS_EQ(0, r);
   6167 	XP_EQ(0, errors);
   6168 
   6169 	/* Check RERROR */
   6170 	errors = 0xdeadbeef;
   6171 	r = IOCTL(fd, AUDIO_RERROR, &errors, "");
   6172 	XP_SYS_EQ(0, r);
   6173 	XP_EQ(0, errors);
   6174 
   6175 	r = CLOSE(fd);
   6176 	XP_SYS_EQ(0, r);
   6177 }
   6178 DEF(AUDIO_ERROR_RDONLY)	{ test_AUDIO_ERROR(O_RDONLY); }
   6179 DEF(AUDIO_ERROR_WRONLY)	{ test_AUDIO_ERROR(O_WRONLY); }
   6180 DEF(AUDIO_ERROR_RDWR)	{ test_AUDIO_ERROR(O_RDWR); }
   6181 
   6182 /*
   6183  * AUDIO_GETIOFFS at least one block.
   6184  */
   6185 void
   6186 test_AUDIO_GETIOFFS_one(int openmode)
   6187 {
   6188 	struct audio_info ai;
   6189 	audio_offset_t o;
   6190 	int fd;
   6191 	int r;
   6192 	u_int blocksize;
   6193 	u_int blk_ms;
   6194 
   6195 	TEST("AUDIO_GETIOFFS_one_%s", openmode_str[openmode] + 2);
   6196 	if (mode2aumode(openmode) == 0) {
   6197 		XP_SKIP("Operation not allowed on this hardware property");
   6198 		return;
   6199 	}
   6200 
   6201 	fd = OPEN(devaudio, openmode);
   6202 	REQUIRED_SYS_OK(fd);
   6203 
   6204 #if 0
   6205 	/*
   6206 	 * On NetBSD7/8, native encodings and emulated encodings behave
   6207 	 * differently.  But it's hard to identify which encoding is native.
   6208 	 * If you try other encodings, edit these parameters manually.
   6209 	 */
   6210 	AUDIO_INITINFO(&ai);
   6211 	ai.record.encoding = AUDIO_ENCODING_SLINEAR_NE;
   6212 	ai.record.precision = 16;
   6213 	ai.record.channels = 2;
   6214 	ai.record.sample_rate = 48000;
   6215 	/* ai.blocksize is shared by play and record, so set both the same. */
   6216 	*ai.play = *ai.record;
   6217 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   6218 	REQUIRED_SYS_EQ(0, r);
   6219 #endif
   6220 
   6221 	/* Get blocksize to calc blk_ms. */
   6222 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6223 	REQUIRED_SYS_EQ(0, r);
   6224 	blocksize = ai.blocksize;
   6225 	if (netbsd < 9) {
   6226 		blk_ms = 0;
   6227 	} else {
   6228 		/* On NetBSD9, blocktime can always be calculated. */
   6229 		blk_ms = blocksize * 1000 /
   6230 		    (ai.play.precision / 8 * ai.play.channels *
   6231 		     ai.play.sample_rate);
   6232 	}
   6233 	if (blk_ms == 0)
   6234 		blk_ms = 50;
   6235 	DPRINTF("  > blocksize=%u, estimated blk_ms=%u\n", blocksize, blk_ms);
   6236 
   6237 	/*
   6238 	 * Even when just opened, recording counters will start.
   6239 	 * Wait a moment, about one block time.
   6240 	 */
   6241 	usleep(blk_ms * 1000);
   6242 
   6243 	r = IOCTL(fd, AUDIO_GETIOFFS, &o, "");
   6244 	XP_SYS_EQ(0, r);
   6245 	if (mode2rec(openmode)) {
   6246 		/*
   6247 		 * It's difficult to know exact values.
   6248 		 * But at least these should not be zero.
   6249 		 */
   6250 		DPRINTF("  > %d: samples=%u deltablks=%u offset=%u\n",
   6251 		    __LINE__, o.samples, o.deltablks, o.offset);
   6252 		XP_NE(0, o.samples);
   6253 		XP_NE(0, o.deltablks);
   6254 		XP_NE(0, o.offset);
   6255 	} else {
   6256 		/* All are zero on playback track. */
   6257 		XP_EQ(0, o.samples);
   6258 		XP_EQ(0, o.deltablks);
   6259 		XP_EQ(0, o.offset);
   6260 	}
   6261 
   6262 	r = CLOSE(fd);
   6263 	XP_SYS_EQ(0, r);
   6264 }
   6265 DEF(AUDIO_GETIOFFS_one_RDONLY) { test_AUDIO_GETIOFFS_one(O_RDONLY); }
   6266 DEF(AUDIO_GETIOFFS_one_WRONLY) { test_AUDIO_GETIOFFS_one(O_WRONLY); }
   6267 DEF(AUDIO_GETIOFFS_one_RDWR)   { test_AUDIO_GETIOFFS_one(O_RDWR); }
   6268 
   6269 /*
   6270  * AUDIO_GETOOFFS for one block.
   6271  */
   6272 void
   6273 test_AUDIO_GETOOFFS_one(int openmode)
   6274 {
   6275 	struct audio_info ai;
   6276 	audio_offset_t o;
   6277 	char *buf;
   6278 	int fd;
   6279 	int r;
   6280 	u_int blocksize;
   6281 	u_int initial_offset;
   6282 	u_int blk_ms;
   6283 
   6284 	TEST("AUDIO_GETOOFFS_one_%s", openmode_str[openmode] + 2);
   6285 	if (mode2aumode(openmode) == 0) {
   6286 		XP_SKIP("Operation not allowed on this hardware property");
   6287 		return;
   6288 	}
   6289 
   6290 	fd = OPEN(devaudio, openmode);
   6291 	REQUIRED_SYS_OK(fd);
   6292 
   6293 #if 0
   6294 	/*
   6295 	 * On NetBSD7/8, native encodings and emulated encodings behave
   6296 	 * differently.  But it's hard to identify which encoding is native.
   6297 	 * If you try other encodings, edit these parameters manually.
   6298 	 */
   6299 	AUDIO_INITINFO(&ai);
   6300 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_NE;
   6301 	ai.play.precision = 16;
   6302 	ai.play.channels = 2;
   6303 	ai.play.sample_rate = 48000;
   6304 	/* ai.blocksize is shared by play and record, so set both the same. */
   6305 	*ai.record = *ai.play;
   6306 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "slinear16/2ch/48000");
   6307 	REQUIRED_SYS_EQ(0, r);
   6308 #endif
   6309 
   6310 	/* Get blocksize to calc blk_ms. */
   6311 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6312 	REQUIRED_SYS_EQ(0, r);
   6313 	blocksize = ai.blocksize;
   6314 	if (netbsd < 9) {
   6315 		blk_ms = 0;
   6316 	} else {
   6317 		/* On NetBSD9, blocktime can always be calculated. */
   6318 		blk_ms = blocksize * 1000 /
   6319 		    (ai.play.precision / 8 * ai.play.channels *
   6320 		     ai.play.sample_rate);
   6321 	}
   6322 	if (blk_ms == 0)
   6323 		blk_ms = 50;
   6324 	DPRINTF("  > blocksize=%u, estimated blk_ms=%u\n", blocksize, blk_ms);
   6325 
   6326 	buf = (char *)malloc(blocksize);
   6327 	REQUIRED_IF(buf != NULL);
   6328 	memset(buf, 0xff, blocksize);
   6329 
   6330 	/*
   6331 	 * On NetBSD7, .offset starts from one block.  What is the block??
   6332 	 * On NetBSD9, .offset starts from zero.
   6333 	 */
   6334 	if (netbsd < 9) {
   6335 		initial_offset = blocksize;
   6336 	} else {
   6337 		initial_offset = 0;
   6338 	}
   6339 
   6340 	/* When just opened, all are zero. */
   6341 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6342 	XP_SYS_EQ(0, r);
   6343 	XP_EQ(0, o.samples);
   6344 	XP_EQ(0, o.deltablks);
   6345 	XP_EQ(initial_offset, o.offset);
   6346 
   6347 	/* Even if wait (at least) one block, these remain unchanged. */
   6348 	usleep(blk_ms * 1000);
   6349 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6350 	XP_SYS_EQ(0, r);
   6351 	XP_EQ(0, o.samples);
   6352 	XP_EQ(0, o.deltablks);
   6353 	XP_EQ(initial_offset, o.offset);
   6354 
   6355 	/* Write one block. */
   6356 	r = WRITE(fd, buf, blocksize);
   6357 	if (mode2play(openmode)) {
   6358 		XP_SYS_EQ(blocksize, r);
   6359 	} else {
   6360 		XP_SYS_NG(EBADF, r);
   6361 	}
   6362 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   6363 	REQUIRED_SYS_EQ(0, r);
   6364 
   6365 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6366 	XP_SYS_EQ(0, r);
   6367 	if (mode2play(openmode)) {
   6368 		/* All advance one block. */
   6369 		XP_EQ(blocksize, o.samples);
   6370 		XP_EQ(1, o.deltablks);
   6371 		XP_EQ(initial_offset + blocksize, o.offset);
   6372 	} else {
   6373 		/*
   6374 		 * All are zero on non-play track.
   6375 		 * On NetBSD7, the rec track has play buffer, too.
   6376 		 */
   6377 		XP_EQ(0, o.samples);
   6378 		XP_EQ(0, o.deltablks);
   6379 		XP_EQ(initial_offset, o.offset);
   6380 	}
   6381 
   6382 	r = CLOSE(fd);
   6383 	XP_SYS_EQ(0, r);
   6384 
   6385 	free(buf);
   6386 }
   6387 DEF(AUDIO_GETOOFFS_one_RDONLY) { test_AUDIO_GETOOFFS_one(O_RDONLY); }
   6388 DEF(AUDIO_GETOOFFS_one_WRONLY) { test_AUDIO_GETOOFFS_one(O_WRONLY); }
   6389 DEF(AUDIO_GETOOFFS_one_RDWR)   { test_AUDIO_GETOOFFS_one(O_RDWR); }
   6390 
   6391 /*
   6392  * AUDIO_GETOOFFS when wrap around buffer.
   6393  */
   6394 void
   6395 test_AUDIO_GETOOFFS_wrap(int openmode)
   6396 {
   6397 	struct audio_info ai;
   6398 	audio_offset_t o;
   6399 	char *buf;
   6400 	int fd;
   6401 	int r;
   6402 	u_int blocksize;
   6403 	u_int buffer_size;
   6404 	u_int initial_offset;
   6405 	u_int nblks;
   6406 
   6407 	TEST("AUDIO_GETOOFFS_wrap_%s", openmode_str[openmode] + 2);
   6408 	if (mode2aumode(openmode) == 0) {
   6409 		XP_SKIP("Operation not allowed on this hardware property");
   6410 		return;
   6411 	}
   6412 
   6413 	fd = OPEN(devaudio, openmode);
   6414 	REQUIRED_SYS_OK(fd);
   6415 
   6416 #if 1
   6417 	/* To save test time, use larger format if possible. */
   6418 	AUDIO_INITINFO(&ai);
   6419 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_NE;
   6420 	ai.play.precision = 16;
   6421 	ai.play.channels = 2;
   6422 	ai.play.sample_rate = 48000;
   6423 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "slinear16/2/48000");
   6424 	if (r != 0)
   6425 #endif
   6426 	{
   6427 		/*
   6428 		 * If it cannot be set, use common format instead.
   6429 		 * May be happened on NetBSD7/8.
   6430 		 */
   6431 		AUDIO_INITINFO(&ai);
   6432 		ai.play.encoding = AUDIO_ENCODING_ULAW;
   6433 		ai.play.precision = 8;
   6434 		ai.play.channels = 1;
   6435 		ai.play.sample_rate = 8000;
   6436 		r = IOCTL(fd, AUDIO_SETINFO, &ai, "ulaw/1/8000");
   6437 	}
   6438 	REQUIRED_SYS_EQ(0, r);
   6439 
   6440 	/* Get buffer_size and blocksize. */
   6441 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6442 	REQUIRED_SYS_EQ(0, r);
   6443 	buffer_size = ai.play.buffer_size;
   6444 	blocksize = ai.blocksize;
   6445 	nblks = buffer_size / blocksize;
   6446 	DPRINTF("  > buffer_size=%u blocksize=%u nblks=%u\n",
   6447 	    buffer_size, blocksize, nblks);
   6448 
   6449 	buf = (char *)malloc(buffer_size);
   6450 	REQUIRED_IF(buf != NULL);
   6451 	memset(buf, 0xff, buffer_size);
   6452 
   6453 	/*
   6454 	 * On NetBSD7, .offset starts from one block.  What is the block??
   6455 	 * On NetBSD9, .offset starts from zero.
   6456 	 */
   6457 	if (netbsd < 9) {
   6458 		initial_offset = blocksize;
   6459 	} else {
   6460 		initial_offset = 0;
   6461 	}
   6462 
   6463 	/* Write full buffer. */
   6464 	r = WRITE(fd, buf, buffer_size);
   6465 	if (mode2play(openmode)) {
   6466 		XP_SYS_EQ(buffer_size, r);
   6467 
   6468 		/* Then, wait. */
   6469 		r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   6470 		REQUIRED_SYS_EQ(0, r);
   6471 	} else {
   6472 		XP_SYS_NG(EBADF, r);
   6473 	}
   6474 
   6475 	/*
   6476 	 * .deltablks is number of blocks since last checked.
   6477 	 * .offset is wrapped around to zero.
   6478 	 */
   6479 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6480 	XP_SYS_EQ(0, r);
   6481 	if (mode2play(openmode)) {
   6482 		/*
   6483 		 * On NetBSD7, samples may be blocksize * nblks or buffer_size
   6484 		 * depending on native/emulated encoding.
   6485 		 * On NetBSD9, samples is always equal to buffer_size.
   6486 		 */
   6487 		if (buffer_size != blocksize * nblks &&
   6488 		    o.samples == blocksize * nblks) {
   6489 			DPRINTF("  > %d: samples(%u) == blocksize * nblks\n",
   6490 			    __LINE__, o.samples);
   6491 		} else {
   6492 			XP_EQ(buffer_size, o.samples);
   6493 		}
   6494 		XP_EQ(nblks, o.deltablks);
   6495 		XP_EQ(initial_offset, o.offset);
   6496 	} else {
   6497 		/*
   6498 		 * On non-play track, it silently succeeds with zero.
   6499 		 * But on NetBSD7, RDONLY descriptor also has play buffer.
   6500 		 */
   6501 		XP_EQ(0, o.samples);
   6502 		XP_EQ(0, o.deltablks);
   6503 		XP_EQ(initial_offset, o.offset);
   6504 	}
   6505 
   6506 	r = CLOSE(fd);
   6507 	XP_SYS_EQ(0, r);
   6508 
   6509 	free(buf);
   6510 }
   6511 DEF(AUDIO_GETOOFFS_wrap_RDONLY) { test_AUDIO_GETOOFFS_wrap(O_RDONLY); }
   6512 DEF(AUDIO_GETOOFFS_wrap_WRONLY) { test_AUDIO_GETOOFFS_wrap(O_WRONLY); }
   6513 DEF(AUDIO_GETOOFFS_wrap_RDWR)   { test_AUDIO_GETOOFFS_wrap(O_RDWR); }
   6514 
   6515 /*
   6516  * Check whether AUDIO_FLUSH clears AUDIO_GETOOFFS.
   6517  */
   6518 void
   6519 test_AUDIO_GETOOFFS_flush(int openmode)
   6520 {
   6521 	struct audio_info ai;
   6522 	audio_offset_t o;
   6523 	char *buf;
   6524 	int fd;
   6525 	int r;
   6526 	u_int initial_offset;
   6527 	u_int last_offset;
   6528 
   6529 	TEST("AUDIO_GETOOFFS_flush_%s", openmode_str[openmode] + 2);
   6530 	if (mode2aumode(openmode) == 0) {
   6531 		XP_SKIP("Operation not allowed on this hardware property");
   6532 		return;
   6533 	}
   6534 
   6535 	fd = OPEN(devaudio, openmode);
   6536 	REQUIRED_SYS_OK(fd);
   6537 
   6538 #if 0
   6539 	/* On NetBSD7/8, native encoding changes buffer behavior. */
   6540 	AUDIO_INITINFO(&ai);
   6541 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_NE;
   6542 	ai.play.precision = 16;
   6543 	ai.play.channels = 2;
   6544 	ai.play.sample_rate = 48000;
   6545 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "");
   6546 	REQUIRED_SYS_EQ(0, r);
   6547 #endif
   6548 
   6549 	/* Get blocksize. */
   6550 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6551 	REQUIRED_SYS_EQ(0, r);
   6552 
   6553 	buf = (char *)malloc(ai.blocksize);
   6554 	REQUIRED_IF(buf != NULL);
   6555 	memset(buf, 0xff, ai.blocksize);
   6556 
   6557 	/*
   6558 	 * On NetBSD7, .offset starts from one block.  What is the block??
   6559 	 * On NetBSD9, .offset starts from zero.
   6560 	 */
   6561 	if (netbsd < 9) {
   6562 		initial_offset = ai.blocksize;
   6563 	} else {
   6564 		initial_offset = 0;
   6565 	}
   6566 
   6567 	/* Write one block. */
   6568 	r = WRITE(fd, buf, ai.blocksize);
   6569 	if (mode2play(openmode)) {
   6570 		XP_SYS_EQ(ai.blocksize, r);
   6571 	} else {
   6572 		XP_SYS_NG(EBADF, r);
   6573 	}
   6574 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   6575 	XP_SYS_EQ(0, r);
   6576 
   6577 	/* Obtain once. */
   6578 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6579 	XP_SYS_EQ(0, r);
   6580 	if (mode2play(openmode)) {
   6581 		XP_EQ(ai.blocksize, o.samples);
   6582 		XP_EQ(1, o.deltablks);
   6583 		XP_EQ(initial_offset + ai.blocksize, o.offset);
   6584 	} else {
   6585 		/*
   6586 		 * On non-play track, it silently succeeds with zero.
   6587 		 * But on NetBSD7, RDONLY descriptor also has play buffer.
   6588 		 */
   6589 		XP_EQ(0, o.samples);
   6590 		XP_EQ(0, o.deltablks);
   6591 		XP_EQ(initial_offset, o.offset);
   6592 	}
   6593 
   6594 	/* Write one more block to advance .offset. */
   6595 	r = WRITE(fd, buf, ai.blocksize);
   6596 	if (mode2play(openmode)) {
   6597 		XP_SYS_EQ(ai.blocksize, r);
   6598 	} else {
   6599 		XP_SYS_NG(EBADF, r);
   6600 	}
   6601 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   6602 	XP_SYS_EQ(0, r);
   6603 
   6604 	/* If offset remains unchanged, this is expected offset. */
   6605 	last_offset = initial_offset + ai.blocksize * 2;
   6606 
   6607 	/* Then, flush. */
   6608 	r = IOCTL(fd, AUDIO_FLUSH, NULL, "");
   6609 	REQUIRED_SYS_EQ(0, r);
   6610 
   6611 	/* All should be cleared. */
   6612 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6613 	XP_SYS_EQ(0, r);
   6614 	XP_EQ(0, o.samples);
   6615 	XP_EQ(0, o.deltablks);
   6616 	if (mode2play(openmode)) {
   6617 		/*
   6618 		 * On NetBSD7,
   6619 		 * offset is cleared if native encodings(?), but remains
   6620 		 * unchanged if emulated encodings(?).  Looks a bug.
   6621 		 * On NetBSD9, it should always be cleared.
   6622 		 */
   6623 		if (netbsd < 9 && o.offset == last_offset) {
   6624 			DPRINTF("  > %d: offset(%u) == last_offset\n",
   6625 			    __LINE__, o.offset);
   6626 		} else {
   6627 			XP_EQ(initial_offset, o.offset);
   6628 		}
   6629 	} else {
   6630 		XP_EQ(initial_offset, o.offset);
   6631 	}
   6632 
   6633 	r = CLOSE(fd);
   6634 	XP_SYS_EQ(0, r);
   6635 
   6636 	free(buf);
   6637 }
   6638 DEF(AUDIO_GETOOFFS_flush_RDONLY) { test_AUDIO_GETOOFFS_flush(O_RDONLY); }
   6639 DEF(AUDIO_GETOOFFS_flush_WRONLY) { test_AUDIO_GETOOFFS_flush(O_WRONLY); }
   6640 DEF(AUDIO_GETOOFFS_flush_RDWR)   { test_AUDIO_GETOOFFS_flush(O_RDWR); }
   6641 
   6642 /*
   6643  * Check whether AUDIO_SETINFO(encoding) clears AUDIO_GETOOFFS.
   6644  */
   6645 void
   6646 test_AUDIO_GETOOFFS_set(int openmode)
   6647 {
   6648 	struct audio_info ai;
   6649 	audio_offset_t o;
   6650 	char *buf;
   6651 	int fd;
   6652 	int r;
   6653 	u_int initial_offset;
   6654 
   6655 	TEST("AUDIO_GETOOFFS_set_%s", openmode_str[openmode] + 2);
   6656 	if (mode2aumode(openmode) == 0) {
   6657 		XP_SKIP("Operation not allowed on this hardware property");
   6658 		return;
   6659 	}
   6660 
   6661 	fd = OPEN(devaudio, openmode);
   6662 	REQUIRED_SYS_OK(fd);
   6663 
   6664 	/* Get blocksize. */
   6665 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6666 	XP_SYS_EQ(0, r);
   6667 
   6668 	buf = (char *)malloc(ai.blocksize);
   6669 	REQUIRED_IF(buf != NULL);
   6670 	memset(buf, 0xff, ai.blocksize);
   6671 
   6672 	/*
   6673 	 * On NetBSD7, .offset starts from one block.  What is the block??
   6674 	 * On NetBSD9, .offset starts from zero.
   6675 	 */
   6676 	if (netbsd < 9) {
   6677 		initial_offset = ai.blocksize;
   6678 	} else {
   6679 		initial_offset = 0;
   6680 	}
   6681 
   6682 	/* Write one block. */
   6683 	r = WRITE(fd, buf, ai.blocksize);
   6684 	if (mode2play(openmode)) {
   6685 		XP_SYS_EQ(ai.blocksize, r);
   6686 	} else {
   6687 		XP_SYS_NG(EBADF, r);
   6688 	}
   6689 	r = IOCTL(fd, AUDIO_DRAIN, NULL, "");
   6690 	XP_SYS_EQ(0, r);
   6691 
   6692 	/*
   6693 	 * Then, change encoding.
   6694 	 * If we fail to change it, we cannot continue.  This may happen
   6695 	 * on NetBSD7/8.
   6696 	 */
   6697 	AUDIO_INITINFO(&ai);
   6698 	ai.play.encoding = AUDIO_ENCODING_SLINEAR_NE;
   6699 	ai.play.precision = 16;
   6700 	ai.play.channels = 2;
   6701 	ai.play.sample_rate = 48000;
   6702 	r = IOCTL(fd, AUDIO_SETINFO, &ai, "slinear16/2ch/48000");
   6703 	REQUIRED_SYS_EQ(0, r);
   6704 
   6705 	r = IOCTL(fd, AUDIO_GETBUFINFO, &ai, "");
   6706 	REQUIRED_SYS_EQ(0, r);
   6707 	if (netbsd < 9) {
   6708 		initial_offset = ai.blocksize;
   6709 	} else {
   6710 		initial_offset = 0;
   6711 	}
   6712 
   6713 	/* Clear counters? */
   6714 	r = IOCTL(fd, AUDIO_GETOOFFS, &o, "");
   6715 	XP_SYS_EQ(0, r);
   6716 	XP_EQ(0, o.samples);
   6717 	XP_EQ(0, o.deltablks);
   6718 	XP_EQ(initial_offset, o.offset);
   6719 
   6720 	r = CLOSE(fd);
   6721 	XP_SYS_EQ(0, r);
   6722 
   6723 	free(buf);
   6724 }
   6725 DEF(AUDIO_GETOOFFS_set_RDONLY) { test_AUDIO_GETOOFFS_set(O_RDONLY); }
   6726 DEF(AUDIO_GETOOFFS_set_WRONLY) { test_AUDIO_GETOOFFS_set(O_WRONLY); }
   6727 DEF(AUDIO_GETOOFFS_set_RDWR)   { test_AUDIO_GETOOFFS_set(O_RDWR); }
   6728 
   6729 /*
   6730  * /dev/audioctl can always be opened while /dev/audio is open.
   6731  */
   6732 void
   6733 test_audioctl_open_1(int fmode, int cmode)
   6734 {
   6735 	int fd;
   6736 	int ctl;
   6737 	int r;
   6738 
   6739 	TEST("audioctl_open_1_%s_%s",
   6740 	    openmode_str[fmode] + 2, openmode_str[cmode] + 2);
   6741 	if (hw_canplay() == 0 && fmode == O_WRONLY) {
   6742 		XP_SKIP("This test is for playable device");
   6743 		return;
   6744 	}
   6745 	if (hw_canrec() == 0 && fmode == O_RDONLY) {
   6746 		XP_SKIP("This test is for recordable device");
   6747 		return;
   6748 	}
   6749 
   6750 	fd = OPEN(devaudio, fmode);
   6751 	REQUIRED_SYS_OK(fd);
   6752 
   6753 	ctl = OPEN(devaudioctl, cmode);
   6754 	XP_SYS_OK(ctl);
   6755 
   6756 	r = CLOSE(ctl);
   6757 	XP_SYS_EQ(0, r);
   6758 
   6759 	r = CLOSE(fd);
   6760 	XP_SYS_EQ(0, r);
   6761 }
   6762 DEF(audioctl_open_1_RDONLY_RDONLY) { test_audioctl_open_1(O_RDONLY, O_RDONLY); }
   6763 DEF(audioctl_open_1_RDONLY_RWONLY) { test_audioctl_open_1(O_RDONLY, O_WRONLY); }
   6764 DEF(audioctl_open_1_RDONLY_RDWR)   { test_audioctl_open_1(O_RDONLY, O_RDWR); }
   6765 DEF(audioctl_open_1_WRONLY_RDONLY) { test_audioctl_open_1(O_WRONLY, O_RDONLY); }
   6766 DEF(audioctl_open_1_WRONLY_RWONLY) { test_audioctl_open_1(O_WRONLY, O_WRONLY); }
   6767 DEF(audioctl_open_1_WRONLY_RDWR)   { test_audioctl_open_1(O_WRONLY, O_RDWR); }
   6768 DEF(audioctl_open_1_RDWR_RDONLY)   { test_audioctl_open_1(O_RDWR, O_RDONLY); }
   6769 DEF(audioctl_open_1_RDWR_RWONLY)   { test_audioctl_open_1(O_RDWR, O_WRONLY); }
   6770 DEF(audioctl_open_1_RDWR_RDWR)     { test_audioctl_open_1(O_RDWR, O_RDWR); }
   6771 
   6772 /*
   6773  * /dev/audio can always be opened while /dev/audioctl is open.
   6774  */
   6775 void
   6776 test_audioctl_open_2(int fmode, int cmode)
   6777 {
   6778 	int fd;
   6779 	int ctl;
   6780 	int r;
   6781 
   6782 	TEST("audioctl_open_2_%s_%s",
   6783 	    openmode_str[fmode] + 2, openmode_str[cmode] + 2);
   6784 	if (hw_canplay() == 0 && fmode == O_WRONLY) {
   6785 		XP_SKIP("This test is for playable device");
   6786 		return;
   6787 	}
   6788 	if (hw_canrec() == 0 && fmode == O_RDONLY) {
   6789 		XP_SKIP("This test is for recordable device");
   6790 		return;
   6791 	}
   6792 
   6793 	ctl = OPEN(devaudioctl, cmode);
   6794 	REQUIRED_SYS_OK(ctl);
   6795 
   6796 	fd = OPEN(devaudio, fmode);
   6797 	XP_SYS_OK(fd);
   6798 
   6799 	r = CLOSE(fd);
   6800 	XP_SYS_EQ(0, r);
   6801 
   6802 	r = CLOSE(ctl);
   6803 	XP_SYS_EQ(0, r);
   6804 }
   6805 DEF(audioctl_open_2_RDONLY_RDONLY) { test_audioctl_open_2(O_RDONLY, O_RDONLY); }
   6806 DEF(audioctl_open_2_RDONLY_RWONLY) { test_audioctl_open_2(O_RDONLY, O_WRONLY); }
   6807 DEF(audioctl_open_2_RDONLY_RDWR)   { test_audioctl_open_2(O_RDONLY, O_RDWR); }
   6808 DEF(audioctl_open_2_WRONLY_RDONLY) { test_audioctl_open_2(O_WRONLY, O_RDONLY); }
   6809 DEF(audioctl_open_2_WRONLY_RWONLY) { test_audioctl_open_2(O_WRONLY, O_WRONLY); }
   6810 DEF(audioctl_open_2_WRONLY_RDWR)   { test_audioctl_open_2(O_WRONLY, O_RDWR); }
   6811 DEF(audioctl_open_2_RDWR_RDONLY)   { test_audioctl_open_2(O_RDWR, O_RDONLY); }
   6812 DEF(audioctl_open_2_RDWR_RWONLY)   { test_audioctl_open_2(O_RDWR, O_WRONLY); }
   6813 DEF(audioctl_open_2_RDWR_RDWR)     { test_audioctl_open_2(O_RDWR, O_RDWR); }
   6814 
   6815 /*
   6816  * Open multiple /dev/audioctl.
   6817  */
   6818 DEF(audioctl_open_simul)
   6819 {
   6820 	int ctl0;
   6821 	int ctl1;
   6822 	int r;
   6823 
   6824 	TEST("audioctl_open_simul");
   6825 
   6826 	ctl0 = OPEN(devaudioctl, O_RDWR);
   6827 	REQUIRED_SYS_OK(ctl0);
   6828 
   6829 	ctl1 = OPEN(devaudioctl, O_RDWR);
   6830 	XP_SYS_OK(ctl1);
   6831 
   6832 	r = CLOSE(ctl0);
   6833 	XP_SYS_EQ(0, r);
   6834 
   6835 	r = CLOSE(ctl1);
   6836 	XP_SYS_EQ(0, r);
   6837 }
   6838 
   6839 /*
   6840  * /dev/audioctl can be opened by other user who opens /dev/audioctl,
   6841  * /dev/audioctl can be opened by other user who opens /dev/audio,
   6842  * /dev/audio    can be opened by other user who opens /dev/audioctl,
   6843  * regardless of multiuser mode.
   6844  */
   6845 void
   6846 try_audioctl_open_multiuser(const char *dev1, const char *dev2)
   6847 {
   6848 	int fd1;
   6849 	int fd2;
   6850 	int r;
   6851 	uid_t ouid;
   6852 
   6853 	/*
   6854 	 * At first, open dev1 as root.
   6855 	 * And then open dev2 as unprivileged user.
   6856 	 */
   6857 
   6858 	fd1 = OPEN(dev1, O_RDWR);
   6859 	REQUIRED_SYS_OK(fd1);
   6860 
   6861 	ouid = GETUID();
   6862 	r = SETEUID(1);
   6863 	REQUIRED_SYS_EQ(0, r);
   6864 
   6865 	fd2 = OPEN(dev2, O_RDWR);
   6866 	XP_SYS_OK(fd2);
   6867 
   6868 	/* Close */
   6869 	r = CLOSE(fd2);
   6870 	XP_SYS_EQ(0, r);
   6871 
   6872 	r = SETEUID(ouid);
   6873 	REQUIRED_SYS_EQ(0, r);
   6874 
   6875 	r = CLOSE(fd1);
   6876 	XP_SYS_EQ(0, r);
   6877 }
   6878 /*
   6879  * This is a wrapper for audioctl_open_multiuser.
   6880  * XXX XP_* macros are not compatible with on-error-goto, we need try-catch...
   6881  */
   6882 void
   6883 test_audioctl_open_multiuser(bool multiuser,
   6884 	const char *dev1, const char *dev2)
   6885 {
   6886 	char mibname[32];
   6887 	bool oldval;
   6888 	size_t oldlen;
   6889 	int r;
   6890 
   6891 	if (netbsd < 8 && multiuser == 1) {
   6892 		XP_SKIP("multiuser is not supported");
   6893 		return;
   6894 	}
   6895 	if (netbsd < 9) {
   6896 		/* NetBSD8 has no way (difficult) to determine device name */
   6897 		XP_SKIP("NetBSD8 cannot determine device name");
   6898 		return;
   6899 	}
   6900 	if (geteuid() != 0) {
   6901 		XP_SKIP("This test must be priviledged user");
   6902 		return;
   6903 	}
   6904 
   6905 	/* Get current multiuser mode (and save it) */
   6906 	snprintf(mibname, sizeof(mibname), "hw.%s.multiuser", devicename);
   6907 	oldlen = sizeof(oldval);
   6908 	r = SYSCTLBYNAME(mibname, &oldval, &oldlen, NULL, 0);
   6909 	REQUIRED_SYS_EQ(0, r);
   6910 	DPRINTF("  > multiuser=%d\n", oldval);
   6911 
   6912 	/* Change if necessary */
   6913 	if (oldval != multiuser) {
   6914 		r = SYSCTLBYNAME(mibname, NULL, NULL, &multiuser,
   6915 		    sizeof(multiuser));
   6916 		REQUIRED_SYS_EQ(0, r);
   6917 		DPRINTF("  > new multiuser=%d\n", multiuser);
   6918 	}
   6919 
   6920 	/* Do test */
   6921 	try_audioctl_open_multiuser(dev1, dev2);
   6922 
   6923 	/* Restore multiuser mode */
   6924 	if (oldval != multiuser) {
   6925 		DPRINTF("  > restore multiuser to %d\n", oldval);
   6926 		r = SYSCTLBYNAME(mibname, NULL, NULL, &oldval, sizeof(oldval));
   6927 		XP_SYS_EQ(0, r);
   6928 	}
   6929 }
   6930 DEF(audioctl_open_multiuser0_audio1) {
   6931 	TEST("audioctl_open_multiuser0_audio1");
   6932 	test_audioctl_open_multiuser(false, devaudio, devaudioctl);
   6933 }
   6934 DEF(audioctl_open_multiuser1_audio1) {
   6935 	TEST("audioctl_open_multiuser1_audio1");
   6936 	test_audioctl_open_multiuser(true, devaudio, devaudioctl);
   6937 }
   6938 DEF(audioctl_open_multiuser0_audio2) {
   6939 	TEST("audioctl_open_multiuser0_audio2");
   6940 	test_audioctl_open_multiuser(false, devaudioctl, devaudio);
   6941 }
   6942 DEF(audioctl_open_multiuser1_audio2) {
   6943 	TEST("audioctl_open_multiuser1_audio2");
   6944 	test_audioctl_open_multiuser(true, devaudioctl, devaudio);
   6945 }
   6946 DEF(audioctl_open_multiuser0_audioctl) {
   6947 	TEST("audioctl_open_multiuser0_audioctl");
   6948 	test_audioctl_open_multiuser(false, devaudioctl, devaudioctl);
   6949 }
   6950 DEF(audioctl_open_multiuser1_audioctl) {
   6951 	TEST("audioctl_open_multiuser1_audioctl");
   6952 	test_audioctl_open_multiuser(true, devaudioctl, devaudioctl);
   6953 }
   6954 
   6955 /*
   6956  * /dev/audioctl cannot be read/written regardless of its open mode.
   6957  */
   6958 void
   6959 test_audioctl_rw(int openmode)
   6960 {
   6961 	char buf[1];
   6962 	int fd;
   6963 	int r;
   6964 
   6965 	TEST("audioctl_rw_%s", openmode_str[openmode] + 2);
   6966 
   6967 	fd = OPEN(devaudioctl, openmode);
   6968 	REQUIRED_SYS_OK(fd);
   6969 
   6970 	if (mode2play(openmode)) {
   6971 		r = WRITE(fd, buf, sizeof(buf));
   6972 		XP_SYS_NG(ENODEV, r);
   6973 	}
   6974 
   6975 	if (mode2rec(openmode)) {
   6976 		r = READ(fd, buf, sizeof(buf));
   6977 		XP_SYS_NG(ENODEV, r);
   6978 	}
   6979 
   6980 	r = CLOSE(fd);
   6981 	XP_SYS_EQ(0, r);
   6982 }
   6983 DEF(audioctl_rw_RDONLY)	{ test_audioctl_rw(O_RDONLY); }
   6984 DEF(audioctl_rw_WRONLY)	{ test_audioctl_rw(O_WRONLY); }
   6985 DEF(audioctl_rw_RDWR)	{ test_audioctl_rw(O_RDWR); }
   6986 
   6987 /*
   6988  * poll(2) for /dev/audioctl should never raise.
   6989  * I'm not sure about consistency between poll(2) and kqueue(2) but
   6990  * anyway I follow it.
   6991  * XXX Omit checking each openmode
   6992  */
   6993 DEF(audioctl_poll)
   6994 {
   6995 	struct pollfd pfd;
   6996 	int fd;
   6997 	int r;
   6998 
   6999 	TEST("audioctl_poll");
   7000 
   7001 	fd = OPEN(devaudioctl, O_WRONLY);
   7002 	REQUIRED_SYS_OK(fd);
   7003 
   7004 	pfd.fd = fd;
   7005 	pfd.events = POLLOUT;
   7006 	r = POLL(&pfd, 1, 100);
   7007 	XP_SYS_EQ(0, r);
   7008 	XP_EQ(0, pfd.revents);
   7009 
   7010 	r = CLOSE(fd);
   7011 	XP_SYS_EQ(0, r);
   7012 }
   7013 
   7014 /*
   7015  * kqueue(2) for /dev/audioctl fails.
   7016  * I'm not sure about consistency between poll(2) and kqueue(2) but
   7017  * anyway I follow it.
   7018  * XXX Omit checking each openmode
   7019  */
   7020 DEF(audioctl_kqueue)
   7021 {
   7022 	struct kevent kev;
   7023 	int fd;
   7024 	int kq;
   7025 	int r;
   7026 
   7027 	TEST("audioctl_kqueue");
   7028 
   7029 	fd = OPEN(devaudioctl, O_WRONLY);
   7030 	REQUIRED_SYS_OK(fd);
   7031 
   7032 	kq = KQUEUE();
   7033 	XP_SYS_OK(kq);
   7034 
   7035 	EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, 0);
   7036 	r = KEVENT_SET(kq, &kev, 1);
   7037 	/*
   7038 	 * NetBSD7 has a bug.  It looks to wanted to treat it as successful
   7039 	 * but returned 1(== EPERM).
   7040 	 * On NetBSD9, I decided to return ENODEV.
   7041 	 */
   7042 	if (netbsd < 8) {
   7043 		XP_SYS_NG(1/*EPERM*/, r);
   7044 	} else {
   7045 		XP_SYS_NG(ENODEV, r);
   7046 	}
   7047 
   7048 	r = CLOSE(fd);
   7049 	XP_SYS_EQ(0, r);
   7050 }
   7051 
   7052 
   7053 /*
   7054  * This table is processed by t_audio.awk!
   7055  * Keep /^\tENT(testname),/ format in order to add to atf.
   7056  */
   7057 #define ENT(x) { #x, test__ ## x }
   7058 struct testentry testtable[] = {
   7059 	ENT(open_mode_RDONLY),
   7060 	ENT(open_mode_WRONLY),
   7061 	ENT(open_mode_RDWR),
   7062 	ENT(open_audio_RDONLY),
   7063 	ENT(open_audio_WRONLY),
   7064 	ENT(open_audio_RDWR),
   7065 	ENT(open_sound_RDONLY),
   7066 	ENT(open_sound_WRONLY),
   7067 	ENT(open_sound_RDWR),
   7068 	ENT(open_audioctl_RDONLY),
   7069 	ENT(open_audioctl_WRONLY),
   7070 	ENT(open_audioctl_RDWR),
   7071 	ENT(open_sound_sticky),
   7072 	ENT(open_audioctl_sticky),
   7073 	ENT(open_simul_RDONLY_RDONLY),
   7074 	ENT(open_simul_RDONLY_WRONLY),
   7075 	ENT(open_simul_RDONLY_RDWR),
   7076 	ENT(open_simul_WRONLY_RDONLY),
   7077 	ENT(open_simul_WRONLY_WRONLY),
   7078 	ENT(open_simul_WRONLY_RDWR),
   7079 	ENT(open_simul_RDWR_RDONLY),
   7080 	ENT(open_simul_RDWR_WRONLY),
   7081 	ENT(open_simul_RDWR_RDWR),
   7082 /**/	ENT(open_multiuser_0),		// XXX TODO sysctl
   7083 /**/	ENT(open_multiuser_1),		// XXX TODO sysctl
   7084 	ENT(write_PLAY_ALL),
   7085 	ENT(write_PLAY),
   7086 	ENT(read),
   7087 	ENT(rept_write),
   7088 	ENT(rept_read),
   7089 	ENT(rdwr_fallback_RDONLY),
   7090 	ENT(rdwr_fallback_WRONLY),
   7091 	ENT(rdwr_fallback_RDWR),
   7092 	ENT(rdwr_two_RDONLY_RDONLY),
   7093 	ENT(rdwr_two_RDONLY_WRONLY),
   7094 	ENT(rdwr_two_RDONLY_RDWR),
   7095 	ENT(rdwr_two_WRONLY_RDONLY),
   7096 	ENT(rdwr_two_WRONLY_WRONLY),
   7097 	ENT(rdwr_two_WRONLY_RDWR),
   7098 	ENT(rdwr_two_RDWR_RDONLY),
   7099 	ENT(rdwr_two_RDWR_WRONLY),
   7100 	ENT(rdwr_two_RDWR_RDWR),
   7101 	ENT(rdwr_simul),
   7102 	ENT(drain_incomplete),
   7103 	ENT(drain_pause),
   7104 	ENT(drain_onrec),
   7105 /**/	ENT(mmap_mode_RDONLY_NONE),	// XXX rump doesn't supprot mmap
   7106 /**/	ENT(mmap_mode_RDONLY_READ),	// XXX rump doesn't supprot mmap
   7107 /**/	ENT(mmap_mode_RDONLY_WRITE),	// XXX rump doesn't supprot mmap
   7108 /**/	ENT(mmap_mode_RDONLY_READWRITE),// XXX rump doesn't supprot mmap
   7109 /**/	ENT(mmap_mode_WRONLY_NONE),	// XXX rump doesn't supprot mmap
   7110 /**/	ENT(mmap_mode_WRONLY_READ),	// XXX rump doesn't supprot mmap
   7111 /**/	ENT(mmap_mode_WRONLY_WRITE),	// XXX rump doesn't supprot mmap
   7112 /**/	ENT(mmap_mode_WRONLY_READWRITE),// XXX rump doesn't supprot mmap
   7113 /**/	ENT(mmap_mode_RDWR_NONE),	// XXX rump doesn't supprot mmap
   7114 /**/	ENT(mmap_mode_RDWR_READ),	// XXX rump doesn't supprot mmap
   7115 /**/	ENT(mmap_mode_RDWR_WRITE),	// XXX rump doesn't supprot mmap
   7116 /**/	ENT(mmap_mode_RDWR_READWRITE),	// XXX rump doesn't supprot mmap
   7117 /**/	ENT(mmap_len),			// XXX rump doesn't supprot mmap
   7118 /**/	ENT(mmap_twice),		// XXX rump doesn't supprot mmap
   7119 /**/	ENT(mmap_multi),		// XXX rump doesn't supprot mmap
   7120 	ENT(poll_mode_RDONLY_IN),
   7121 	ENT(poll_mode_RDONLY_OUT),
   7122 	ENT(poll_mode_RDONLY_INOUT),
   7123 	ENT(poll_mode_WRONLY_IN),
   7124 	ENT(poll_mode_WRONLY_OUT),
   7125 	ENT(poll_mode_WRONLY_INOUT),
   7126 	ENT(poll_mode_RDWR_IN),
   7127 	ENT(poll_mode_RDWR_OUT),
   7128 	ENT(poll_mode_RDWR_INOUT),
   7129 	ENT(poll_out_empty),
   7130 	ENT(poll_out_full),
   7131 	ENT(poll_out_hiwat),
   7132 /**/	ENT(poll_out_unpause),		// XXX does not seem to work on rump
   7133 /**/	ENT(poll_out_simul),		// XXX does not seem to work on rump
   7134 	ENT(poll_in_open_audio),
   7135 	ENT(poll_in_open_sound),
   7136 	ENT(poll_in_open_audioctl),
   7137 	ENT(poll_in_simul),
   7138 	ENT(kqueue_mode_RDONLY_READ),
   7139 	ENT(kqueue_mode_RDONLY_WRITE),
   7140 	ENT(kqueue_mode_WRONLY_READ),
   7141 	ENT(kqueue_mode_WRONLY_WRITE),
   7142 	ENT(kqueue_mode_RDWR_READ),
   7143 	ENT(kqueue_mode_RDWR_WRITE),
   7144 	ENT(kqueue_empty),
   7145 	ENT(kqueue_full),
   7146 	ENT(kqueue_hiwat),
   7147 /**/	ENT(kqueue_unpause),		// XXX does not seem to work on rump
   7148 /**/	ENT(kqueue_simul),		// XXX does not seem to work on rump
   7149 	ENT(ioctl_while_write),
   7150 	ENT(FIOASYNC_reset),
   7151 	ENT(FIOASYNC_play_signal),
   7152 	ENT(FIOASYNC_rec_signal),
   7153 /**/	ENT(FIOASYNC_multi),		// XXX does not seem to work on rump
   7154 	ENT(AUDIO_WSEEK),
   7155 	ENT(AUDIO_SETFD_RDONLY),
   7156 	ENT(AUDIO_SETFD_WRONLY),
   7157 	ENT(AUDIO_SETFD_RDWR),
   7158 	ENT(AUDIO_GETINFO_eof),
   7159 	ENT(AUDIO_SETINFO_mode_RDONLY_0),
   7160 	ENT(AUDIO_SETINFO_mode_RDONLY_1),
   7161 	ENT(AUDIO_SETINFO_mode_RDONLY_2),
   7162 	ENT(AUDIO_SETINFO_mode_RDONLY_3),
   7163 	ENT(AUDIO_SETINFO_mode_RDONLY_4),
   7164 	ENT(AUDIO_SETINFO_mode_RDONLY_5),
   7165 	ENT(AUDIO_SETINFO_mode_RDONLY_6),
   7166 	ENT(AUDIO_SETINFO_mode_RDONLY_7),
   7167 	ENT(AUDIO_SETINFO_mode_RDONLY_8),
   7168 	ENT(AUDIO_SETINFO_mode_WRONLY_0),
   7169 	ENT(AUDIO_SETINFO_mode_WRONLY_1),
   7170 	ENT(AUDIO_SETINFO_mode_WRONLY_2),
   7171 	ENT(AUDIO_SETINFO_mode_WRONLY_3),
   7172 	ENT(AUDIO_SETINFO_mode_WRONLY_4),
   7173 	ENT(AUDIO_SETINFO_mode_WRONLY_5),
   7174 	ENT(AUDIO_SETINFO_mode_WRONLY_6),
   7175 	ENT(AUDIO_SETINFO_mode_WRONLY_7),
   7176 	ENT(AUDIO_SETINFO_mode_WRONLY_8),
   7177 	ENT(AUDIO_SETINFO_mode_RDWR_0),
   7178 	ENT(AUDIO_SETINFO_mode_RDWR_1),
   7179 	ENT(AUDIO_SETINFO_mode_RDWR_2),
   7180 	ENT(AUDIO_SETINFO_mode_RDWR_3),
   7181 	ENT(AUDIO_SETINFO_mode_RDWR_4),
   7182 	ENT(AUDIO_SETINFO_mode_RDWR_5),
   7183 	ENT(AUDIO_SETINFO_mode_RDWR_6),
   7184 	ENT(AUDIO_SETINFO_mode_RDWR_7),
   7185 	ENT(AUDIO_SETINFO_mode_RDWR_8),
   7186 	ENT(AUDIO_SETINFO_params_set_RDONLY_0),
   7187 	ENT(AUDIO_SETINFO_params_set_RDONLY_1),
   7188 	ENT(AUDIO_SETINFO_params_set_WRONLY_0),
   7189 	ENT(AUDIO_SETINFO_params_set_WRONLY_1),
   7190 	ENT(AUDIO_SETINFO_params_set_WRONLY_2),
   7191 	ENT(AUDIO_SETINFO_params_set_WRONLY_3),
   7192 	ENT(AUDIO_SETINFO_params_set_RDWR_0),
   7193 	ENT(AUDIO_SETINFO_params_set_RDWR_1),
   7194 	ENT(AUDIO_SETINFO_params_set_RDWR_2),
   7195 	ENT(AUDIO_SETINFO_params_set_RDWR_3),
   7196 	ENT(AUDIO_SETINFO_params_simul),
   7197 	ENT(AUDIO_SETINFO_channels),
   7198 	ENT(AUDIO_SETINFO_sample_rate),
   7199 	ENT(AUDIO_SETINFO_sample_rate_0),
   7200 	ENT(AUDIO_SETINFO_pause_RDONLY_0),
   7201 	ENT(AUDIO_SETINFO_pause_RDONLY_1),
   7202 	ENT(AUDIO_SETINFO_pause_WRONLY_0),
   7203 	ENT(AUDIO_SETINFO_pause_WRONLY_1),
   7204 	ENT(AUDIO_SETINFO_pause_WRONLY_2),
   7205 	ENT(AUDIO_SETINFO_pause_WRONLY_3),
   7206 	ENT(AUDIO_SETINFO_pause_RDWR_0),
   7207 	ENT(AUDIO_SETINFO_pause_RDWR_1),
   7208 	ENT(AUDIO_SETINFO_pause_RDWR_2),
   7209 	ENT(AUDIO_SETINFO_pause_RDWR_3),
   7210 	ENT(AUDIO_SETINFO_gain),
   7211 	ENT(AUDIO_SETINFO_gain_balance),
   7212 	ENT(AUDIO_GETENC_range),
   7213 	ENT(AUDIO_GETENC_error),
   7214 	ENT(AUDIO_ERROR_RDONLY),
   7215 	ENT(AUDIO_ERROR_WRONLY),
   7216 	ENT(AUDIO_ERROR_RDWR),
   7217 	ENT(AUDIO_GETIOFFS_one_RDONLY),
   7218 	ENT(AUDIO_GETIOFFS_one_WRONLY),
   7219 	ENT(AUDIO_GETIOFFS_one_RDWR),
   7220 	ENT(AUDIO_GETOOFFS_one_RDONLY),
   7221 	ENT(AUDIO_GETOOFFS_one_WRONLY),
   7222 	ENT(AUDIO_GETOOFFS_one_RDWR),
   7223 	ENT(AUDIO_GETOOFFS_wrap_RDONLY),
   7224 	ENT(AUDIO_GETOOFFS_wrap_WRONLY),
   7225 	ENT(AUDIO_GETOOFFS_wrap_RDWR),
   7226 	ENT(AUDIO_GETOOFFS_flush_RDONLY),
   7227 	ENT(AUDIO_GETOOFFS_flush_WRONLY),
   7228 	ENT(AUDIO_GETOOFFS_flush_RDWR),
   7229 	ENT(AUDIO_GETOOFFS_set_RDONLY),
   7230 	ENT(AUDIO_GETOOFFS_set_WRONLY),
   7231 	ENT(AUDIO_GETOOFFS_set_RDWR),
   7232 	ENT(audioctl_open_1_RDONLY_RDONLY),
   7233 	ENT(audioctl_open_1_RDONLY_RWONLY),
   7234 	ENT(audioctl_open_1_RDONLY_RDWR),
   7235 	ENT(audioctl_open_1_WRONLY_RDONLY),
   7236 	ENT(audioctl_open_1_WRONLY_RWONLY),
   7237 	ENT(audioctl_open_1_WRONLY_RDWR),
   7238 	ENT(audioctl_open_1_RDWR_RDONLY),
   7239 	ENT(audioctl_open_1_RDWR_RWONLY),
   7240 	ENT(audioctl_open_1_RDWR_RDWR),
   7241 	ENT(audioctl_open_2_RDONLY_RDONLY),
   7242 	ENT(audioctl_open_2_RDONLY_RWONLY),
   7243 	ENT(audioctl_open_2_RDONLY_RDWR),
   7244 	ENT(audioctl_open_2_WRONLY_RDONLY),
   7245 	ENT(audioctl_open_2_WRONLY_RWONLY),
   7246 	ENT(audioctl_open_2_WRONLY_RDWR),
   7247 	ENT(audioctl_open_2_RDWR_RDONLY),
   7248 	ENT(audioctl_open_2_RDWR_RWONLY),
   7249 	ENT(audioctl_open_2_RDWR_RDWR),
   7250 	ENT(audioctl_open_simul),
   7251 /**/	ENT(audioctl_open_multiuser0_audio1),	// XXX TODO sysctl
   7252 /**/	ENT(audioctl_open_multiuser1_audio1),	// XXX TODO sysctl
   7253 /**/	ENT(audioctl_open_multiuser0_audio2),	// XXX TODO sysctl
   7254 /**/	ENT(audioctl_open_multiuser1_audio2),	// XXX TODO sysctl
   7255 /**/	ENT(audioctl_open_multiuser0_audioctl),	// XXX TODO sysctl
   7256 /**/	ENT(audioctl_open_multiuser1_audioctl),	// XXX TODO sysctl
   7257 	ENT(audioctl_rw_RDONLY),
   7258 	ENT(audioctl_rw_WRONLY),
   7259 	ENT(audioctl_rw_RDWR),
   7260 	ENT(audioctl_poll),
   7261 	ENT(audioctl_kqueue),
   7262 	{.name = NULL},
   7263 };
   7264