Home | History | Annotate | Line # | Download | only in audio
audio.c revision 1.142
      1 /*	$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1991-1993 Regents of the University of California.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the Computer Systems
     47  *	Engineering Group at Lawrence Berkeley Laboratory.
     48  * 4. Neither the name of the University nor of the Laboratory may be used
     49  *    to endorse or promote products derived from this software without
     50  *    specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * Terminology: "sample", "channel", "frame", "block", "track":
     67  *
     68  *  channel       frame
     69  *   |           ........
     70  *   v           :      :                                    \
     71  *        +------:------:------:-  -+------+ : +------+-..   |
     72  *  #0(L) |sample|sample|sample| .. |sample| : |sample|      |
     73  *        +------:------:------:-  -+------+ : +------+-..   |
     74  *  #1(R) |sample|sample|sample| .. |sample| : |sample|      |
     75  *        +------:------:------:-  -+------+ : +------+-..   | track
     76  *   :           :      :                    :               |
     77  *        +------:------:------:-  -+------+ : +------+-..   |
     78  *        |sample|sample|sample| .. |sample| : |sample|      |
     79  *        +------:------:------:-  -+------+ : +------+-..   |
     80  *               :      :                                    /
     81  *               ........
     82  *
     83  *        \--------------------------------/   \--------..
     84  *                     block
     85  *
     86  * - A "frame" is the minimum unit in the time axis direction, and consists
     87  *   of samples for the number of channels.
     88  * - A "block" is basic length of processing.  The audio layer basically
     89  *   handles audio data stream block by block, asks underlying hardware to
     90  *   process them block by block, and then the hardware raises interrupt by
     91  *   each block.
     92  * - A "track" is single completed audio stream.
     93  *
     94  * For example, the hardware block is assumed to be 10 msec, and your audio
     95  * track consists of 2.1(=3) channels 44.1kHz 16bit PCM,
     96  *
     97  * "channel" = 3
     98  * "sample" = 2 [bytes]
     99  * "frame" = 2 [bytes/sample] * 3 [channels] = 6 [bytes]
    100  * "block" = 44100 [Hz] * (10/1000) [seconds] * 6 [bytes/frame] = 2646 [bytes]
    101  *
    102  * The terminologies shown here are only for this MI audio layer.  Note that
    103  * different terminologies may be used in each manufacturer's datasheet, and
    104  * each MD driver may follow it.  For example, what we call a "block" is
    105  * called a "frame" in sys/dev/pci/yds.c.
    106  */
    107 
    108 /*
    109  * Locking: there are three locks per device.
    110  *
    111  * - sc_lock, provided by the underlying driver.  This is an adaptive lock,
    112  *   returned in the second parameter to hw_if->get_locks().  It is known
    113  *   as the "thread lock".
    114  *
    115  *   It serializes access to state in all places except the
    116  *   driver's interrupt service routine.  This lock is taken from process
    117  *   context (example: access to /dev/audio).  It is also taken from soft
    118  *   interrupt handlers in this module, primarily to serialize delivery of
    119  *   wakeups.  This lock may be used/provided by modules external to the
    120  *   audio subsystem, so take care not to introduce a lock order problem.
    121  *   LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
    122  *
    123  * - sc_intr_lock, provided by the underlying driver.  This may be either a
    124  *   spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
    125  *   IPL_SOFT*), returned in the first parameter to hw_if->get_locks().  It
    126  *   is known as the "interrupt lock".
    127  *
    128  *   It provides atomic access to the device's hardware state, and to audio
    129  *   channel data that may be accessed by the hardware driver's ISR.
    130  *   In all places outside the ISR, sc_lock must be held before taking
    131  *   sc_intr_lock.  This is to ensure that groups of hardware operations are
    132  *   made atomically.  SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
    133  *
    134  * - sc_exlock, private to this module.  This is a variable protected by
    135  *   sc_lock.  It is known as the "critical section".
    136  *   Some operations release sc_lock in order to allocate memory, to wait
    137  *   for in-flight I/O to complete, to copy to/from user context, etc.
    138  *   sc_exlock provides a critical section even under the circumstance.
    139  *   "+" in following list indicates the interfaces which necessary to be
    140  *   protected by sc_exlock.
    141  *
    142  * List of hardware interface methods, and which locks are held when each
    143  * is called by this module:
    144  *
    145  *	METHOD			INTR	THREAD  NOTES
    146  *	----------------------- ------- -------	-------------------------
    147  *	open 			x	x +
    148  *	close 			x	x +
    149  *	query_format		-	x
    150  *	set_format		-	x
    151  *	round_blocksize		-	x
    152  *	commit_settings		-	x
    153  *	init_output 		x	x
    154  *	init_input 		x	x
    155  *	start_output 		x	x +
    156  *	start_input 		x	x +
    157  *	halt_output 		x	x +
    158  *	halt_input 		x	x +
    159  *	speaker_ctl 		x	x
    160  *	getdev 			-	-
    161  *	set_port 		-	x +
    162  *	get_port 		-	x +
    163  *	query_devinfo 		-	x
    164  *	allocm 			-	- +
    165  *	freem 			-	- +
    166  *	round_buffersize 	-	x
    167  *	get_props 		-	-	Called at attach time
    168  *	trigger_output 		x	x +
    169  *	trigger_input 		x	x +
    170  *	dev_ioctl 		-	x
    171  *	get_locks 		-	-	Called at attach time
    172  *
    173  * In addition, there is an additional lock.
    174  *
    175  * - track->lock.  This is an atomic variable and is similar to the
    176  *   "interrupt lock".  This is one for each track.  If any thread context
    177  *   (and software interrupt context) and hardware interrupt context who
    178  *   want to access some variables on this track, they must acquire this
    179  *   lock before.  It protects track's consistency between hardware
    180  *   interrupt context and others.
    181  */
    182 
    183 #include <sys/cdefs.h>
    184 __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $");
    185 
    186 #ifdef _KERNEL_OPT
    187 #include "audio.h"
    188 #include "midi.h"
    189 #endif
    190 
    191 #if NAUDIO > 0
    192 
    193 #include <sys/types.h>
    194 #include <sys/param.h>
    195 #include <sys/atomic.h>
    196 #include <sys/audioio.h>
    197 #include <sys/conf.h>
    198 #include <sys/cpu.h>
    199 #include <sys/device.h>
    200 #include <sys/fcntl.h>
    201 #include <sys/file.h>
    202 #include <sys/filedesc.h>
    203 #include <sys/intr.h>
    204 #include <sys/ioctl.h>
    205 #include <sys/kauth.h>
    206 #include <sys/kernel.h>
    207 #include <sys/kmem.h>
    208 #include <sys/lock.h>
    209 #include <sys/malloc.h>
    210 #include <sys/mman.h>
    211 #include <sys/module.h>
    212 #include <sys/poll.h>
    213 #include <sys/proc.h>
    214 #include <sys/queue.h>
    215 #include <sys/select.h>
    216 #include <sys/signalvar.h>
    217 #include <sys/stat.h>
    218 #include <sys/sysctl.h>
    219 #include <sys/systm.h>
    220 #include <sys/syslog.h>
    221 #include <sys/vnode.h>
    222 
    223 #include <dev/audio/audio_if.h>
    224 #include <dev/audio/audiovar.h>
    225 #include <dev/audio/audiodef.h>
    226 #include <dev/audio/linear.h>
    227 #include <dev/audio/mulaw.h>
    228 
    229 #include <machine/endian.h>
    230 
    231 #include <uvm/uvm_extern.h>
    232 
    233 #include "ioconf.h"
    234 
    235 /*
    236  * 0: No debug logs
    237  * 1: action changes like open/close/set_format/mmap...
    238  * 2: + normal operations like read/write/ioctl...
    239  * 3: + TRACEs except interrupt
    240  * 4: + TRACEs including interrupt
    241  */
    242 //#define AUDIO_DEBUG 1
    243 
    244 #if defined(AUDIO_DEBUG)
    245 
    246 int audiodebug = AUDIO_DEBUG;
    247 static void audio_vtrace(struct audio_softc *sc, const char *, const char *,
    248 	const char *, va_list);
    249 static void audio_trace(struct audio_softc *sc, const char *, const char *, ...)
    250 	__printflike(3, 4);
    251 static void audio_tracet(const char *, audio_track_t *, const char *, ...)
    252 	__printflike(3, 4);
    253 static void audio_tracef(const char *, audio_file_t *, const char *, ...)
    254 	__printflike(3, 4);
    255 
    256 /* XXX sloppy memory logger */
    257 static void audio_mlog_init(void);
    258 static void audio_mlog_free(void);
    259 static void audio_mlog_softintr(void *);
    260 extern void audio_mlog_flush(void);
    261 extern void audio_mlog_printf(const char *, ...);
    262 
    263 static int mlog_refs;		/* reference counter */
    264 static char *mlog_buf[2];	/* double buffer */
    265 static int mlog_buflen;		/* buffer length */
    266 static int mlog_used;		/* used length */
    267 static int mlog_full;		/* number of dropped lines by buffer full */
    268 static int mlog_drop;		/* number of dropped lines by busy */
    269 static volatile uint32_t mlog_inuse;	/* in-use */
    270 static int mlog_wpage;		/* active page */
    271 static void *mlog_sih;		/* softint handle */
    272 
    273 static void
    274 audio_mlog_init(void)
    275 {
    276 	mlog_refs++;
    277 	if (mlog_refs > 1)
    278 		return;
    279 	mlog_buflen = 4096;
    280 	mlog_buf[0] = kmem_zalloc(mlog_buflen, KM_SLEEP);
    281 	mlog_buf[1] = kmem_zalloc(mlog_buflen, KM_SLEEP);
    282 	mlog_used = 0;
    283 	mlog_full = 0;
    284 	mlog_drop = 0;
    285 	mlog_inuse = 0;
    286 	mlog_wpage = 0;
    287 	mlog_sih = softint_establish(SOFTINT_SERIAL, audio_mlog_softintr, NULL);
    288 	if (mlog_sih == NULL)
    289 		printf("%s: softint_establish failed\n", __func__);
    290 }
    291 
    292 static void
    293 audio_mlog_free(void)
    294 {
    295 	mlog_refs--;
    296 	if (mlog_refs > 0)
    297 		return;
    298 
    299 	audio_mlog_flush();
    300 	if (mlog_sih)
    301 		softint_disestablish(mlog_sih);
    302 	kmem_free(mlog_buf[0], mlog_buflen);
    303 	kmem_free(mlog_buf[1], mlog_buflen);
    304 }
    305 
    306 /*
    307  * Flush memory buffer.
    308  * It must not be called from hardware interrupt context.
    309  */
    310 void
    311 audio_mlog_flush(void)
    312 {
    313 	if (mlog_refs == 0)
    314 		return;
    315 
    316 	/* Nothing to do if already in use ? */
    317 	if (atomic_swap_32(&mlog_inuse, 1) == 1)
    318 		return;
    319 	membar_acquire();
    320 
    321 	int rpage = mlog_wpage;
    322 	mlog_wpage ^= 1;
    323 	mlog_buf[mlog_wpage][0] = '\0';
    324 	mlog_used = 0;
    325 
    326 	atomic_store_release(&mlog_inuse, 0);
    327 
    328 	if (mlog_buf[rpage][0] != '\0') {
    329 		printf("%s", mlog_buf[rpage]);
    330 		if (mlog_drop > 0)
    331 			printf("mlog_drop %d\n", mlog_drop);
    332 		if (mlog_full > 0)
    333 			printf("mlog_full %d\n", mlog_full);
    334 	}
    335 	mlog_full = 0;
    336 	mlog_drop = 0;
    337 }
    338 
    339 static void
    340 audio_mlog_softintr(void *cookie)
    341 {
    342 	audio_mlog_flush();
    343 }
    344 
    345 void
    346 audio_mlog_printf(const char *fmt, ...)
    347 {
    348 	int len;
    349 	va_list ap;
    350 
    351 	if (atomic_swap_32(&mlog_inuse, 1) == 1) {
    352 		/* already inuse */
    353 		mlog_drop++;
    354 		return;
    355 	}
    356 	membar_acquire();
    357 
    358 	va_start(ap, fmt);
    359 	len = vsnprintf(
    360 	    mlog_buf[mlog_wpage] + mlog_used,
    361 	    mlog_buflen - mlog_used,
    362 	    fmt, ap);
    363 	va_end(ap);
    364 
    365 	mlog_used += len;
    366 	if (mlog_buflen - mlog_used <= 1) {
    367 		mlog_full++;
    368 	}
    369 
    370 	atomic_store_release(&mlog_inuse, 0);
    371 
    372 	if (mlog_sih)
    373 		softint_schedule(mlog_sih);
    374 }
    375 
    376 /* trace functions */
    377 static void
    378 audio_vtrace(struct audio_softc *sc, const char *funcname, const char *header,
    379 	const char *fmt, va_list ap)
    380 {
    381 	char buf[256];
    382 	int n;
    383 
    384 	n = 0;
    385 	buf[0] = '\0';
    386 	n += snprintf(buf + n, sizeof(buf) - n, "%s@%d %s",
    387 	    funcname, device_unit(sc->sc_dev), header);
    388 	n += vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
    389 
    390 	if (cpu_intr_p()) {
    391 		audio_mlog_printf("%s\n", buf);
    392 	} else {
    393 		audio_mlog_flush();
    394 		printf("%s\n", buf);
    395 	}
    396 }
    397 
    398 static void
    399 audio_trace(struct audio_softc *sc, const char *funcname, const char *fmt, ...)
    400 {
    401 	va_list ap;
    402 
    403 	va_start(ap, fmt);
    404 	audio_vtrace(sc, funcname, "", fmt, ap);
    405 	va_end(ap);
    406 }
    407 
    408 static void
    409 audio_tracet(const char *funcname, audio_track_t *track, const char *fmt, ...)
    410 {
    411 	char hdr[16];
    412 	va_list ap;
    413 
    414 	snprintf(hdr, sizeof(hdr), "#%d ", track->id);
    415 	va_start(ap, fmt);
    416 	audio_vtrace(track->mixer->sc, funcname, hdr, fmt, ap);
    417 	va_end(ap);
    418 }
    419 
    420 static void
    421 audio_tracef(const char *funcname, audio_file_t *file, const char *fmt, ...)
    422 {
    423 	char hdr[32];
    424 	char phdr[16], rhdr[16];
    425 	va_list ap;
    426 
    427 	phdr[0] = '\0';
    428 	rhdr[0] = '\0';
    429 	if (file->ptrack)
    430 		snprintf(phdr, sizeof(phdr), "#%d", file->ptrack->id);
    431 	if (file->rtrack)
    432 		snprintf(rhdr, sizeof(rhdr), "#%d", file->rtrack->id);
    433 	snprintf(hdr, sizeof(hdr), "{%s,%s} ", phdr, rhdr);
    434 
    435 	va_start(ap, fmt);
    436 	audio_vtrace(file->sc, funcname, hdr, fmt, ap);
    437 	va_end(ap);
    438 }
    439 
    440 #define DPRINTF(n, fmt...)	do {	\
    441 	if (audiodebug >= (n)) {	\
    442 		audio_mlog_flush();	\
    443 		printf(fmt);		\
    444 	}				\
    445 } while (0)
    446 #define TRACE(n, fmt...)	do { \
    447 	if (audiodebug >= (n)) audio_trace(sc, __func__, fmt); \
    448 } while (0)
    449 #define TRACET(n, t, fmt...)	do { \
    450 	if (audiodebug >= (n)) audio_tracet(__func__, t, fmt); \
    451 } while (0)
    452 #define TRACEF(n, f, fmt...)	do { \
    453 	if (audiodebug >= (n)) audio_tracef(__func__, f, fmt); \
    454 } while (0)
    455 
    456 struct audio_track_debugbuf {
    457 	char usrbuf[32];
    458 	char codec[32];
    459 	char chvol[32];
    460 	char chmix[32];
    461 	char freq[32];
    462 	char outbuf[32];
    463 };
    464 
    465 static void
    466 audio_track_bufstat(audio_track_t *track, struct audio_track_debugbuf *buf)
    467 {
    468 
    469 	memset(buf, 0, sizeof(*buf));
    470 
    471 	snprintf(buf->outbuf, sizeof(buf->outbuf), " out=%d/%d/%d",
    472 	    track->outbuf.head, track->outbuf.used, track->outbuf.capacity);
    473 	if (track->freq.filter)
    474 		snprintf(buf->freq, sizeof(buf->freq), " f=%d/%d/%d",
    475 		    track->freq.srcbuf.head,
    476 		    track->freq.srcbuf.used,
    477 		    track->freq.srcbuf.capacity);
    478 	if (track->chmix.filter)
    479 		snprintf(buf->chmix, sizeof(buf->chmix), " m=%d",
    480 		    track->chmix.srcbuf.used);
    481 	if (track->chvol.filter)
    482 		snprintf(buf->chvol, sizeof(buf->chvol), " v=%d",
    483 		    track->chvol.srcbuf.used);
    484 	if (track->codec.filter)
    485 		snprintf(buf->codec, sizeof(buf->codec), " e=%d",
    486 		    track->codec.srcbuf.used);
    487 	snprintf(buf->usrbuf, sizeof(buf->usrbuf), " usr=%d/%d/H%d",
    488 	    track->usrbuf.head, track->usrbuf.used, track->usrbuf_usedhigh);
    489 }
    490 #else
    491 #define DPRINTF(n, fmt...)	do { } while (0)
    492 #define TRACE(n, fmt, ...)	do { } while (0)
    493 #define TRACET(n, t, fmt, ...)	do { } while (0)
    494 #define TRACEF(n, f, fmt, ...)	do { } while (0)
    495 #endif
    496 
    497 #define SPECIFIED(x)	((x) != ~0)
    498 #define SPECIFIED_CH(x)	((x) != (u_char)~0)
    499 
    500 /*
    501  * Default hardware blocksize in msec.
    502  *
    503  * We use 10 msec for most modern platforms.  This period is good enough to
    504  * play audio and video synchronizely.
    505  * In contrast, for very old platforms, this is usually too short and too
    506  * severe.  Also such platforms usually can not play video confortably, so
    507  * it's not so important to make the blocksize shorter.  If the platform
    508  * defines its own value as __AUDIO_BLK_MS in its <machine/param.h>, it
    509  * uses this instead.
    510  *
    511  * In either case, you can overwrite AUDIO_BLK_MS by your kernel
    512  * configuration file if you wish.
    513  */
    514 #if !defined(AUDIO_BLK_MS)
    515 # if defined(__AUDIO_BLK_MS)
    516 #  define AUDIO_BLK_MS __AUDIO_BLK_MS
    517 # else
    518 #  define AUDIO_BLK_MS (10)
    519 # endif
    520 #endif
    521 
    522 /* Device timeout in msec */
    523 #define AUDIO_TIMEOUT	(3000)
    524 
    525 /* #define AUDIO_PM_IDLE */
    526 #ifdef AUDIO_PM_IDLE
    527 int audio_idle_timeout = 30;
    528 #endif
    529 
    530 /* Number of elements of async mixer's pid */
    531 #define AM_CAPACITY	(4)
    532 
    533 struct portname {
    534 	const char *name;
    535 	int mask;
    536 };
    537 
    538 static int audiomatch(device_t, cfdata_t, void *);
    539 static void audioattach(device_t, device_t, void *);
    540 static int audiodetach(device_t, int);
    541 static int audioactivate(device_t, enum devact);
    542 static void audiochilddet(device_t, device_t);
    543 static int audiorescan(device_t, const char *, const int *);
    544 
    545 static int audio_modcmd(modcmd_t, void *);
    546 
    547 #ifdef AUDIO_PM_IDLE
    548 static void audio_idle(void *);
    549 static void audio_activity(device_t, devactive_t);
    550 #endif
    551 
    552 static bool audio_suspend(device_t dv, const pmf_qual_t *);
    553 static bool audio_resume(device_t dv, const pmf_qual_t *);
    554 static void audio_volume_down(device_t);
    555 static void audio_volume_up(device_t);
    556 static void audio_volume_toggle(device_t);
    557 
    558 static void audio_mixer_capture(struct audio_softc *);
    559 static void audio_mixer_restore(struct audio_softc *);
    560 
    561 static void audio_softintr_rd(void *);
    562 static void audio_softintr_wr(void *);
    563 
    564 static int audio_properties(struct audio_softc *);
    565 static void audio_printf(struct audio_softc *, const char *, ...)
    566 	__printflike(2, 3);
    567 static int audio_exlock_mutex_enter(struct audio_softc *);
    568 static void audio_exlock_mutex_exit(struct audio_softc *);
    569 static int audio_exlock_enter(struct audio_softc *);
    570 static void audio_exlock_exit(struct audio_softc *);
    571 static struct audio_softc *audio_sc_acquire_fromfile(audio_file_t *,
    572 	struct psref *);
    573 static void audio_sc_release(struct audio_softc *, struct psref *);
    574 static int audio_track_waitio(struct audio_softc *, audio_track_t *,
    575 	const char *mess);
    576 
    577 static int audioclose(struct file *);
    578 static int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    579 static int audiowrite(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    580 static int audioioctl(struct file *, u_long, void *);
    581 static int audiopoll(struct file *, int);
    582 static int audiokqfilter(struct file *, struct knote *);
    583 static int audiommap(struct file *, off_t *, size_t, int, int *, int *,
    584 	struct uvm_object **, int *);
    585 static int audiostat(struct file *, struct stat *);
    586 
    587 static void filt_audiowrite_detach(struct knote *);
    588 static int  filt_audiowrite_event(struct knote *, long);
    589 static void filt_audioread_detach(struct knote *);
    590 static int  filt_audioread_event(struct knote *, long);
    591 
    592 static int audio_open(dev_t, struct audio_softc *, int, int, struct lwp *,
    593 	audio_file_t **);
    594 static int audio_close(struct audio_softc *, audio_file_t *);
    595 static void audio_unlink(struct audio_softc *, audio_file_t *);
    596 static int audio_read(struct audio_softc *, struct uio *, int, audio_file_t *);
    597 static int audio_write(struct audio_softc *, struct uio *, int, audio_file_t *);
    598 static void audio_file_clear(struct audio_softc *, audio_file_t *);
    599 static int audio_ioctl(dev_t, struct audio_softc *, u_long, void *, int,
    600 	struct lwp *, audio_file_t *);
    601 static int audio_poll(struct audio_softc *, int, struct lwp *, audio_file_t *);
    602 static int audio_kqfilter(struct audio_softc *, audio_file_t *, struct knote *);
    603 static int audio_mmap(struct audio_softc *, off_t *, size_t, int, int *, int *,
    604 	struct uvm_object **, int *, audio_file_t *);
    605 
    606 static int audioctl_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    607 
    608 static void audio_pintr(void *);
    609 static void audio_rintr(void *);
    610 
    611 static int audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
    612 
    613 static int audio_track_inputblk_as_usrbyte(const audio_track_t *, int);
    614 static int audio_track_readablebytes(const audio_track_t *);
    615 static int audio_file_setinfo(struct audio_softc *, audio_file_t *,
    616 	const struct audio_info *);
    617 static int audio_track_setinfo_check(audio_track_t *,
    618 	audio_format2_t *, const struct audio_prinfo *);
    619 static void audio_track_setinfo_water(audio_track_t *,
    620 	const struct audio_info *);
    621 static int audio_hw_setinfo(struct audio_softc *, const struct audio_info *,
    622 	struct audio_info *);
    623 static int audio_hw_set_format(struct audio_softc *, int,
    624 	const audio_format2_t *, const audio_format2_t *,
    625 	audio_filter_reg_t *, audio_filter_reg_t *);
    626 static int audiogetinfo(struct audio_softc *, struct audio_info *, int,
    627 	audio_file_t *);
    628 static bool audio_can_playback(struct audio_softc *);
    629 static bool audio_can_capture(struct audio_softc *);
    630 static int audio_check_params(audio_format2_t *);
    631 static int audio_mixers_init(struct audio_softc *sc, int,
    632 	const audio_format2_t *, const audio_format2_t *,
    633 	const audio_filter_reg_t *, const audio_filter_reg_t *);
    634 static int audio_select_freq(const struct audio_format *);
    635 static int audio_hw_probe(struct audio_softc *, audio_format2_t *, int);
    636 static int audio_hw_validate_format(struct audio_softc *, int,
    637 	const audio_format2_t *);
    638 static int audio_mixers_set_format(struct audio_softc *,
    639 	const struct audio_info *);
    640 static void audio_mixers_get_format(struct audio_softc *, struct audio_info *);
    641 static int audio_sysctl_blk_ms(SYSCTLFN_PROTO);
    642 static int audio_sysctl_multiuser(SYSCTLFN_PROTO);
    643 #if defined(AUDIO_DEBUG)
    644 static int audio_sysctl_debug(SYSCTLFN_PROTO);
    645 static void audio_format2_tostr(char *, size_t, const audio_format2_t *);
    646 static void audio_print_format2(const char *, const audio_format2_t *) __unused;
    647 #endif
    648 
    649 static void *audio_realloc(void *, size_t);
    650 static void audio_free_usrbuf(audio_track_t *);
    651 
    652 static audio_track_t *audio_track_create(struct audio_softc *,
    653 	audio_trackmixer_t *);
    654 static void audio_track_destroy(audio_track_t *);
    655 static audio_filter_t audio_track_get_codec(audio_track_t *,
    656 	const audio_format2_t *, const audio_format2_t *);
    657 static int audio_track_set_format(audio_track_t *, audio_format2_t *);
    658 static void audio_track_play(audio_track_t *);
    659 static int audio_track_drain(struct audio_softc *, audio_track_t *);
    660 static void audio_track_record(audio_track_t *);
    661 static void audio_track_clear(struct audio_softc *, audio_track_t *);
    662 
    663 static int audio_mixer_init(struct audio_softc *, int,
    664 	const audio_format2_t *, const audio_filter_reg_t *);
    665 static void audio_mixer_destroy(struct audio_softc *, audio_trackmixer_t *);
    666 static void audio_pmixer_start(struct audio_softc *, bool);
    667 static void audio_pmixer_process(struct audio_softc *);
    668 static void audio_pmixer_agc(audio_trackmixer_t *, int);
    669 static int  audio_pmixer_mix_track(audio_trackmixer_t *, audio_track_t *, int);
    670 static void audio_pmixer_output(struct audio_softc *);
    671 static int  audio_pmixer_halt(struct audio_softc *);
    672 static void audio_rmixer_start(struct audio_softc *);
    673 static void audio_rmixer_process(struct audio_softc *);
    674 static void audio_rmixer_input(struct audio_softc *);
    675 static int  audio_rmixer_halt(struct audio_softc *);
    676 
    677 static void mixer_init(struct audio_softc *);
    678 static int mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    679 static int mixer_close(struct audio_softc *, audio_file_t *);
    680 static int mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
    681 static void mixer_async_add(struct audio_softc *, pid_t);
    682 static void mixer_async_remove(struct audio_softc *, pid_t);
    683 static void mixer_signal(struct audio_softc *);
    684 
    685 static int au_portof(struct audio_softc *, char *, int);
    686 
    687 static void au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
    688 	mixer_devinfo_t *, const struct portname *);
    689 static int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
    690 static int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
    691 static int au_set_gain(struct audio_softc *, struct au_mixer_ports *, int, int);
    692 static void au_get_gain(struct audio_softc *, struct au_mixer_ports *,
    693 	u_int *, u_char *);
    694 static int au_set_port(struct audio_softc *, struct au_mixer_ports *, u_int);
    695 static int au_get_port(struct audio_softc *, struct au_mixer_ports *);
    696 static int au_set_monitor_gain(struct audio_softc *, int);
    697 static int au_get_monitor_gain(struct audio_softc *);
    698 static int audio_get_port(struct audio_softc *, mixer_ctrl_t *);
    699 static int audio_set_port(struct audio_softc *, mixer_ctrl_t *);
    700 
    701 void audio_mixsample_to_linear(audio_filter_arg_t *);
    702 
    703 static __inline struct audio_params
    704 format2_to_params(const audio_format2_t *f2)
    705 {
    706 	audio_params_t p;
    707 
    708 	/* validbits/precision <-> precision/stride */
    709 	p.sample_rate = f2->sample_rate;
    710 	p.channels    = f2->channels;
    711 	p.encoding    = f2->encoding;
    712 	p.validbits   = f2->precision;
    713 	p.precision   = f2->stride;
    714 	return p;
    715 }
    716 
    717 static __inline audio_format2_t
    718 params_to_format2(const struct audio_params *p)
    719 {
    720 	audio_format2_t f2;
    721 
    722 	/* precision/stride <-> validbits/precision */
    723 	f2.sample_rate = p->sample_rate;
    724 	f2.channels    = p->channels;
    725 	f2.encoding    = p->encoding;
    726 	f2.precision   = p->validbits;
    727 	f2.stride      = p->precision;
    728 	return f2;
    729 }
    730 
    731 /* Return true if this track is a playback track. */
    732 static __inline bool
    733 audio_track_is_playback(const audio_track_t *track)
    734 {
    735 
    736 	return ((track->mode & AUMODE_PLAY) != 0);
    737 }
    738 
    739 #if 0
    740 /* Return true if this track is a recording track. */
    741 static __inline bool
    742 audio_track_is_record(const audio_track_t *track)
    743 {
    744 
    745 	return ((track->mode & AUMODE_RECORD) != 0);
    746 }
    747 #endif
    748 
    749 #if 0 /* XXX Not used yet */
    750 /*
    751  * Convert 0..255 volume used in userland to internal presentation 0..256.
    752  */
    753 static __inline u_int
    754 audio_volume_to_inner(u_int v)
    755 {
    756 
    757 	return v < 127 ? v : v + 1;
    758 }
    759 
    760 /*
    761  * Convert 0..256 internal presentation to 0..255 volume used in userland.
    762  */
    763 static __inline u_int
    764 audio_volume_to_outer(u_int v)
    765 {
    766 
    767 	return v < 127 ? v : v - 1;
    768 }
    769 #endif /* 0 */
    770 
    771 static dev_type_open(audioopen);
    772 /* XXXMRG use more dev_type_xxx */
    773 
    774 static int
    775 audiounit(dev_t dev)
    776 {
    777 
    778 	return AUDIOUNIT(dev);
    779 }
    780 
    781 const struct cdevsw audio_cdevsw = {
    782 	.d_open = audioopen,
    783 	.d_close = noclose,
    784 	.d_read = noread,
    785 	.d_write = nowrite,
    786 	.d_ioctl = noioctl,
    787 	.d_stop = nostop,
    788 	.d_tty = notty,
    789 	.d_poll = nopoll,
    790 	.d_mmap = nommap,
    791 	.d_kqfilter = nokqfilter,
    792 	.d_discard = nodiscard,
    793 	.d_cfdriver = &audio_cd,
    794 	.d_devtounit = audiounit,
    795 	.d_flag = D_OTHER | D_MPSAFE
    796 };
    797 
    798 const struct fileops audio_fileops = {
    799 	.fo_name = "audio",
    800 	.fo_read = audioread,
    801 	.fo_write = audiowrite,
    802 	.fo_ioctl = audioioctl,
    803 	.fo_fcntl = fnullop_fcntl,
    804 	.fo_stat = audiostat,
    805 	.fo_poll = audiopoll,
    806 	.fo_close = audioclose,
    807 	.fo_mmap = audiommap,
    808 	.fo_kqfilter = audiokqfilter,
    809 	.fo_restart = fnullop_restart
    810 };
    811 
    812 /* The default audio mode: 8 kHz mono mu-law */
    813 static const struct audio_params audio_default = {
    814 	.sample_rate = 8000,
    815 	.encoding = AUDIO_ENCODING_ULAW,
    816 	.precision = 8,
    817 	.validbits = 8,
    818 	.channels = 1,
    819 };
    820 
    821 static const char *encoding_names[] = {
    822 	"none",
    823 	AudioEmulaw,
    824 	AudioEalaw,
    825 	"pcm16",
    826 	"pcm8",
    827 	AudioEadpcm,
    828 	AudioEslinear_le,
    829 	AudioEslinear_be,
    830 	AudioEulinear_le,
    831 	AudioEulinear_be,
    832 	AudioEslinear,
    833 	AudioEulinear,
    834 	AudioEmpeg_l1_stream,
    835 	AudioEmpeg_l1_packets,
    836 	AudioEmpeg_l1_system,
    837 	AudioEmpeg_l2_stream,
    838 	AudioEmpeg_l2_packets,
    839 	AudioEmpeg_l2_system,
    840 	AudioEac3,
    841 };
    842 
    843 /*
    844  * Returns encoding name corresponding to AUDIO_ENCODING_*.
    845  * Note that it may return a local buffer because it is mainly for debugging.
    846  */
    847 const char *
    848 audio_encoding_name(int encoding)
    849 {
    850 	static char buf[16];
    851 
    852 	if (0 <= encoding && encoding < __arraycount(encoding_names)) {
    853 		return encoding_names[encoding];
    854 	} else {
    855 		snprintf(buf, sizeof(buf), "enc=%d", encoding);
    856 		return buf;
    857 	}
    858 }
    859 
    860 /*
    861  * Supported encodings used by AUDIO_GETENC.
    862  * index and flags are set by code.
    863  * XXX is there any needs for SLINEAR_OE:>=16/ULINEAR_OE:>=16 ?
    864  */
    865 static const audio_encoding_t audio_encodings[] = {
    866 	{ 0, AudioEmulaw,	AUDIO_ENCODING_ULAW,		8,  0 },
    867 	{ 0, AudioEalaw,	AUDIO_ENCODING_ALAW,		8,  0 },
    868 	{ 0, AudioEslinear,	AUDIO_ENCODING_SLINEAR,		8,  0 },
    869 	{ 0, AudioEulinear,	AUDIO_ENCODING_ULINEAR,		8,  0 },
    870 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	16, 0 },
    871 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	16, 0 },
    872 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	16, 0 },
    873 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	16, 0 },
    874 #if defined(AUDIO_SUPPORT_LINEAR24)
    875 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	24, 0 },
    876 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	24, 0 },
    877 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	24, 0 },
    878 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	24, 0 },
    879 #endif
    880 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	32, 0 },
    881 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	32, 0 },
    882 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	32, 0 },
    883 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	32, 0 },
    884 };
    885 
    886 static const struct portname itable[] = {
    887 	{ AudioNmicrophone,	AUDIO_MICROPHONE },
    888 	{ AudioNline,		AUDIO_LINE_IN },
    889 	{ AudioNcd,		AUDIO_CD },
    890 	{ 0, 0 }
    891 };
    892 static const struct portname otable[] = {
    893 	{ AudioNspeaker,	AUDIO_SPEAKER },
    894 	{ AudioNheadphone,	AUDIO_HEADPHONE },
    895 	{ AudioNline,		AUDIO_LINE_OUT },
    896 	{ 0, 0 }
    897 };
    898 
    899 static struct psref_class *audio_psref_class __read_mostly;
    900 
    901 CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
    902     audiomatch, audioattach, audiodetach, audioactivate, audiorescan,
    903     audiochilddet, DVF_DETACH_SHUTDOWN);
    904 
    905 static int
    906 audiomatch(device_t parent, cfdata_t match, void *aux)
    907 {
    908 	struct audio_attach_args *sa;
    909 
    910 	sa = aux;
    911 	DPRINTF(1, "%s: type=%d sa=%p hw=%p\n",
    912 	     __func__, sa->type, sa, sa->hwif);
    913 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
    914 }
    915 
    916 static void
    917 audioattach(device_t parent, device_t self, void *aux)
    918 {
    919 	struct audio_softc *sc;
    920 	struct audio_attach_args *sa;
    921 	const struct audio_hw_if *hw_if;
    922 	audio_format2_t phwfmt;
    923 	audio_format2_t rhwfmt;
    924 	audio_filter_reg_t pfil;
    925 	audio_filter_reg_t rfil;
    926 	const struct sysctlnode *node;
    927 	void *hdlp;
    928 	bool has_playback;
    929 	bool has_capture;
    930 	bool has_indep;
    931 	bool has_fulldup;
    932 	int mode;
    933 	int error;
    934 
    935 	sc = device_private(self);
    936 	sc->sc_dev = self;
    937 	sa = (struct audio_attach_args *)aux;
    938 	hw_if = sa->hwif;
    939 	hdlp = sa->hdl;
    940 
    941 	if (hw_if == NULL) {
    942 		panic("audioattach: missing hw_if method");
    943 	}
    944 	if (hw_if->get_locks == NULL || hw_if->get_props == NULL) {
    945 		aprint_error(": missing mandatory method\n");
    946 		return;
    947 	}
    948 
    949 	hw_if->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
    950 	sc->sc_props = hw_if->get_props(hdlp);
    951 
    952 	has_playback = (sc->sc_props & AUDIO_PROP_PLAYBACK);
    953 	has_capture  = (sc->sc_props & AUDIO_PROP_CAPTURE);
    954 	has_indep    = (sc->sc_props & AUDIO_PROP_INDEPENDENT);
    955 	has_fulldup  = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
    956 
    957 #ifdef DIAGNOSTIC
    958 	if (hw_if->query_format == NULL ||
    959 	    hw_if->set_format == NULL ||
    960 	    hw_if->getdev == NULL ||
    961 	    hw_if->set_port == NULL ||
    962 	    hw_if->get_port == NULL ||
    963 	    hw_if->query_devinfo == NULL) {
    964 		aprint_error(": missing mandatory method\n");
    965 		return;
    966 	}
    967 	if (has_playback) {
    968 		if ((hw_if->start_output == NULL &&
    969 		     hw_if->trigger_output == NULL) ||
    970 		    hw_if->halt_output == NULL) {
    971 			aprint_error(": missing playback method\n");
    972 		}
    973 	}
    974 	if (has_capture) {
    975 		if ((hw_if->start_input == NULL &&
    976 		     hw_if->trigger_input == NULL) ||
    977 		    hw_if->halt_input == NULL) {
    978 			aprint_error(": missing capture method\n");
    979 		}
    980 	}
    981 #endif
    982 
    983 	sc->hw_if = hw_if;
    984 	sc->hw_hdl = hdlp;
    985 	sc->hw_dev = parent;
    986 
    987 	sc->sc_exlock = 1;
    988 	sc->sc_blk_ms = AUDIO_BLK_MS;
    989 	SLIST_INIT(&sc->sc_files);
    990 	cv_init(&sc->sc_exlockcv, "audiolk");
    991 	sc->sc_am_capacity = 0;
    992 	sc->sc_am_used = 0;
    993 	sc->sc_am = NULL;
    994 
    995 	/* MMAP is now supported by upper layer.  */
    996 	sc->sc_props |= AUDIO_PROP_MMAP;
    997 
    998 	KASSERT(has_playback || has_capture);
    999 	/* Unidirectional device must have neither FULLDUP nor INDEPENDENT. */
   1000 	if (!has_playback || !has_capture) {
   1001 		KASSERT(!has_indep);
   1002 		KASSERT(!has_fulldup);
   1003 	}
   1004 
   1005 	mode = 0;
   1006 	if (has_playback) {
   1007 		aprint_normal(": playback");
   1008 		mode |= AUMODE_PLAY;
   1009 	}
   1010 	if (has_capture) {
   1011 		aprint_normal("%c capture", has_playback ? ',' : ':');
   1012 		mode |= AUMODE_RECORD;
   1013 	}
   1014 	if (has_playback && has_capture) {
   1015 		if (has_fulldup)
   1016 			aprint_normal(", full duplex");
   1017 		else
   1018 			aprint_normal(", half duplex");
   1019 
   1020 		if (has_indep)
   1021 			aprint_normal(", independent");
   1022 	}
   1023 
   1024 	aprint_naive("\n");
   1025 	aprint_normal("\n");
   1026 
   1027 	/* probe hw params */
   1028 	memset(&phwfmt, 0, sizeof(phwfmt));
   1029 	memset(&rhwfmt, 0, sizeof(rhwfmt));
   1030 	memset(&pfil, 0, sizeof(pfil));
   1031 	memset(&rfil, 0, sizeof(rfil));
   1032 	if (has_indep) {
   1033 		int perror, rerror;
   1034 
   1035 		/* On independent devices, probe separately. */
   1036 		perror = audio_hw_probe(sc, &phwfmt, AUMODE_PLAY);
   1037 		rerror = audio_hw_probe(sc, &rhwfmt, AUMODE_RECORD);
   1038 		if (perror && rerror) {
   1039 			aprint_error_dev(self,
   1040 			    "audio_hw_probe failed: perror=%d, rerror=%d\n",
   1041 			    perror, rerror);
   1042 			goto bad;
   1043 		}
   1044 		if (perror) {
   1045 			mode &= ~AUMODE_PLAY;
   1046 			aprint_error_dev(self, "audio_hw_probe failed: "
   1047 			    "errno=%d, playback disabled\n", perror);
   1048 		}
   1049 		if (rerror) {
   1050 			mode &= ~AUMODE_RECORD;
   1051 			aprint_error_dev(self, "audio_hw_probe failed: "
   1052 			    "errno=%d, capture disabled\n", rerror);
   1053 		}
   1054 	} else {
   1055 		/*
   1056 		 * On non independent devices or uni-directional devices,
   1057 		 * probe once (simultaneously).
   1058 		 */
   1059 		audio_format2_t *fmt = has_playback ? &phwfmt : &rhwfmt;
   1060 		error = audio_hw_probe(sc, fmt, mode);
   1061 		if (error) {
   1062 			aprint_error_dev(self,
   1063 			    "audio_hw_probe failed: errno=%d\n", error);
   1064 			goto bad;
   1065 		}
   1066 		if (has_playback && has_capture)
   1067 			rhwfmt = phwfmt;
   1068 	}
   1069 
   1070 	/* Make device id available */
   1071 	if (audio_properties(sc))
   1072 		aprint_error_dev(self, "audio_properties failed\n");
   1073 
   1074 	/* Init hardware. */
   1075 	/* hw_probe() also validates [pr]hwfmt.  */
   1076 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   1077 	if (error) {
   1078 		aprint_error_dev(self,
   1079 		    "audio_hw_set_format failed: errno=%d\n", error);
   1080 		goto bad;
   1081 	}
   1082 
   1083 	/*
   1084 	 * Init track mixers.  If at least one direction is available on
   1085 	 * attach time, we assume a success.
   1086 	 */
   1087 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   1088 	if (sc->sc_pmixer == NULL && sc->sc_rmixer == NULL) {
   1089 		aprint_error_dev(self,
   1090 		    "audio_mixers_init failed: errno=%d\n", error);
   1091 		goto bad;
   1092 	}
   1093 
   1094 	sc->sc_psz = pserialize_create();
   1095 	psref_target_init(&sc->sc_psref, audio_psref_class);
   1096 
   1097 	selinit(&sc->sc_wsel);
   1098 	selinit(&sc->sc_rsel);
   1099 
   1100 	/* Initial parameter of /dev/sound */
   1101 	sc->sc_sound_pparams = params_to_format2(&audio_default);
   1102 	sc->sc_sound_rparams = params_to_format2(&audio_default);
   1103 	sc->sc_sound_ppause = false;
   1104 	sc->sc_sound_rpause = false;
   1105 
   1106 	/* XXX TODO: consider about sc_ai */
   1107 
   1108 	mixer_init(sc);
   1109 	TRACE(2, "inputs ports=0x%x, input master=%d, "
   1110 	    "output ports=0x%x, output master=%d",
   1111 	    sc->sc_inports.allports, sc->sc_inports.master,
   1112 	    sc->sc_outports.allports, sc->sc_outports.master);
   1113 
   1114 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
   1115 	    0,
   1116 	    CTLTYPE_NODE, device_xname(sc->sc_dev),
   1117 	    SYSCTL_DESCR("audio test"),
   1118 	    NULL, 0,
   1119 	    NULL, 0,
   1120 	    CTL_HW,
   1121 	    CTL_CREATE, CTL_EOL);
   1122 
   1123 	if (node != NULL) {
   1124 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1125 		    CTLFLAG_READWRITE,
   1126 		    CTLTYPE_INT, "blk_ms",
   1127 		    SYSCTL_DESCR("blocksize in msec"),
   1128 		    audio_sysctl_blk_ms, 0, (void *)sc, 0,
   1129 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1130 
   1131 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1132 		    CTLFLAG_READWRITE,
   1133 		    CTLTYPE_BOOL, "multiuser",
   1134 		    SYSCTL_DESCR("allow multiple user access"),
   1135 		    audio_sysctl_multiuser, 0, (void *)sc, 0,
   1136 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1137 
   1138 #if defined(AUDIO_DEBUG)
   1139 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1140 		    CTLFLAG_READWRITE,
   1141 		    CTLTYPE_INT, "debug",
   1142 		    SYSCTL_DESCR("debug level (0..4)"),
   1143 		    audio_sysctl_debug, 0, (void *)sc, 0,
   1144 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1145 #endif
   1146 	}
   1147 
   1148 #ifdef AUDIO_PM_IDLE
   1149 	callout_init(&sc->sc_idle_counter, 0);
   1150 	callout_setfunc(&sc->sc_idle_counter, audio_idle, self);
   1151 #endif
   1152 
   1153 	if (!pmf_device_register(self, audio_suspend, audio_resume))
   1154 		aprint_error_dev(self, "couldn't establish power handler\n");
   1155 #ifdef AUDIO_PM_IDLE
   1156 	if (!device_active_register(self, audio_activity))
   1157 		aprint_error_dev(self, "couldn't register activity handler\n");
   1158 #endif
   1159 
   1160 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
   1161 	    audio_volume_down, true))
   1162 		aprint_error_dev(self, "couldn't add volume down handler\n");
   1163 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
   1164 	    audio_volume_up, true))
   1165 		aprint_error_dev(self, "couldn't add volume up handler\n");
   1166 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE,
   1167 	    audio_volume_toggle, true))
   1168 		aprint_error_dev(self, "couldn't add volume toggle handler\n");
   1169 
   1170 #ifdef AUDIO_PM_IDLE
   1171 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   1172 #endif
   1173 
   1174 #if defined(AUDIO_DEBUG)
   1175 	audio_mlog_init();
   1176 #endif
   1177 
   1178 	audiorescan(self, NULL, NULL);
   1179 	sc->sc_exlock = 0;
   1180 	return;
   1181 
   1182 bad:
   1183 	/* Clearing hw_if means that device is attached but disabled. */
   1184 	sc->hw_if = NULL;
   1185 	sc->sc_exlock = 0;
   1186 	aprint_error_dev(sc->sc_dev, "disabled\n");
   1187 	return;
   1188 }
   1189 
   1190 /*
   1191  * Identify audio backend device for drvctl.
   1192  */
   1193 static int
   1194 audio_properties(struct audio_softc *sc)
   1195 {
   1196 	prop_dictionary_t dict = device_properties(sc->sc_dev);
   1197 	audio_device_t adev;
   1198 	int error;
   1199 
   1200 	error = sc->hw_if->getdev(sc->hw_hdl, &adev);
   1201 	if (error)
   1202 		return error;
   1203 
   1204 	prop_dictionary_set_string(dict, "name", adev.name);
   1205 	prop_dictionary_set_string(dict, "version", adev.version);
   1206 	prop_dictionary_set_string(dict, "config", adev.config);
   1207 
   1208 	return 0;
   1209 }
   1210 
   1211 /*
   1212  * Initialize hardware mixer.
   1213  * This function is called from audioattach().
   1214  */
   1215 static void
   1216 mixer_init(struct audio_softc *sc)
   1217 {
   1218 	mixer_devinfo_t mi;
   1219 	int iclass, mclass, oclass, rclass;
   1220 	int record_master_found, record_source_found;
   1221 
   1222 	iclass = mclass = oclass = rclass = -1;
   1223 	sc->sc_inports.index = -1;
   1224 	sc->sc_inports.master = -1;
   1225 	sc->sc_inports.nports = 0;
   1226 	sc->sc_inports.isenum = false;
   1227 	sc->sc_inports.allports = 0;
   1228 	sc->sc_inports.isdual = false;
   1229 	sc->sc_inports.mixerout = -1;
   1230 	sc->sc_inports.cur_port = -1;
   1231 	sc->sc_outports.index = -1;
   1232 	sc->sc_outports.master = -1;
   1233 	sc->sc_outports.nports = 0;
   1234 	sc->sc_outports.isenum = false;
   1235 	sc->sc_outports.allports = 0;
   1236 	sc->sc_outports.isdual = false;
   1237 	sc->sc_outports.mixerout = -1;
   1238 	sc->sc_outports.cur_port = -1;
   1239 	sc->sc_monitor_port = -1;
   1240 	/*
   1241 	 * Read through the underlying driver's list, picking out the class
   1242 	 * names from the mixer descriptions. We'll need them to decode the
   1243 	 * mixer descriptions on the next pass through the loop.
   1244 	 */
   1245 	mutex_enter(sc->sc_lock);
   1246 	for(mi.index = 0; ; mi.index++) {
   1247 		if (audio_query_devinfo(sc, &mi) != 0)
   1248 			break;
   1249 		 /*
   1250 		  * The type of AUDIO_MIXER_CLASS merely introduces a class.
   1251 		  * All the other types describe an actual mixer.
   1252 		  */
   1253 		if (mi.type == AUDIO_MIXER_CLASS) {
   1254 			if (strcmp(mi.label.name, AudioCinputs) == 0)
   1255 				iclass = mi.mixer_class;
   1256 			if (strcmp(mi.label.name, AudioCmonitor) == 0)
   1257 				mclass = mi.mixer_class;
   1258 			if (strcmp(mi.label.name, AudioCoutputs) == 0)
   1259 				oclass = mi.mixer_class;
   1260 			if (strcmp(mi.label.name, AudioCrecord) == 0)
   1261 				rclass = mi.mixer_class;
   1262 		}
   1263 	}
   1264 	mutex_exit(sc->sc_lock);
   1265 
   1266 	/* Allocate save area.  Ensure non-zero allocation. */
   1267 	sc->sc_nmixer_states = mi.index;
   1268 	sc->sc_mixer_state = kmem_zalloc(sizeof(sc->sc_mixer_state[0]) *
   1269 	    (sc->sc_nmixer_states + 1), KM_SLEEP);
   1270 
   1271 	/*
   1272 	 * This is where we assign each control in the "audio" model, to the
   1273 	 * underlying "mixer" control.  We walk through the whole list once,
   1274 	 * assigning likely candidates as we come across them.
   1275 	 */
   1276 	record_master_found = 0;
   1277 	record_source_found = 0;
   1278 	mutex_enter(sc->sc_lock);
   1279 	for(mi.index = 0; ; mi.index++) {
   1280 		if (audio_query_devinfo(sc, &mi) != 0)
   1281 			break;
   1282 		KASSERT(mi.index < sc->sc_nmixer_states);
   1283 		if (mi.type == AUDIO_MIXER_CLASS)
   1284 			continue;
   1285 		if (mi.mixer_class == iclass) {
   1286 			/*
   1287 			 * AudioCinputs is only a fallback, when we don't
   1288 			 * find what we're looking for in AudioCrecord, so
   1289 			 * check the flags before accepting one of these.
   1290 			 */
   1291 			if (strcmp(mi.label.name, AudioNmaster) == 0
   1292 			    && record_master_found == 0)
   1293 				sc->sc_inports.master = mi.index;
   1294 			if (strcmp(mi.label.name, AudioNsource) == 0
   1295 			    && record_source_found == 0) {
   1296 				if (mi.type == AUDIO_MIXER_ENUM) {
   1297 				    int i;
   1298 				    for(i = 0; i < mi.un.e.num_mem; i++)
   1299 					if (strcmp(mi.un.e.member[i].label.name,
   1300 						    AudioNmixerout) == 0)
   1301 						sc->sc_inports.mixerout =
   1302 						    mi.un.e.member[i].ord;
   1303 				}
   1304 				au_setup_ports(sc, &sc->sc_inports, &mi,
   1305 				    itable);
   1306 			}
   1307 			if (strcmp(mi.label.name, AudioNdac) == 0 &&
   1308 			    sc->sc_outports.master == -1)
   1309 				sc->sc_outports.master = mi.index;
   1310 		} else if (mi.mixer_class == mclass) {
   1311 			if (strcmp(mi.label.name, AudioNmonitor) == 0)
   1312 				sc->sc_monitor_port = mi.index;
   1313 		} else if (mi.mixer_class == oclass) {
   1314 			if (strcmp(mi.label.name, AudioNmaster) == 0)
   1315 				sc->sc_outports.master = mi.index;
   1316 			if (strcmp(mi.label.name, AudioNselect) == 0)
   1317 				au_setup_ports(sc, &sc->sc_outports, &mi,
   1318 				    otable);
   1319 		} else if (mi.mixer_class == rclass) {
   1320 			/*
   1321 			 * These are the preferred mixers for the audio record
   1322 			 * controls, so set the flags here, but don't check.
   1323 			 */
   1324 			if (strcmp(mi.label.name, AudioNmaster) == 0) {
   1325 				sc->sc_inports.master = mi.index;
   1326 				record_master_found = 1;
   1327 			}
   1328 #if 1	/* Deprecated. Use AudioNmaster. */
   1329 			if (strcmp(mi.label.name, AudioNrecord) == 0) {
   1330 				sc->sc_inports.master = mi.index;
   1331 				record_master_found = 1;
   1332 			}
   1333 			if (strcmp(mi.label.name, AudioNvolume) == 0) {
   1334 				sc->sc_inports.master = mi.index;
   1335 				record_master_found = 1;
   1336 			}
   1337 #endif
   1338 			if (strcmp(mi.label.name, AudioNsource) == 0) {
   1339 				if (mi.type == AUDIO_MIXER_ENUM) {
   1340 				    int i;
   1341 				    for(i = 0; i < mi.un.e.num_mem; i++)
   1342 					if (strcmp(mi.un.e.member[i].label.name,
   1343 						    AudioNmixerout) == 0)
   1344 						sc->sc_inports.mixerout =
   1345 						    mi.un.e.member[i].ord;
   1346 				}
   1347 				au_setup_ports(sc, &sc->sc_inports, &mi,
   1348 				    itable);
   1349 				record_source_found = 1;
   1350 			}
   1351 		}
   1352 	}
   1353 	mutex_exit(sc->sc_lock);
   1354 }
   1355 
   1356 static int
   1357 audioactivate(device_t self, enum devact act)
   1358 {
   1359 	struct audio_softc *sc = device_private(self);
   1360 
   1361 	switch (act) {
   1362 	case DVACT_DEACTIVATE:
   1363 		mutex_enter(sc->sc_lock);
   1364 		sc->sc_dying = true;
   1365 		cv_broadcast(&sc->sc_exlockcv);
   1366 		mutex_exit(sc->sc_lock);
   1367 		return 0;
   1368 	default:
   1369 		return EOPNOTSUPP;
   1370 	}
   1371 }
   1372 
   1373 static int
   1374 audiodetach(device_t self, int flags)
   1375 {
   1376 	struct audio_softc *sc;
   1377 	struct audio_file *file;
   1378 	int maj, mn;
   1379 	int error;
   1380 
   1381 	sc = device_private(self);
   1382 	TRACE(2, "flags=%d", flags);
   1383 
   1384 	/* device is not initialized */
   1385 	if (sc->hw_if == NULL)
   1386 		return 0;
   1387 
   1388 	/* Start draining existing accessors of the device. */
   1389 	error = config_detach_children(self, flags);
   1390 	if (error)
   1391 		return error;
   1392 
   1393 	/*
   1394 	 * Prevent new opens and wait for existing opens to complete.
   1395 	 *
   1396 	 * At the moment there are only four bits in the minor for the
   1397 	 * unit number, so we only revoke if the unit number could be
   1398 	 * used in a device node.
   1399 	 *
   1400 	 * XXX If we want more audio units, we need to encode them
   1401 	 * more elaborately in the minor space.
   1402 	 */
   1403 	maj = cdevsw_lookup_major(&audio_cdevsw);
   1404 	mn = device_unit(self);
   1405 	if (mn <= 0xf) {
   1406 		vdevgone(maj, mn|SOUND_DEVICE, mn|SOUND_DEVICE, VCHR);
   1407 		vdevgone(maj, mn|AUDIO_DEVICE, mn|AUDIO_DEVICE, VCHR);
   1408 		vdevgone(maj, mn|AUDIOCTL_DEVICE, mn|AUDIOCTL_DEVICE, VCHR);
   1409 		vdevgone(maj, mn|MIXER_DEVICE, mn|MIXER_DEVICE, VCHR);
   1410 	}
   1411 
   1412 	/*
   1413 	 * This waits currently running sysctls to finish if exists.
   1414 	 * After this, no more new sysctls will come.
   1415 	 */
   1416 	sysctl_teardown(&sc->sc_log);
   1417 
   1418 	mutex_enter(sc->sc_lock);
   1419 	sc->sc_dying = true;
   1420 	cv_broadcast(&sc->sc_exlockcv);
   1421 	if (sc->sc_pmixer)
   1422 		cv_broadcast(&sc->sc_pmixer->outcv);
   1423 	if (sc->sc_rmixer)
   1424 		cv_broadcast(&sc->sc_rmixer->outcv);
   1425 
   1426 	/* Prevent new users */
   1427 	SLIST_FOREACH(file, &sc->sc_files, entry) {
   1428 		atomic_store_relaxed(&file->dying, true);
   1429 	}
   1430 	mutex_exit(sc->sc_lock);
   1431 
   1432 	/*
   1433 	 * Wait for existing users to drain.
   1434 	 * - pserialize_perform waits for all pserialize_read sections on
   1435 	 *   all CPUs; after this, no more new psref_acquire can happen.
   1436 	 * - psref_target_destroy waits for all extant acquired psrefs to
   1437 	 *   be psref_released.
   1438 	 */
   1439 	pserialize_perform(sc->sc_psz);
   1440 	psref_target_destroy(&sc->sc_psref, audio_psref_class);
   1441 
   1442 	/*
   1443 	 * We are now guaranteed that there are no calls to audio fileops
   1444 	 * that hold sc, and any new calls with files that were for sc will
   1445 	 * fail.  Thus, we now have exclusive access to the softc.
   1446 	 */
   1447 	sc->sc_exlock = 1;
   1448 
   1449 	/*
   1450 	 * Clean up all open instances.
   1451 	 */
   1452 	mutex_enter(sc->sc_lock);
   1453 	while ((file = SLIST_FIRST(&sc->sc_files)) != NULL) {
   1454 		mutex_enter(sc->sc_intr_lock);
   1455 		SLIST_REMOVE_HEAD(&sc->sc_files, entry);
   1456 		mutex_exit(sc->sc_intr_lock);
   1457 		if (file->ptrack || file->rtrack) {
   1458 			mutex_exit(sc->sc_lock);
   1459 			audio_unlink(sc, file);
   1460 			mutex_enter(sc->sc_lock);
   1461 		}
   1462 	}
   1463 	mutex_exit(sc->sc_lock);
   1464 
   1465 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN,
   1466 	    audio_volume_down, true);
   1467 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP,
   1468 	    audio_volume_up, true);
   1469 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE,
   1470 	    audio_volume_toggle, true);
   1471 
   1472 #ifdef AUDIO_PM_IDLE
   1473 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   1474 
   1475 	device_active_deregister(self, audio_activity);
   1476 #endif
   1477 
   1478 	pmf_device_deregister(self);
   1479 
   1480 	/* Free resources */
   1481 	if (sc->sc_pmixer) {
   1482 		audio_mixer_destroy(sc, sc->sc_pmixer);
   1483 		kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
   1484 	}
   1485 	if (sc->sc_rmixer) {
   1486 		audio_mixer_destroy(sc, sc->sc_rmixer);
   1487 		kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
   1488 	}
   1489 	if (sc->sc_am)
   1490 		kern_free(sc->sc_am);
   1491 
   1492 	seldestroy(&sc->sc_wsel);
   1493 	seldestroy(&sc->sc_rsel);
   1494 
   1495 #ifdef AUDIO_PM_IDLE
   1496 	callout_destroy(&sc->sc_idle_counter);
   1497 #endif
   1498 
   1499 	cv_destroy(&sc->sc_exlockcv);
   1500 
   1501 #if defined(AUDIO_DEBUG)
   1502 	audio_mlog_free();
   1503 #endif
   1504 
   1505 	return 0;
   1506 }
   1507 
   1508 static void
   1509 audiochilddet(device_t self, device_t child)
   1510 {
   1511 
   1512 	/* we hold no child references, so do nothing */
   1513 }
   1514 
   1515 static int
   1516 audiosearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
   1517 {
   1518 
   1519 	if (config_probe(parent, cf, aux))
   1520 		config_attach(parent, cf, aux, NULL,
   1521 		    CFARGS_NONE);
   1522 
   1523 	return 0;
   1524 }
   1525 
   1526 static int
   1527 audiorescan(device_t self, const char *ifattr, const int *locators)
   1528 {
   1529 	struct audio_softc *sc = device_private(self);
   1530 
   1531 	config_search(sc->sc_dev, NULL,
   1532 	    CFARGS(.search = audiosearch));
   1533 
   1534 	return 0;
   1535 }
   1536 
   1537 /*
   1538  * Called from hardware driver.  This is where the MI audio driver gets
   1539  * probed/attached to the hardware driver.
   1540  */
   1541 device_t
   1542 audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev)
   1543 {
   1544 	struct audio_attach_args arg;
   1545 
   1546 #ifdef DIAGNOSTIC
   1547 	if (ahwp == NULL) {
   1548 		aprint_error("audio_attach_mi: NULL\n");
   1549 		return 0;
   1550 	}
   1551 #endif
   1552 	arg.type = AUDIODEV_TYPE_AUDIO;
   1553 	arg.hwif = ahwp;
   1554 	arg.hdl = hdlp;
   1555 	return config_found(dev, &arg, audioprint,
   1556 	    CFARGS(.iattr = "audiobus"));
   1557 }
   1558 
   1559 /*
   1560  * audio_printf() outputs fmt... with the audio device name and MD device
   1561  * name prefixed.  If the message is considered to be related to the MD
   1562  * driver, use this one instead of device_printf().
   1563  */
   1564 static void
   1565 audio_printf(struct audio_softc *sc, const char *fmt, ...)
   1566 {
   1567 	va_list ap;
   1568 
   1569 	printf("%s(%s): ", device_xname(sc->sc_dev), device_xname(sc->hw_dev));
   1570 	va_start(ap, fmt);
   1571 	vprintf(fmt, ap);
   1572 	va_end(ap);
   1573 }
   1574 
   1575 /*
   1576  * Enter critical section and also keep sc_lock.
   1577  * If successful, returns 0 with sc_lock held.  Otherwise returns errno.
   1578  * Must be called without sc_lock held.
   1579  */
   1580 static int
   1581 audio_exlock_mutex_enter(struct audio_softc *sc)
   1582 {
   1583 	int error;
   1584 
   1585 	mutex_enter(sc->sc_lock);
   1586 	if (sc->sc_dying) {
   1587 		mutex_exit(sc->sc_lock);
   1588 		return EIO;
   1589 	}
   1590 
   1591 	while (__predict_false(sc->sc_exlock != 0)) {
   1592 		error = cv_wait_sig(&sc->sc_exlockcv, sc->sc_lock);
   1593 		if (sc->sc_dying)
   1594 			error = EIO;
   1595 		if (error) {
   1596 			mutex_exit(sc->sc_lock);
   1597 			return error;
   1598 		}
   1599 	}
   1600 
   1601 	/* Acquire */
   1602 	sc->sc_exlock = 1;
   1603 	return 0;
   1604 }
   1605 
   1606 /*
   1607  * Exit critical section and exit sc_lock.
   1608  * Must be called with sc_lock held.
   1609  */
   1610 static void
   1611 audio_exlock_mutex_exit(struct audio_softc *sc)
   1612 {
   1613 
   1614 	KASSERT(mutex_owned(sc->sc_lock));
   1615 
   1616 	sc->sc_exlock = 0;
   1617 	cv_broadcast(&sc->sc_exlockcv);
   1618 	mutex_exit(sc->sc_lock);
   1619 }
   1620 
   1621 /*
   1622  * Enter critical section.
   1623  * If successful, it returns 0.  Otherwise returns errno.
   1624  * Must be called without sc_lock held.
   1625  * This function returns without sc_lock held.
   1626  */
   1627 static int
   1628 audio_exlock_enter(struct audio_softc *sc)
   1629 {
   1630 	int error;
   1631 
   1632 	error = audio_exlock_mutex_enter(sc);
   1633 	if (error)
   1634 		return error;
   1635 	mutex_exit(sc->sc_lock);
   1636 	return 0;
   1637 }
   1638 
   1639 /*
   1640  * Exit critical section.
   1641  * Must be called without sc_lock held.
   1642  */
   1643 static void
   1644 audio_exlock_exit(struct audio_softc *sc)
   1645 {
   1646 
   1647 	mutex_enter(sc->sc_lock);
   1648 	audio_exlock_mutex_exit(sc);
   1649 }
   1650 
   1651 /*
   1652  * Get sc from file, and increment reference counter for this sc.
   1653  * This is intended to be used for methods other than open.
   1654  * If successful, returns sc.  Otherwise returns NULL.
   1655  */
   1656 struct audio_softc *
   1657 audio_sc_acquire_fromfile(audio_file_t *file, struct psref *refp)
   1658 {
   1659 	int s;
   1660 	bool dying;
   1661 
   1662 	/* Block audiodetach while we acquire a reference */
   1663 	s = pserialize_read_enter();
   1664 
   1665 	/* If close or audiodetach already ran, tough -- no more audio */
   1666 	dying = atomic_load_relaxed(&file->dying);
   1667 	if (dying) {
   1668 		pserialize_read_exit(s);
   1669 		return NULL;
   1670 	}
   1671 
   1672 	/* Acquire a reference */
   1673 	psref_acquire(refp, &file->sc->sc_psref, audio_psref_class);
   1674 
   1675 	/* Now sc won't go away until we drop the reference count */
   1676 	pserialize_read_exit(s);
   1677 
   1678 	return file->sc;
   1679 }
   1680 
   1681 /*
   1682  * Decrement reference counter for this sc.
   1683  */
   1684 void
   1685 audio_sc_release(struct audio_softc *sc, struct psref *refp)
   1686 {
   1687 
   1688 	psref_release(refp, &sc->sc_psref, audio_psref_class);
   1689 }
   1690 
   1691 /*
   1692  * Wait for I/O to complete, releasing sc_lock.
   1693  * Must be called with sc_lock held.
   1694  */
   1695 static int
   1696 audio_track_waitio(struct audio_softc *sc, audio_track_t *track,
   1697     const char *mess)
   1698 {
   1699 	int error;
   1700 
   1701 	KASSERT(track);
   1702 	KASSERT(mutex_owned(sc->sc_lock));
   1703 
   1704 	/* Wait for pending I/O to complete. */
   1705 	error = cv_timedwait_sig(&track->mixer->outcv, sc->sc_lock,
   1706 	    mstohz(AUDIO_TIMEOUT));
   1707 	if (sc->sc_suspending) {
   1708 		/* If it's about to suspend, ignore timeout error. */
   1709 		if (error == EWOULDBLOCK) {
   1710 			TRACET(2, track, "timeout (suspending)");
   1711 			return 0;
   1712 		}
   1713 	}
   1714 	if (sc->sc_dying) {
   1715 		error = EIO;
   1716 	}
   1717 	if (error) {
   1718 		TRACET(2, track, "cv_timedwait_sig failed %d", error);
   1719 		if (error == EWOULDBLOCK) {
   1720 			audio_ring_t *usrbuf = &track->usrbuf;
   1721 			audio_ring_t *outbuf = &track->outbuf;
   1722 			audio_printf(sc,
   1723 			    "%s: device timeout, seq=%d, usrbuf=%d/H%d, outbuf=%d/%d\n",
   1724 			    mess, (int)track->seq,
   1725 			    usrbuf->used, track->usrbuf_usedhigh,
   1726 			    outbuf->used, outbuf->capacity);
   1727 		}
   1728 	} else {
   1729 		TRACET(3, track, "wakeup");
   1730 	}
   1731 	return error;
   1732 }
   1733 
   1734 /*
   1735  * Try to acquire track lock.
   1736  * It doesn't block if the track lock is already acquired.
   1737  * Returns true if the track lock was acquired, or false if the track
   1738  * lock was already acquired.
   1739  */
   1740 static __inline bool
   1741 audio_track_lock_tryenter(audio_track_t *track)
   1742 {
   1743 
   1744 	if (atomic_swap_uint(&track->lock, 1) != 0)
   1745 		return false;
   1746 	membar_acquire();
   1747 	return true;
   1748 }
   1749 
   1750 /*
   1751  * Acquire track lock.
   1752  */
   1753 static __inline void
   1754 audio_track_lock_enter(audio_track_t *track)
   1755 {
   1756 
   1757 	/* Don't sleep here. */
   1758 	while (audio_track_lock_tryenter(track) == false)
   1759 		SPINLOCK_BACKOFF_HOOK;
   1760 }
   1761 
   1762 /*
   1763  * Release track lock.
   1764  */
   1765 static __inline void
   1766 audio_track_lock_exit(audio_track_t *track)
   1767 {
   1768 
   1769 	atomic_store_release(&track->lock, 0);
   1770 }
   1771 
   1772 
   1773 static int
   1774 audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1775 {
   1776 	struct audio_softc *sc;
   1777 	int error;
   1778 
   1779 	/*
   1780 	 * Find the device.  Because we wired the cdevsw to the audio
   1781 	 * autoconf instance, the system ensures it will not go away
   1782 	 * until after we return.
   1783 	 */
   1784 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1785 	if (sc == NULL || sc->hw_if == NULL)
   1786 		return ENXIO;
   1787 
   1788 	error = audio_exlock_enter(sc);
   1789 	if (error)
   1790 		return error;
   1791 
   1792 	device_active(sc->sc_dev, DVA_SYSTEM);
   1793 	switch (AUDIODEV(dev)) {
   1794 	case SOUND_DEVICE:
   1795 	case AUDIO_DEVICE:
   1796 		error = audio_open(dev, sc, flags, ifmt, l, NULL);
   1797 		break;
   1798 	case AUDIOCTL_DEVICE:
   1799 		error = audioctl_open(dev, sc, flags, ifmt, l);
   1800 		break;
   1801 	case MIXER_DEVICE:
   1802 		error = mixer_open(dev, sc, flags, ifmt, l);
   1803 		break;
   1804 	default:
   1805 		error = ENXIO;
   1806 		break;
   1807 	}
   1808 	audio_exlock_exit(sc);
   1809 
   1810 	return error;
   1811 }
   1812 
   1813 static int
   1814 audioclose(struct file *fp)
   1815 {
   1816 	struct audio_softc *sc;
   1817 	struct psref sc_ref;
   1818 	audio_file_t *file;
   1819 	int bound;
   1820 	int error;
   1821 	dev_t dev;
   1822 
   1823 	KASSERT(fp->f_audioctx);
   1824 	file = fp->f_audioctx;
   1825 	dev = file->dev;
   1826 	error = 0;
   1827 
   1828 	/*
   1829 	 * audioclose() must
   1830 	 * - unplug track from the trackmixer (and unplug anything from softc),
   1831 	 *   if sc exists.
   1832 	 * - free all memory objects, regardless of sc.
   1833 	 */
   1834 
   1835 	bound = curlwp_bind();
   1836 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   1837 	if (sc) {
   1838 		switch (AUDIODEV(dev)) {
   1839 		case SOUND_DEVICE:
   1840 		case AUDIO_DEVICE:
   1841 			error = audio_close(sc, file);
   1842 			break;
   1843 		case AUDIOCTL_DEVICE:
   1844 			mutex_enter(sc->sc_lock);
   1845 			mutex_enter(sc->sc_intr_lock);
   1846 			SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
   1847 			mutex_exit(sc->sc_intr_lock);
   1848 			mutex_exit(sc->sc_lock);
   1849 			error = 0;
   1850 			break;
   1851 		case MIXER_DEVICE:
   1852 			mutex_enter(sc->sc_lock);
   1853 			mutex_enter(sc->sc_intr_lock);
   1854 			SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
   1855 			mutex_exit(sc->sc_intr_lock);
   1856 			mutex_exit(sc->sc_lock);
   1857 			error = mixer_close(sc, file);
   1858 			break;
   1859 		default:
   1860 			error = ENXIO;
   1861 			break;
   1862 		}
   1863 
   1864 		audio_sc_release(sc, &sc_ref);
   1865 	}
   1866 	curlwp_bindx(bound);
   1867 
   1868 	/* Free memory objects anyway */
   1869 	TRACEF(2, file, "free memory");
   1870 	if (file->ptrack)
   1871 		audio_track_destroy(file->ptrack);
   1872 	if (file->rtrack)
   1873 		audio_track_destroy(file->rtrack);
   1874 	kmem_free(file, sizeof(*file));
   1875 	fp->f_audioctx = NULL;
   1876 
   1877 	return error;
   1878 }
   1879 
   1880 static int
   1881 audioread(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1882 	int ioflag)
   1883 {
   1884 	struct audio_softc *sc;
   1885 	struct psref sc_ref;
   1886 	audio_file_t *file;
   1887 	int bound;
   1888 	int error;
   1889 	dev_t dev;
   1890 
   1891 	KASSERT(fp->f_audioctx);
   1892 	file = fp->f_audioctx;
   1893 	dev = file->dev;
   1894 
   1895 	bound = curlwp_bind();
   1896 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   1897 	if (sc == NULL) {
   1898 		error = EIO;
   1899 		goto done;
   1900 	}
   1901 
   1902 	if (fp->f_flag & O_NONBLOCK)
   1903 		ioflag |= IO_NDELAY;
   1904 
   1905 	switch (AUDIODEV(dev)) {
   1906 	case SOUND_DEVICE:
   1907 	case AUDIO_DEVICE:
   1908 		error = audio_read(sc, uio, ioflag, file);
   1909 		break;
   1910 	case AUDIOCTL_DEVICE:
   1911 	case MIXER_DEVICE:
   1912 		error = ENODEV;
   1913 		break;
   1914 	default:
   1915 		error = ENXIO;
   1916 		break;
   1917 	}
   1918 
   1919 	audio_sc_release(sc, &sc_ref);
   1920 done:
   1921 	curlwp_bindx(bound);
   1922 	return error;
   1923 }
   1924 
   1925 static int
   1926 audiowrite(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1927 	int ioflag)
   1928 {
   1929 	struct audio_softc *sc;
   1930 	struct psref sc_ref;
   1931 	audio_file_t *file;
   1932 	int bound;
   1933 	int error;
   1934 	dev_t dev;
   1935 
   1936 	KASSERT(fp->f_audioctx);
   1937 	file = fp->f_audioctx;
   1938 	dev = file->dev;
   1939 
   1940 	bound = curlwp_bind();
   1941 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   1942 	if (sc == NULL) {
   1943 		error = EIO;
   1944 		goto done;
   1945 	}
   1946 
   1947 	if (fp->f_flag & O_NONBLOCK)
   1948 		ioflag |= IO_NDELAY;
   1949 
   1950 	switch (AUDIODEV(dev)) {
   1951 	case SOUND_DEVICE:
   1952 	case AUDIO_DEVICE:
   1953 		error = audio_write(sc, uio, ioflag, file);
   1954 		break;
   1955 	case AUDIOCTL_DEVICE:
   1956 	case MIXER_DEVICE:
   1957 		error = ENODEV;
   1958 		break;
   1959 	default:
   1960 		error = ENXIO;
   1961 		break;
   1962 	}
   1963 
   1964 	audio_sc_release(sc, &sc_ref);
   1965 done:
   1966 	curlwp_bindx(bound);
   1967 	return error;
   1968 }
   1969 
   1970 static int
   1971 audioioctl(struct file *fp, u_long cmd, void *addr)
   1972 {
   1973 	struct audio_softc *sc;
   1974 	struct psref sc_ref;
   1975 	audio_file_t *file;
   1976 	struct lwp *l = curlwp;
   1977 	int bound;
   1978 	int error;
   1979 	dev_t dev;
   1980 
   1981 	KASSERT(fp->f_audioctx);
   1982 	file = fp->f_audioctx;
   1983 	dev = file->dev;
   1984 
   1985 	bound = curlwp_bind();
   1986 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   1987 	if (sc == NULL) {
   1988 		error = EIO;
   1989 		goto done;
   1990 	}
   1991 
   1992 	switch (AUDIODEV(dev)) {
   1993 	case SOUND_DEVICE:
   1994 	case AUDIO_DEVICE:
   1995 	case AUDIOCTL_DEVICE:
   1996 		mutex_enter(sc->sc_lock);
   1997 		device_active(sc->sc_dev, DVA_SYSTEM);
   1998 		mutex_exit(sc->sc_lock);
   1999 		if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ))
   2000 			error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   2001 		else
   2002 			error = audio_ioctl(dev, sc, cmd, addr, fp->f_flag, l,
   2003 			    file);
   2004 		break;
   2005 	case MIXER_DEVICE:
   2006 		error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   2007 		break;
   2008 	default:
   2009 		error = ENXIO;
   2010 		break;
   2011 	}
   2012 
   2013 	audio_sc_release(sc, &sc_ref);
   2014 done:
   2015 	curlwp_bindx(bound);
   2016 	return error;
   2017 }
   2018 
   2019 static int
   2020 audiostat(struct file *fp, struct stat *st)
   2021 {
   2022 	struct audio_softc *sc;
   2023 	struct psref sc_ref;
   2024 	audio_file_t *file;
   2025 	int bound;
   2026 	int error;
   2027 
   2028 	KASSERT(fp->f_audioctx);
   2029 	file = fp->f_audioctx;
   2030 
   2031 	bound = curlwp_bind();
   2032 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2033 	if (sc == NULL) {
   2034 		error = EIO;
   2035 		goto done;
   2036 	}
   2037 
   2038 	error = 0;
   2039 	memset(st, 0, sizeof(*st));
   2040 
   2041 	st->st_dev = file->dev;
   2042 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   2043 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   2044 	st->st_mode = S_IFCHR;
   2045 
   2046 	audio_sc_release(sc, &sc_ref);
   2047 done:
   2048 	curlwp_bindx(bound);
   2049 	return error;
   2050 }
   2051 
   2052 static int
   2053 audiopoll(struct file *fp, int events)
   2054 {
   2055 	struct audio_softc *sc;
   2056 	struct psref sc_ref;
   2057 	audio_file_t *file;
   2058 	struct lwp *l = curlwp;
   2059 	int bound;
   2060 	int revents;
   2061 	dev_t dev;
   2062 
   2063 	KASSERT(fp->f_audioctx);
   2064 	file = fp->f_audioctx;
   2065 	dev = file->dev;
   2066 
   2067 	bound = curlwp_bind();
   2068 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2069 	if (sc == NULL) {
   2070 		revents = POLLERR;
   2071 		goto done;
   2072 	}
   2073 
   2074 	switch (AUDIODEV(dev)) {
   2075 	case SOUND_DEVICE:
   2076 	case AUDIO_DEVICE:
   2077 		revents = audio_poll(sc, events, l, file);
   2078 		break;
   2079 	case AUDIOCTL_DEVICE:
   2080 	case MIXER_DEVICE:
   2081 		revents = 0;
   2082 		break;
   2083 	default:
   2084 		revents = POLLERR;
   2085 		break;
   2086 	}
   2087 
   2088 	audio_sc_release(sc, &sc_ref);
   2089 done:
   2090 	curlwp_bindx(bound);
   2091 	return revents;
   2092 }
   2093 
   2094 static int
   2095 audiokqfilter(struct file *fp, struct knote *kn)
   2096 {
   2097 	struct audio_softc *sc;
   2098 	struct psref sc_ref;
   2099 	audio_file_t *file;
   2100 	dev_t dev;
   2101 	int bound;
   2102 	int error;
   2103 
   2104 	KASSERT(fp->f_audioctx);
   2105 	file = fp->f_audioctx;
   2106 	dev = file->dev;
   2107 
   2108 	bound = curlwp_bind();
   2109 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2110 	if (sc == NULL) {
   2111 		error = EIO;
   2112 		goto done;
   2113 	}
   2114 
   2115 	switch (AUDIODEV(dev)) {
   2116 	case SOUND_DEVICE:
   2117 	case AUDIO_DEVICE:
   2118 		error = audio_kqfilter(sc, file, kn);
   2119 		break;
   2120 	case AUDIOCTL_DEVICE:
   2121 	case MIXER_DEVICE:
   2122 		error = ENODEV;
   2123 		break;
   2124 	default:
   2125 		error = ENXIO;
   2126 		break;
   2127 	}
   2128 
   2129 	audio_sc_release(sc, &sc_ref);
   2130 done:
   2131 	curlwp_bindx(bound);
   2132 	return error;
   2133 }
   2134 
   2135 static int
   2136 audiommap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
   2137 	int *advicep, struct uvm_object **uobjp, int *maxprotp)
   2138 {
   2139 	struct audio_softc *sc;
   2140 	struct psref sc_ref;
   2141 	audio_file_t *file;
   2142 	dev_t dev;
   2143 	int bound;
   2144 	int error;
   2145 
   2146 	KASSERT(len > 0);
   2147 
   2148 	KASSERT(fp->f_audioctx);
   2149 	file = fp->f_audioctx;
   2150 	dev = file->dev;
   2151 
   2152 	bound = curlwp_bind();
   2153 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2154 	if (sc == NULL) {
   2155 		error = EIO;
   2156 		goto done;
   2157 	}
   2158 
   2159 	mutex_enter(sc->sc_lock);
   2160 	device_active(sc->sc_dev, DVA_SYSTEM); /* XXXJDM */
   2161 	mutex_exit(sc->sc_lock);
   2162 
   2163 	switch (AUDIODEV(dev)) {
   2164 	case SOUND_DEVICE:
   2165 	case AUDIO_DEVICE:
   2166 		error = audio_mmap(sc, offp, len, prot, flagsp, advicep,
   2167 		    uobjp, maxprotp, file);
   2168 		break;
   2169 	case AUDIOCTL_DEVICE:
   2170 	case MIXER_DEVICE:
   2171 	default:
   2172 		error = ENOTSUP;
   2173 		break;
   2174 	}
   2175 
   2176 	audio_sc_release(sc, &sc_ref);
   2177 done:
   2178 	curlwp_bindx(bound);
   2179 	return error;
   2180 }
   2181 
   2182 
   2183 /* Exported interfaces for audiobell. */
   2184 
   2185 /*
   2186  * Open for audiobell.
   2187  * It stores allocated file to *filep.
   2188  * If successful returns 0, otherwise errno.
   2189  */
   2190 int
   2191 audiobellopen(dev_t dev, audio_file_t **filep)
   2192 {
   2193 	device_t audiodev = NULL;
   2194 	struct audio_softc *sc;
   2195 	bool exlock = false;
   2196 	int error;
   2197 
   2198 	/*
   2199 	 * Find the autoconf instance and make sure it doesn't go away
   2200 	 * while we are opening it.
   2201 	 */
   2202 	audiodev = device_lookup_acquire(&audio_cd, AUDIOUNIT(dev));
   2203 	if (audiodev == NULL) {
   2204 		error = ENXIO;
   2205 		goto out;
   2206 	}
   2207 
   2208 	/* If attach failed, it's hopeless -- give up.  */
   2209 	sc = device_private(audiodev);
   2210 	if (sc->hw_if == NULL) {
   2211 		error = ENXIO;
   2212 		goto out;
   2213 	}
   2214 
   2215 	/* Take the exclusive configuration lock.  */
   2216 	error = audio_exlock_enter(sc);
   2217 	if (error)
   2218 		goto out;
   2219 	exlock = true;
   2220 
   2221 	/* Open the audio device.  */
   2222 	device_active(sc->sc_dev, DVA_SYSTEM);
   2223 	error = audio_open(dev, sc, FWRITE, 0, curlwp, filep);
   2224 
   2225 out:	if (exlock)
   2226 		audio_exlock_exit(sc);
   2227 	if (audiodev)
   2228 		device_release(audiodev);
   2229 	return error;
   2230 }
   2231 
   2232 /* Close for audiobell */
   2233 int
   2234 audiobellclose(audio_file_t *file)
   2235 {
   2236 	struct audio_softc *sc;
   2237 	struct psref sc_ref;
   2238 	int bound;
   2239 	int error;
   2240 
   2241 	error = 0;
   2242 	/*
   2243 	 * audiobellclose() must
   2244 	 * - unplug track from the trackmixer if sc exist.
   2245 	 * - free all memory objects, regardless of sc.
   2246 	 */
   2247 	bound = curlwp_bind();
   2248 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2249 	if (sc) {
   2250 		error = audio_close(sc, file);
   2251 		audio_sc_release(sc, &sc_ref);
   2252 	}
   2253 	curlwp_bindx(bound);
   2254 
   2255 	/* Free memory objects anyway */
   2256 	KASSERT(file->ptrack);
   2257 	audio_track_destroy(file->ptrack);
   2258 	KASSERT(file->rtrack == NULL);
   2259 	kmem_free(file, sizeof(*file));
   2260 	return error;
   2261 }
   2262 
   2263 /* Set sample rate for audiobell */
   2264 int
   2265 audiobellsetrate(audio_file_t *file, u_int sample_rate)
   2266 {
   2267 	struct audio_softc *sc;
   2268 	struct psref sc_ref;
   2269 	struct audio_info ai;
   2270 	int bound;
   2271 	int error;
   2272 
   2273 	bound = curlwp_bind();
   2274 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2275 	if (sc == NULL) {
   2276 		error = EIO;
   2277 		goto done1;
   2278 	}
   2279 
   2280 	AUDIO_INITINFO(&ai);
   2281 	ai.play.sample_rate = sample_rate;
   2282 
   2283 	error = audio_exlock_enter(sc);
   2284 	if (error)
   2285 		goto done2;
   2286 	error = audio_file_setinfo(sc, file, &ai);
   2287 	audio_exlock_exit(sc);
   2288 
   2289 done2:
   2290 	audio_sc_release(sc, &sc_ref);
   2291 done1:
   2292 	curlwp_bindx(bound);
   2293 	return error;
   2294 }
   2295 
   2296 /* Playback for audiobell */
   2297 int
   2298 audiobellwrite(audio_file_t *file, struct uio *uio)
   2299 {
   2300 	struct audio_softc *sc;
   2301 	struct psref sc_ref;
   2302 	int bound;
   2303 	int error;
   2304 
   2305 	bound = curlwp_bind();
   2306 	sc = audio_sc_acquire_fromfile(file, &sc_ref);
   2307 	if (sc == NULL) {
   2308 		error = EIO;
   2309 		goto done;
   2310 	}
   2311 
   2312 	error = audio_write(sc, uio, 0, file);
   2313 
   2314 	audio_sc_release(sc, &sc_ref);
   2315 done:
   2316 	curlwp_bindx(bound);
   2317 	return error;
   2318 }
   2319 
   2320 
   2321 /*
   2322  * Audio driver
   2323  */
   2324 
   2325 /*
   2326  * Must be called with sc_exlock held and without sc_lock held.
   2327  */
   2328 int
   2329 audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   2330 	struct lwp *l, audio_file_t **bellfile)
   2331 {
   2332 	struct audio_info ai;
   2333 	struct file *fp;
   2334 	audio_file_t *af;
   2335 	audio_ring_t *hwbuf;
   2336 	bool fullduplex;
   2337 	bool cred_held;
   2338 	bool hw_opened;
   2339 	bool rmixer_started;
   2340 	bool inserted;
   2341 	int fd;
   2342 	int error;
   2343 
   2344 	KASSERT(sc->sc_exlock);
   2345 
   2346 	TRACE(1, "%sdev=%s flags=0x%x po=%d ro=%d",
   2347 	    (audiodebug >= 3) ? "start " : "",
   2348 	    ISDEVSOUND(dev) ? "sound" : "audio",
   2349 	    flags, sc->sc_popens, sc->sc_ropens);
   2350 
   2351 	fp = NULL;
   2352 	cred_held = false;
   2353 	hw_opened = false;
   2354 	rmixer_started = false;
   2355 	inserted = false;
   2356 
   2357 	af = kmem_zalloc(sizeof(*af), KM_SLEEP);
   2358 	af->sc = sc;
   2359 	af->dev = dev;
   2360 	if ((flags & FWRITE) != 0 && audio_can_playback(sc))
   2361 		af->mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
   2362 	if ((flags & FREAD) != 0 && audio_can_capture(sc))
   2363 		af->mode |= AUMODE_RECORD;
   2364 	if (af->mode == 0) {
   2365 		error = ENXIO;
   2366 		goto bad;
   2367 	}
   2368 
   2369 	fullduplex = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
   2370 
   2371 	/*
   2372 	 * On half duplex hardware,
   2373 	 * 1. if mode is (PLAY | REC), let mode PLAY.
   2374 	 * 2. if mode is PLAY, let mode PLAY if no rec tracks, otherwise error.
   2375 	 * 3. if mode is REC, let mode REC if no play tracks, otherwise error.
   2376 	 */
   2377 	if (fullduplex == false) {
   2378 		if ((af->mode & AUMODE_PLAY)) {
   2379 			if (sc->sc_ropens != 0) {
   2380 				TRACE(1, "record track already exists");
   2381 				error = ENODEV;
   2382 				goto bad;
   2383 			}
   2384 			/* Play takes precedence */
   2385 			af->mode &= ~AUMODE_RECORD;
   2386 		}
   2387 		if ((af->mode & AUMODE_RECORD)) {
   2388 			if (sc->sc_popens != 0) {
   2389 				TRACE(1, "play track already exists");
   2390 				error = ENODEV;
   2391 				goto bad;
   2392 			}
   2393 		}
   2394 	}
   2395 
   2396 	/* Create tracks */
   2397 	if ((af->mode & AUMODE_PLAY))
   2398 		af->ptrack = audio_track_create(sc, sc->sc_pmixer);
   2399 	if ((af->mode & AUMODE_RECORD))
   2400 		af->rtrack = audio_track_create(sc, sc->sc_rmixer);
   2401 
   2402 	/* Set parameters */
   2403 	AUDIO_INITINFO(&ai);
   2404 	if (bellfile) {
   2405 		/* If audiobell, only sample_rate will be set later. */
   2406 		ai.play.sample_rate   = audio_default.sample_rate;
   2407 		ai.play.encoding      = AUDIO_ENCODING_SLINEAR_NE;
   2408 		ai.play.channels      = 1;
   2409 		ai.play.precision     = 16;
   2410 		ai.play.pause         = 0;
   2411 	} else if (ISDEVAUDIO(dev)) {
   2412 		/* If /dev/audio, initialize everytime. */
   2413 		ai.play.sample_rate   = audio_default.sample_rate;
   2414 		ai.play.encoding      = audio_default.encoding;
   2415 		ai.play.channels      = audio_default.channels;
   2416 		ai.play.precision     = audio_default.precision;
   2417 		ai.play.pause         = 0;
   2418 		ai.record.sample_rate = audio_default.sample_rate;
   2419 		ai.record.encoding    = audio_default.encoding;
   2420 		ai.record.channels    = audio_default.channels;
   2421 		ai.record.precision   = audio_default.precision;
   2422 		ai.record.pause       = 0;
   2423 	} else {
   2424 		/* If /dev/sound, take over the previous parameters. */
   2425 		ai.play.sample_rate   = sc->sc_sound_pparams.sample_rate;
   2426 		ai.play.encoding      = sc->sc_sound_pparams.encoding;
   2427 		ai.play.channels      = sc->sc_sound_pparams.channels;
   2428 		ai.play.precision     = sc->sc_sound_pparams.precision;
   2429 		ai.play.pause         = sc->sc_sound_ppause;
   2430 		ai.record.sample_rate = sc->sc_sound_rparams.sample_rate;
   2431 		ai.record.encoding    = sc->sc_sound_rparams.encoding;
   2432 		ai.record.channels    = sc->sc_sound_rparams.channels;
   2433 		ai.record.precision   = sc->sc_sound_rparams.precision;
   2434 		ai.record.pause       = sc->sc_sound_rpause;
   2435 	}
   2436 	error = audio_file_setinfo(sc, af, &ai);
   2437 	if (error)
   2438 		goto bad;
   2439 
   2440 	if (sc->sc_popens + sc->sc_ropens == 0) {
   2441 		/* First open */
   2442 
   2443 		sc->sc_cred = kauth_cred_get();
   2444 		kauth_cred_hold(sc->sc_cred);
   2445 		cred_held = true;
   2446 
   2447 		if (sc->hw_if->open) {
   2448 			int hwflags;
   2449 
   2450 			/*
   2451 			 * Call hw_if->open() only at first open of
   2452 			 * combination of playback and recording.
   2453 			 * On full duplex hardware, the flags passed to
   2454 			 * hw_if->open() is always (FREAD | FWRITE)
   2455 			 * regardless of this open()'s flags.
   2456 			 * see also dev/isa/aria.c
   2457 			 * On half duplex hardware, the flags passed to
   2458 			 * hw_if->open() is either FREAD or FWRITE.
   2459 			 * see also arch/evbarm/mini2440/audio_mini2440.c
   2460 			 */
   2461 			if (fullduplex) {
   2462 				hwflags = FREAD | FWRITE;
   2463 			} else {
   2464 				/* Construct hwflags from af->mode. */
   2465 				hwflags = 0;
   2466 				if ((af->mode & AUMODE_PLAY) != 0)
   2467 					hwflags |= FWRITE;
   2468 				if ((af->mode & AUMODE_RECORD) != 0)
   2469 					hwflags |= FREAD;
   2470 			}
   2471 
   2472 			mutex_enter(sc->sc_lock);
   2473 			mutex_enter(sc->sc_intr_lock);
   2474 			error = sc->hw_if->open(sc->hw_hdl, hwflags);
   2475 			mutex_exit(sc->sc_intr_lock);
   2476 			mutex_exit(sc->sc_lock);
   2477 			if (error)
   2478 				goto bad;
   2479 		}
   2480 		/*
   2481 		 * Regardless of whether we called hw_if->open (whether
   2482 		 * hw_if->open exists) or not, we move to the Opened phase
   2483 		 * here.  Therefore from this point, we have to call
   2484 		 * hw_if->close (if exists) whenever abort.
   2485 		 * Note that both of hw_if->{open,close} are optional.
   2486 		 */
   2487 		hw_opened = true;
   2488 
   2489 		/*
   2490 		 * Set speaker mode when a half duplex.
   2491 		 * XXX I'm not sure this is correct.
   2492 		 */
   2493 		if (1/*XXX*/) {
   2494 			if (sc->hw_if->speaker_ctl) {
   2495 				int on;
   2496 				if (af->ptrack) {
   2497 					on = 1;
   2498 				} else {
   2499 					on = 0;
   2500 				}
   2501 				mutex_enter(sc->sc_lock);
   2502 				mutex_enter(sc->sc_intr_lock);
   2503 				error = sc->hw_if->speaker_ctl(sc->hw_hdl, on);
   2504 				mutex_exit(sc->sc_intr_lock);
   2505 				mutex_exit(sc->sc_lock);
   2506 				if (error)
   2507 					goto bad;
   2508 			}
   2509 		}
   2510 	} else if (sc->sc_multiuser == false) {
   2511 		uid_t euid = kauth_cred_geteuid(kauth_cred_get());
   2512 		if (euid != 0 && euid != kauth_cred_geteuid(sc->sc_cred)) {
   2513 			error = EPERM;
   2514 			goto bad;
   2515 		}
   2516 	}
   2517 
   2518 	/* Call init_output if this is the first playback open. */
   2519 	if (af->ptrack && sc->sc_popens == 0) {
   2520 		if (sc->hw_if->init_output) {
   2521 			hwbuf = &sc->sc_pmixer->hwbuf;
   2522 			mutex_enter(sc->sc_lock);
   2523 			mutex_enter(sc->sc_intr_lock);
   2524 			error = sc->hw_if->init_output(sc->hw_hdl,
   2525 			    hwbuf->mem,
   2526 			    hwbuf->capacity *
   2527 			    hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
   2528 			mutex_exit(sc->sc_intr_lock);
   2529 			mutex_exit(sc->sc_lock);
   2530 			if (error)
   2531 				goto bad;
   2532 		}
   2533 	}
   2534 	/*
   2535 	 * Call init_input and start rmixer, if this is the first recording
   2536 	 * open.  See pause consideration notes.
   2537 	 */
   2538 	if (af->rtrack && sc->sc_ropens == 0) {
   2539 		if (sc->hw_if->init_input) {
   2540 			hwbuf = &sc->sc_rmixer->hwbuf;
   2541 			mutex_enter(sc->sc_lock);
   2542 			mutex_enter(sc->sc_intr_lock);
   2543 			error = sc->hw_if->init_input(sc->hw_hdl,
   2544 			    hwbuf->mem,
   2545 			    hwbuf->capacity *
   2546 			    hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
   2547 			mutex_exit(sc->sc_intr_lock);
   2548 			mutex_exit(sc->sc_lock);
   2549 			if (error)
   2550 				goto bad;
   2551 		}
   2552 
   2553 		mutex_enter(sc->sc_lock);
   2554 		audio_rmixer_start(sc);
   2555 		mutex_exit(sc->sc_lock);
   2556 		rmixer_started = true;
   2557 	}
   2558 
   2559 	/*
   2560 	 * This is the last sc_lock section in the function, so we have to
   2561 	 * examine sc_dying again before starting the rest tasks.  Because
   2562 	 * audiodeatch() may have been invoked (and it would set sc_dying)
   2563 	 * from the time audioopen() was executed until now.  If it happens,
   2564 	 * audiodetach() may already have set file->dying for all sc_files
   2565 	 * that exist at that point, so that audioopen() must abort without
   2566 	 * inserting af to sc_files, in order to keep consistency.
   2567 	 */
   2568 	mutex_enter(sc->sc_lock);
   2569 	if (sc->sc_dying) {
   2570 		mutex_exit(sc->sc_lock);
   2571 		error = ENXIO;
   2572 		goto bad;
   2573 	}
   2574 
   2575 	/* Count up finally */
   2576 	if (af->ptrack)
   2577 		sc->sc_popens++;
   2578 	if (af->rtrack)
   2579 		sc->sc_ropens++;
   2580 	mutex_enter(sc->sc_intr_lock);
   2581 	SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
   2582 	mutex_exit(sc->sc_intr_lock);
   2583 	mutex_exit(sc->sc_lock);
   2584 	inserted = true;
   2585 
   2586 	if (bellfile) {
   2587 		*bellfile = af;
   2588 	} else {
   2589 		error = fd_allocfile(&fp, &fd);
   2590 		if (error)
   2591 			goto bad;
   2592 
   2593 		error = fd_clone(fp, fd, flags, &audio_fileops, af);
   2594 		KASSERTMSG(error == EMOVEFD, "error=%d", error);
   2595 	}
   2596 
   2597 	/* Be nothing else after fd_clone */
   2598 
   2599 	TRACEF(3, af, "done");
   2600 	return error;
   2601 
   2602 bad:
   2603 	if (inserted) {
   2604 		mutex_enter(sc->sc_lock);
   2605 		mutex_enter(sc->sc_intr_lock);
   2606 		SLIST_REMOVE(&sc->sc_files, af, audio_file, entry);
   2607 		mutex_exit(sc->sc_intr_lock);
   2608 		if (af->ptrack)
   2609 			sc->sc_popens--;
   2610 		if (af->rtrack)
   2611 			sc->sc_ropens--;
   2612 		mutex_exit(sc->sc_lock);
   2613 	}
   2614 
   2615 	if (rmixer_started) {
   2616 		mutex_enter(sc->sc_lock);
   2617 		audio_rmixer_halt(sc);
   2618 		mutex_exit(sc->sc_lock);
   2619 	}
   2620 
   2621 	if (hw_opened) {
   2622 		if (sc->hw_if->close) {
   2623 			mutex_enter(sc->sc_lock);
   2624 			mutex_enter(sc->sc_intr_lock);
   2625 			sc->hw_if->close(sc->hw_hdl);
   2626 			mutex_exit(sc->sc_intr_lock);
   2627 			mutex_exit(sc->sc_lock);
   2628 		}
   2629 	}
   2630 	if (cred_held) {
   2631 		kauth_cred_free(sc->sc_cred);
   2632 	}
   2633 
   2634 	/*
   2635 	 * Since track here is not yet linked to sc_files,
   2636 	 * you can call track_destroy() without sc_intr_lock.
   2637 	 */
   2638 	if (af->rtrack) {
   2639 		audio_track_destroy(af->rtrack);
   2640 		af->rtrack = NULL;
   2641 	}
   2642 	if (af->ptrack) {
   2643 		audio_track_destroy(af->ptrack);
   2644 		af->ptrack = NULL;
   2645 	}
   2646 
   2647 	kmem_free(af, sizeof(*af));
   2648 	return error;
   2649 }
   2650 
   2651 /*
   2652  * Must be called without sc_lock nor sc_exlock held.
   2653  */
   2654 int
   2655 audio_close(struct audio_softc *sc, audio_file_t *file)
   2656 {
   2657 	int error;
   2658 
   2659 	/*
   2660 	 * Drain first.
   2661 	 * It must be done before unlinking(acquiring exlock).
   2662 	 */
   2663 	if (file->ptrack) {
   2664 		mutex_enter(sc->sc_lock);
   2665 		audio_track_drain(sc, file->ptrack);
   2666 		mutex_exit(sc->sc_lock);
   2667 	}
   2668 
   2669 	mutex_enter(sc->sc_lock);
   2670 	mutex_enter(sc->sc_intr_lock);
   2671 	SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
   2672 	mutex_exit(sc->sc_intr_lock);
   2673 	mutex_exit(sc->sc_lock);
   2674 
   2675 	error = audio_exlock_enter(sc);
   2676 	if (error) {
   2677 		/*
   2678 		 * If EIO, this sc is about to detach.  In this case, even if
   2679 		 * we don't do subsequent _unlink(), audiodetach() will do it.
   2680 		 */
   2681 		if (error == EIO)
   2682 			return error;
   2683 
   2684 		/* XXX This should not happen but what should I do ? */
   2685 		panic("%s: can't acquire exlock: errno=%d", __func__, error);
   2686 	}
   2687 	audio_unlink(sc, file);
   2688 	audio_exlock_exit(sc);
   2689 
   2690 	return 0;
   2691 }
   2692 
   2693 /*
   2694  * Unlink this file, but not freeing memory here.
   2695  * Must be called with sc_exlock held and without sc_lock held.
   2696  */
   2697 static void
   2698 audio_unlink(struct audio_softc *sc, audio_file_t *file)
   2699 {
   2700 	kauth_cred_t cred = NULL;
   2701 	int error;
   2702 
   2703 	mutex_enter(sc->sc_lock);
   2704 
   2705 	TRACEF(1, file, "%spid=%d.%d po=%d ro=%d",
   2706 	    (audiodebug >= 3) ? "start " : "",
   2707 	    (int)curproc->p_pid, (int)curlwp->l_lid,
   2708 	    sc->sc_popens, sc->sc_ropens);
   2709 	KASSERTMSG(sc->sc_popens + sc->sc_ropens > 0,
   2710 	    "sc->sc_popens=%d, sc->sc_ropens=%d",
   2711 	    sc->sc_popens, sc->sc_ropens);
   2712 
   2713 	device_active(sc->sc_dev, DVA_SYSTEM);
   2714 
   2715 	if (file->ptrack) {
   2716 		TRACET(3, file->ptrack, "dropframes=%" PRIu64,
   2717 		    file->ptrack->dropframes);
   2718 
   2719 		KASSERT(sc->sc_popens > 0);
   2720 		sc->sc_popens--;
   2721 
   2722 		/* Call hw halt_output if this is the last playback track. */
   2723 		if (sc->sc_popens == 0 && sc->sc_pbusy) {
   2724 			error = audio_pmixer_halt(sc);
   2725 			if (error) {
   2726 				audio_printf(sc,
   2727 				    "halt_output failed: errno=%d (ignored)\n",
   2728 				    error);
   2729 			}
   2730 		}
   2731 
   2732 		/* Restore mixing volume if all tracks are gone. */
   2733 		if (sc->sc_popens == 0) {
   2734 			/* intr_lock is not necessary, but just manners. */
   2735 			mutex_enter(sc->sc_intr_lock);
   2736 			sc->sc_pmixer->volume = 256;
   2737 			sc->sc_pmixer->voltimer = 0;
   2738 			mutex_exit(sc->sc_intr_lock);
   2739 		}
   2740 	}
   2741 	if (file->rtrack) {
   2742 		TRACET(3, file->rtrack, "dropframes=%" PRIu64,
   2743 		    file->rtrack->dropframes);
   2744 
   2745 		KASSERT(sc->sc_ropens > 0);
   2746 		sc->sc_ropens--;
   2747 
   2748 		/* Call hw halt_input if this is the last recording track. */
   2749 		if (sc->sc_ropens == 0 && sc->sc_rbusy) {
   2750 			error = audio_rmixer_halt(sc);
   2751 			if (error) {
   2752 				audio_printf(sc,
   2753 				    "halt_input failed: errno=%d (ignored)\n",
   2754 				    error);
   2755 			}
   2756 		}
   2757 
   2758 	}
   2759 
   2760 	/* Call hw close if this is the last track. */
   2761 	if (sc->sc_popens + sc->sc_ropens == 0) {
   2762 		if (sc->hw_if->close) {
   2763 			TRACE(2, "hw_if close");
   2764 			mutex_enter(sc->sc_intr_lock);
   2765 			sc->hw_if->close(sc->hw_hdl);
   2766 			mutex_exit(sc->sc_intr_lock);
   2767 		}
   2768 		cred = sc->sc_cred;
   2769 		sc->sc_cred = NULL;
   2770 	}
   2771 
   2772 	mutex_exit(sc->sc_lock);
   2773 	if (cred)
   2774 		kauth_cred_free(cred);
   2775 
   2776 	TRACE(3, "done");
   2777 }
   2778 
   2779 /*
   2780  * Must be called without sc_lock nor sc_exlock held.
   2781  */
   2782 int
   2783 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag,
   2784 	audio_file_t *file)
   2785 {
   2786 	audio_track_t *track;
   2787 	audio_ring_t *usrbuf;
   2788 	audio_ring_t *input;
   2789 	int error;
   2790 
   2791 	/*
   2792 	 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
   2793 	 * However read() system call itself can be called because it's
   2794 	 * opened with O_RDWR.  So in this case, deny this read().
   2795 	 */
   2796 	track = file->rtrack;
   2797 	if (track == NULL) {
   2798 		return EBADF;
   2799 	}
   2800 
   2801 	/* I think it's better than EINVAL. */
   2802 	if (track->mmapped)
   2803 		return EPERM;
   2804 
   2805 	TRACET(2, track, "resid=%zd ioflag=0x%x", uio->uio_resid, ioflag);
   2806 
   2807 #ifdef AUDIO_PM_IDLE
   2808 	error = audio_exlock_mutex_enter(sc);
   2809 	if (error)
   2810 		return error;
   2811 
   2812 	if (device_is_active(&sc->sc_dev) || sc->sc_idle)
   2813 		device_active(&sc->sc_dev, DVA_SYSTEM);
   2814 
   2815 	/* In recording, unlike playback, read() never operates rmixer. */
   2816 
   2817 	audio_exlock_mutex_exit(sc);
   2818 #endif
   2819 
   2820 	usrbuf = &track->usrbuf;
   2821 	input = track->input;
   2822 	error = 0;
   2823 
   2824 	while (uio->uio_resid > 0 && error == 0) {
   2825 		int bytes;
   2826 
   2827 		TRACET(3, track,
   2828 		    "while resid=%zd input=%d/%d/%d usrbuf=%d/%d/C%d",
   2829 		    uio->uio_resid,
   2830 		    input->head, input->used, input->capacity,
   2831 		    usrbuf->head, usrbuf->used, usrbuf->capacity);
   2832 
   2833 		/* Wait when buffers are empty. */
   2834 		mutex_enter(sc->sc_lock);
   2835 		for (;;) {
   2836 			bool empty;
   2837 			audio_track_lock_enter(track);
   2838 			empty = (input->used == 0 && usrbuf->used == 0);
   2839 			audio_track_lock_exit(track);
   2840 			if (!empty)
   2841 				break;
   2842 
   2843 			if ((ioflag & IO_NDELAY)) {
   2844 				mutex_exit(sc->sc_lock);
   2845 				return EWOULDBLOCK;
   2846 			}
   2847 
   2848 			TRACET(3, track, "sleep");
   2849 			error = audio_track_waitio(sc, track, "audio_read");
   2850 			if (error) {
   2851 				mutex_exit(sc->sc_lock);
   2852 				return error;
   2853 			}
   2854 		}
   2855 		mutex_exit(sc->sc_lock);
   2856 
   2857 		audio_track_lock_enter(track);
   2858 		/* Convert one block if possible. */
   2859 		if (usrbuf->used == 0 && input->used > 0) {
   2860 			audio_track_record(track);
   2861 		}
   2862 
   2863 		/* uiomove from usrbuf as many bytes as possible. */
   2864 		bytes = uimin(usrbuf->used, uio->uio_resid);
   2865 		error = uiomove((uint8_t *)usrbuf->mem + usrbuf->head, bytes,
   2866 		    uio);
   2867 		if (error) {
   2868 			audio_track_lock_exit(track);
   2869 			device_printf(sc->sc_dev,
   2870 			    "%s: uiomove(%d) failed: errno=%d\n",
   2871 			    __func__, bytes, error);
   2872 			goto abort;
   2873 		}
   2874 		auring_take(usrbuf, bytes);
   2875 		TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
   2876 		    bytes,
   2877 		    usrbuf->head, usrbuf->used, usrbuf->capacity);
   2878 
   2879 		audio_track_lock_exit(track);
   2880 	}
   2881 
   2882 abort:
   2883 	return error;
   2884 }
   2885 
   2886 
   2887 /*
   2888  * Clear file's playback and/or record track buffer immediately.
   2889  */
   2890 static void
   2891 audio_file_clear(struct audio_softc *sc, audio_file_t *file)
   2892 {
   2893 
   2894 	if (file->ptrack)
   2895 		audio_track_clear(sc, file->ptrack);
   2896 	if (file->rtrack)
   2897 		audio_track_clear(sc, file->rtrack);
   2898 }
   2899 
   2900 /*
   2901  * Must be called without sc_lock nor sc_exlock held.
   2902  */
   2903 int
   2904 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag,
   2905 	audio_file_t *file)
   2906 {
   2907 	audio_track_t *track;
   2908 	audio_ring_t *usrbuf;
   2909 	audio_ring_t *outbuf;
   2910 	int error;
   2911 
   2912 	track = file->ptrack;
   2913 	if (track == NULL)
   2914 		return EPERM;
   2915 
   2916 	/* I think it's better than EINVAL. */
   2917 	if (track->mmapped)
   2918 		return EPERM;
   2919 
   2920 	TRACET(2, track, "%sresid=%zd pid=%d.%d ioflag=0x%x",
   2921 	    audiodebug >= 3 ? "begin " : "",
   2922 	    uio->uio_resid, (int)curproc->p_pid, (int)curlwp->l_lid, ioflag);
   2923 
   2924 	if (uio->uio_resid == 0) {
   2925 		track->eofcounter++;
   2926 		return 0;
   2927 	}
   2928 
   2929 	error = audio_exlock_mutex_enter(sc);
   2930 	if (error)
   2931 		return error;
   2932 
   2933 #ifdef AUDIO_PM_IDLE
   2934 	if (device_is_active(&sc->sc_dev) || sc->sc_idle)
   2935 		device_active(&sc->sc_dev, DVA_SYSTEM);
   2936 #endif
   2937 
   2938 	/*
   2939 	 * The first write starts pmixer.
   2940 	 */
   2941 	if (sc->sc_pbusy == false)
   2942 		audio_pmixer_start(sc, false);
   2943 	audio_exlock_mutex_exit(sc);
   2944 
   2945 	usrbuf = &track->usrbuf;
   2946 	outbuf = &track->outbuf;
   2947 	track->pstate = AUDIO_STATE_RUNNING;
   2948 	error = 0;
   2949 
   2950 	while (uio->uio_resid > 0 && error == 0) {
   2951 		int bytes;
   2952 
   2953 		TRACET(3, track, "while resid=%zd usrbuf=%d/%d/H%d",
   2954 		    uio->uio_resid,
   2955 		    usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
   2956 
   2957 		/* Wait when buffers are full. */
   2958 		mutex_enter(sc->sc_lock);
   2959 		for (;;) {
   2960 			bool full;
   2961 			audio_track_lock_enter(track);
   2962 			full = (usrbuf->used >= track->usrbuf_usedhigh &&
   2963 			    outbuf->used >= outbuf->capacity);
   2964 			audio_track_lock_exit(track);
   2965 			if (!full)
   2966 				break;
   2967 
   2968 			if ((ioflag & IO_NDELAY)) {
   2969 				error = EWOULDBLOCK;
   2970 				mutex_exit(sc->sc_lock);
   2971 				goto abort;
   2972 			}
   2973 
   2974 			TRACET(3, track, "sleep usrbuf=%d/H%d",
   2975 			    usrbuf->used, track->usrbuf_usedhigh);
   2976 			error = audio_track_waitio(sc, track, "audio_write");
   2977 			if (error) {
   2978 				mutex_exit(sc->sc_lock);
   2979 				goto abort;
   2980 			}
   2981 		}
   2982 		mutex_exit(sc->sc_lock);
   2983 
   2984 		audio_track_lock_enter(track);
   2985 
   2986 		/* uiomove to usrbuf as many bytes as possible. */
   2987 		bytes = uimin(track->usrbuf_usedhigh - usrbuf->used,
   2988 		    uio->uio_resid);
   2989 		while (bytes > 0) {
   2990 			int tail = auring_tail(usrbuf);
   2991 			int len = uimin(bytes, usrbuf->capacity - tail);
   2992 			error = uiomove((uint8_t *)usrbuf->mem + tail, len,
   2993 			    uio);
   2994 			if (error) {
   2995 				audio_track_lock_exit(track);
   2996 				device_printf(sc->sc_dev,
   2997 				    "%s: uiomove(%d) failed: errno=%d\n",
   2998 				    __func__, len, error);
   2999 				goto abort;
   3000 			}
   3001 			auring_push(usrbuf, len);
   3002 			TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
   3003 			    len,
   3004 			    usrbuf->head, usrbuf->used, usrbuf->capacity);
   3005 			bytes -= len;
   3006 		}
   3007 
   3008 		/* Convert them as many blocks as possible. */
   3009 		while (usrbuf->used >= track->usrbuf_blksize &&
   3010 		    outbuf->used < outbuf->capacity) {
   3011 			audio_track_play(track);
   3012 		}
   3013 
   3014 		audio_track_lock_exit(track);
   3015 	}
   3016 
   3017 abort:
   3018 	TRACET(3, track, "done error=%d", error);
   3019 	return error;
   3020 }
   3021 
   3022 /*
   3023  * Must be called without sc_lock nor sc_exlock held.
   3024  */
   3025 int
   3026 audio_ioctl(dev_t dev, struct audio_softc *sc, u_long cmd, void *addr, int flag,
   3027 	struct lwp *l, audio_file_t *file)
   3028 {
   3029 	struct audio_offset *ao;
   3030 	struct audio_info ai;
   3031 	audio_track_t *track;
   3032 	audio_encoding_t *ae;
   3033 	audio_format_query_t *query;
   3034 	u_int stamp;
   3035 	u_int offset;
   3036 	int val;
   3037 	int index;
   3038 	int error;
   3039 
   3040 #if defined(AUDIO_DEBUG)
   3041 	const char *ioctlnames[] = {
   3042 		"AUDIO_GETINFO",	/* 21 */
   3043 		"AUDIO_SETINFO",	/* 22 */
   3044 		"AUDIO_DRAIN",		/* 23 */
   3045 		"AUDIO_FLUSH",		/* 24 */
   3046 		"AUDIO_WSEEK",		/* 25 */
   3047 		"AUDIO_RERROR",		/* 26 */
   3048 		"AUDIO_GETDEV",		/* 27 */
   3049 		"AUDIO_GETENC",		/* 28 */
   3050 		"AUDIO_GETFD",		/* 29 */
   3051 		"AUDIO_SETFD",		/* 30 */
   3052 		"AUDIO_PERROR",		/* 31 */
   3053 		"AUDIO_GETIOFFS",	/* 32 */
   3054 		"AUDIO_GETOOFFS",	/* 33 */
   3055 		"AUDIO_GETPROPS",	/* 34 */
   3056 		"AUDIO_GETBUFINFO",	/* 35 */
   3057 		"AUDIO_SETCHAN",	/* 36 */
   3058 		"AUDIO_GETCHAN",	/* 37 */
   3059 		"AUDIO_QUERYFORMAT",	/* 38 */
   3060 		"AUDIO_GETFORMAT",	/* 39 */
   3061 		"AUDIO_SETFORMAT",	/* 40 */
   3062 	};
   3063 	char pre[64];
   3064 	int nameidx = (cmd & 0xff);
   3065 	if (21 <= nameidx && nameidx <= 21 + __arraycount(ioctlnames)) {
   3066 		snprintf(pre, sizeof(pre), "pid=%d.%d %s",
   3067 		    (int)curproc->p_pid, (int)l->l_lid,
   3068 		    ioctlnames[nameidx - 21]);
   3069 	} else {
   3070 		snprintf(pre, sizeof(pre), "pid=%d.%d (%lu,'%c',%u)",
   3071 		    (int)curproc->p_pid, (int)l->l_lid,
   3072 		    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), nameidx);
   3073 	}
   3074 #endif
   3075 
   3076 	error = 0;
   3077 	switch (cmd) {
   3078 	case FIONBIO:
   3079 		/* All handled in the upper FS layer. */
   3080 		break;
   3081 
   3082 	case FIONREAD:
   3083 		/* Get the number of bytes that can be read. */
   3084 		track = file->rtrack;
   3085 		if (track) {
   3086 			val = audio_track_readablebytes(track);
   3087 			*(int *)addr = val;
   3088 			TRACET(2, track, "pid=%d.%d FIONREAD bytes=%d",
   3089 			    (int)curproc->p_pid, (int)l->l_lid, val);
   3090 		} else {
   3091 			TRACEF(2, file, "pid=%d.%d FIONREAD no track",
   3092 			    (int)curproc->p_pid, (int)l->l_lid);
   3093 		}
   3094 		break;
   3095 
   3096 	case FIOASYNC:
   3097 		/* Set/Clear ASYNC I/O. */
   3098 		if (*(int *)addr) {
   3099 			file->async_audio = curproc->p_pid;
   3100 		} else {
   3101 			file->async_audio = 0;
   3102 		}
   3103 		TRACEF(2, file, "pid=%d.%d FIOASYNC %s",
   3104 		    (int)curproc->p_pid, (int)l->l_lid,
   3105 		    file->async_audio ? "on" : "off");
   3106 		break;
   3107 
   3108 	case AUDIO_FLUSH:
   3109 		/* XXX TODO: clear errors and restart? */
   3110 		TRACEF(2, file, "%s", pre);
   3111 		audio_file_clear(sc, file);
   3112 		break;
   3113 
   3114 	case AUDIO_PERROR:
   3115 	case AUDIO_RERROR:
   3116 		/*
   3117 		 * Number of dropped bytes during playback/record.  We don't
   3118 		 * know where or when they were dropped (including conversion
   3119 		 * stage).  Therefore, the number of accurate bytes or samples
   3120 		 * is also unknown.
   3121 		 */
   3122 		track = (cmd == AUDIO_PERROR) ? file->ptrack : file->rtrack;
   3123 		if (track) {
   3124 			val = frametobyte(&track->usrbuf.fmt,
   3125 			    track->dropframes);
   3126 			*(int *)addr = val;
   3127 			TRACET(2, track, "%s bytes=%d", pre, val);
   3128 		} else {
   3129 			TRACEF(2, file, "%s no track", pre);
   3130 		}
   3131 		break;
   3132 
   3133 	case AUDIO_GETIOFFS:
   3134 		ao = (struct audio_offset *)addr;
   3135 		track = file->rtrack;
   3136 		if (track == NULL) {
   3137 			ao->samples = 0;
   3138 			ao->deltablks = 0;
   3139 			ao->offset = 0;
   3140 			TRACEF(2, file, "%s no rtrack", pre);
   3141 			break;
   3142 		}
   3143 		mutex_enter(sc->sc_lock);
   3144 		mutex_enter(sc->sc_intr_lock);
   3145 		/* figure out where next transfer will start */
   3146 		stamp = track->stamp;
   3147 		offset = auring_tail(track->input);
   3148 		mutex_exit(sc->sc_intr_lock);
   3149 		mutex_exit(sc->sc_lock);
   3150 
   3151 		/* samples will overflow soon but is as per spec. */
   3152 		ao->samples = stamp * track->usrbuf_blksize;
   3153 		ao->deltablks = stamp - track->last_stamp;
   3154 		ao->offset = audio_track_inputblk_as_usrbyte(track, offset);
   3155 		TRACET(2, track, "%s samples=%u deltablks=%u offset=%u",
   3156 		    pre, ao->samples, ao->deltablks, ao->offset);
   3157 
   3158 		track->last_stamp = stamp;
   3159 		break;
   3160 
   3161 	case AUDIO_GETOOFFS:
   3162 		ao = (struct audio_offset *)addr;
   3163 		track = file->ptrack;
   3164 		if (track == NULL) {
   3165 			ao->samples = 0;
   3166 			ao->deltablks = 0;
   3167 			ao->offset = 0;
   3168 			TRACEF(2, file, "%s no ptrack", pre);
   3169 			break;
   3170 		}
   3171 		mutex_enter(sc->sc_lock);
   3172 		mutex_enter(sc->sc_intr_lock);
   3173 		/* figure out where next transfer will start */
   3174 		stamp = track->stamp;
   3175 		offset = track->usrbuf.head;
   3176 		mutex_exit(sc->sc_intr_lock);
   3177 		mutex_exit(sc->sc_lock);
   3178 
   3179 		/* samples will overflow soon but is as per spec. */
   3180 		ao->samples = stamp * track->usrbuf_blksize;
   3181 		ao->deltablks = stamp - track->last_stamp;
   3182 		ao->offset = offset;
   3183 		TRACET(2, track, "%s samples=%u deltablks=%u offset=%u",
   3184 		    pre, ao->samples, ao->deltablks, ao->offset);
   3185 
   3186 		track->last_stamp = stamp;
   3187 		break;
   3188 
   3189 	case AUDIO_WSEEK:
   3190 		track = file->ptrack;
   3191 		if (track) {
   3192 			val = track->usrbuf.used;
   3193 			*(u_long *)addr = val;
   3194 			TRACET(2, track, "%s bytes=%d", pre, val);
   3195 		} else {
   3196 			TRACEF(2, file, "%s no ptrack", pre);
   3197 		}
   3198 		break;
   3199 
   3200 	case AUDIO_SETINFO:
   3201 		TRACEF(2, file, "%s", pre);
   3202 		error = audio_exlock_enter(sc);
   3203 		if (error)
   3204 			break;
   3205 		error = audio_file_setinfo(sc, file, (struct audio_info *)addr);
   3206 		if (error) {
   3207 			audio_exlock_exit(sc);
   3208 			break;
   3209 		}
   3210 		if (ISDEVSOUND(dev))
   3211 			error = audiogetinfo(sc, &sc->sc_ai, 0, file);
   3212 		audio_exlock_exit(sc);
   3213 		break;
   3214 
   3215 	case AUDIO_GETINFO:
   3216 		TRACEF(2, file, "%s", pre);
   3217 		error = audio_exlock_enter(sc);
   3218 		if (error)
   3219 			break;
   3220 		error = audiogetinfo(sc, (struct audio_info *)addr, 1, file);
   3221 		audio_exlock_exit(sc);
   3222 		break;
   3223 
   3224 	case AUDIO_GETBUFINFO:
   3225 		TRACEF(2, file, "%s", pre);
   3226 		error = audio_exlock_enter(sc);
   3227 		if (error)
   3228 			break;
   3229 		error = audiogetinfo(sc, (struct audio_info *)addr, 0, file);
   3230 		audio_exlock_exit(sc);
   3231 		break;
   3232 
   3233 	case AUDIO_DRAIN:
   3234 		track = file->ptrack;
   3235 		if (track) {
   3236 			TRACET(2, track, "%s", pre);
   3237 			mutex_enter(sc->sc_lock);
   3238 			error = audio_track_drain(sc, track);
   3239 			mutex_exit(sc->sc_lock);
   3240 		} else {
   3241 			TRACEF(2, file, "%s no ptrack", pre);
   3242 		}
   3243 		break;
   3244 
   3245 	case AUDIO_GETDEV:
   3246 		TRACEF(2, file, "%s", pre);
   3247 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
   3248 		break;
   3249 
   3250 	case AUDIO_GETENC:
   3251 		ae = (audio_encoding_t *)addr;
   3252 		index = ae->index;
   3253 		TRACEF(2, file, "%s index=%d", pre, index);
   3254 		if (index < 0 || index >= __arraycount(audio_encodings)) {
   3255 			error = EINVAL;
   3256 			break;
   3257 		}
   3258 		*ae = audio_encodings[index];
   3259 		ae->index = index;
   3260 		/*
   3261 		 * EMULATED always.
   3262 		 * EMULATED flag at that time used to mean that it could
   3263 		 * not be passed directly to the hardware as-is.  But
   3264 		 * currently, all formats including hardware native is not
   3265 		 * passed directly to the hardware.  So I set EMULATED
   3266 		 * flag for all formats.
   3267 		 */
   3268 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   3269 		break;
   3270 
   3271 	case AUDIO_GETFD:
   3272 		/*
   3273 		 * Returns the current setting of full duplex mode.
   3274 		 * If HW has full duplex mode and there are two mixers,
   3275 		 * it is full duplex.  Otherwise half duplex.
   3276 		 */
   3277 		error = audio_exlock_enter(sc);
   3278 		if (error)
   3279 			break;
   3280 		val = (sc->sc_props & AUDIO_PROP_FULLDUPLEX)
   3281 		    && (sc->sc_pmixer && sc->sc_rmixer);
   3282 		audio_exlock_exit(sc);
   3283 		*(int *)addr = val;
   3284 		TRACEF(2, file, "%s fulldup=%d", pre, val);
   3285 		break;
   3286 
   3287 	case AUDIO_GETPROPS:
   3288 		val = sc->sc_props;
   3289 		*(int *)addr = val;
   3290 #if defined(AUDIO_DEBUG)
   3291 		char pbuf[64];
   3292 		snprintb(pbuf, sizeof(pbuf), "\x10"
   3293 		    "\6CAPTURE" "\5PLAY" "\3INDEP" "\2MMAP" "\1FULLDUP", val);
   3294 		TRACEF(2, file, "%s %s", pre, pbuf);
   3295 #endif
   3296 		break;
   3297 
   3298 	case AUDIO_QUERYFORMAT:
   3299 		query = (audio_format_query_t *)addr;
   3300 		TRACEF(2, file, "%s index=%u", pre, query->index);
   3301 		mutex_enter(sc->sc_lock);
   3302 		error = sc->hw_if->query_format(sc->hw_hdl, query);
   3303 		mutex_exit(sc->sc_lock);
   3304 		/* Hide internal information */
   3305 		query->fmt.driver_data = NULL;
   3306 		break;
   3307 
   3308 	case AUDIO_GETFORMAT:
   3309 		TRACEF(2, file, "%s", pre);
   3310 		error = audio_exlock_enter(sc);
   3311 		if (error)
   3312 			break;
   3313 		audio_mixers_get_format(sc, (struct audio_info *)addr);
   3314 		audio_exlock_exit(sc);
   3315 		break;
   3316 
   3317 	case AUDIO_SETFORMAT:
   3318 		TRACEF(2, file, "%s", pre);
   3319 		error = audio_exlock_enter(sc);
   3320 		audio_mixers_get_format(sc, &ai);
   3321 		error = audio_mixers_set_format(sc, (struct audio_info *)addr);
   3322 		if (error) {
   3323 			/* Rollback */
   3324 			audio_mixers_set_format(sc, &ai);
   3325 		}
   3326 		audio_exlock_exit(sc);
   3327 		break;
   3328 
   3329 	case AUDIO_SETFD:
   3330 	case AUDIO_SETCHAN:
   3331 	case AUDIO_GETCHAN:
   3332 		/* Obsoleted */
   3333 		TRACEF(2, file, "%s", pre);
   3334 		break;
   3335 
   3336 	default:
   3337 		TRACEF(2, file, "%s", pre);
   3338 		if (sc->hw_if->dev_ioctl) {
   3339 			mutex_enter(sc->sc_lock);
   3340 			error = sc->hw_if->dev_ioctl(sc->hw_hdl,
   3341 			    cmd, addr, flag, l);
   3342 			mutex_exit(sc->sc_lock);
   3343 		} else {
   3344 			error = EINVAL;
   3345 		}
   3346 		break;
   3347 	}
   3348 
   3349 	if (error)
   3350 		TRACEF(2, file, "%s error=%d", pre, error);
   3351 	return error;
   3352 }
   3353 
   3354 /*
   3355  * Convert n [frames] of the input buffer to bytes in the usrbuf format.
   3356  * n is in frames but should be a multiple of frame/block.  Note that the
   3357  * usrbuf's frame/block and the input buffer's frame/block may be different
   3358  * (i.e., if frequencies are different).
   3359  *
   3360  * This function is for recording track only.
   3361  */
   3362 static int
   3363 audio_track_inputblk_as_usrbyte(const audio_track_t *track, int n)
   3364 {
   3365 	int input_fpb;
   3366 
   3367 	/*
   3368 	 * In the input buffer on recording track, these are the same.
   3369 	 * input_fpb = frame_per_block(track->mixer, &track->input->fmt);
   3370 	 */
   3371 	input_fpb = track->mixer->frames_per_block;
   3372 
   3373 	return (n / input_fpb) * track->usrbuf_blksize;
   3374 }
   3375 
   3376 /*
   3377  * Returns the number of bytes that can be read on recording buffer.
   3378  */
   3379 static int
   3380 audio_track_readablebytes(const audio_track_t *track)
   3381 {
   3382 	int bytes;
   3383 
   3384 	KASSERT(track);
   3385 	KASSERT(track->mode == AUMODE_RECORD);
   3386 
   3387 	/*
   3388 	 * For recording, track->input is the main block-unit buffer and
   3389 	 * track->usrbuf holds less than one block of byte data ("fragment").
   3390 	 * Note that the input buffer is in frames and the usrbuf is in bytes.
   3391 	 *
   3392 	 * Actual total capacity of these two buffers is
   3393 	 *  input->capacity [frames] + usrbuf.capacity [bytes],
   3394 	 * but only input->capacity is reported to userland as buffer_size.
   3395 	 * So, even if the total used bytes exceed input->capacity, report it
   3396 	 * as input->capacity for consistency.
   3397 	 */
   3398 	bytes = audio_track_inputblk_as_usrbyte(track, track->input->used);
   3399 	if (track->input->used < track->input->capacity) {
   3400 		bytes += track->usrbuf.used;
   3401 	}
   3402 	return bytes;
   3403 }
   3404 
   3405 /*
   3406  * Must be called without sc_lock nor sc_exlock held.
   3407  */
   3408 int
   3409 audio_poll(struct audio_softc *sc, int events, struct lwp *l,
   3410 	audio_file_t *file)
   3411 {
   3412 	audio_track_t *track;
   3413 	int revents;
   3414 	bool in_is_valid;
   3415 	bool out_is_valid;
   3416 
   3417 #if defined(AUDIO_DEBUG)
   3418 #define POLLEV_BITMAP "\177\020" \
   3419 	    "b\10WRBAND\0" \
   3420 	    "b\7RDBAND\0" "b\6RDNORM\0" "b\5NVAL\0" "b\4HUP\0" \
   3421 	    "b\3ERR\0" "b\2OUT\0" "b\1PRI\0" "b\0IN\0"
   3422 	char evbuf[64];
   3423 	snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, events);
   3424 	TRACEF(2, file, "pid=%d.%d events=%s",
   3425 	    (int)curproc->p_pid, (int)l->l_lid, evbuf);
   3426 #endif
   3427 
   3428 	revents = 0;
   3429 	in_is_valid = false;
   3430 	out_is_valid = false;
   3431 	if (events & (POLLIN | POLLRDNORM)) {
   3432 		track = file->rtrack;
   3433 		if (track) {
   3434 			int used;
   3435 			in_is_valid = true;
   3436 			used = audio_track_readablebytes(track);
   3437 			if (used > 0)
   3438 				revents |= events & (POLLIN | POLLRDNORM);
   3439 		}
   3440 	}
   3441 	if (events & (POLLOUT | POLLWRNORM)) {
   3442 		track = file->ptrack;
   3443 		if (track) {
   3444 			out_is_valid = true;
   3445 			if (track->usrbuf.used <= track->usrbuf_usedlow)
   3446 				revents |= events & (POLLOUT | POLLWRNORM);
   3447 		}
   3448 	}
   3449 
   3450 	if (revents == 0) {
   3451 		mutex_enter(sc->sc_lock);
   3452 		if (in_is_valid) {
   3453 			TRACEF(3, file, "selrecord rsel");
   3454 			selrecord(l, &sc->sc_rsel);
   3455 		}
   3456 		if (out_is_valid) {
   3457 			TRACEF(3, file, "selrecord wsel");
   3458 			selrecord(l, &sc->sc_wsel);
   3459 		}
   3460 		mutex_exit(sc->sc_lock);
   3461 	}
   3462 
   3463 #if defined(AUDIO_DEBUG)
   3464 	snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, revents);
   3465 	TRACEF(2, file, "revents=%s", evbuf);
   3466 #endif
   3467 	return revents;
   3468 }
   3469 
   3470 static const struct filterops audioread_filtops = {
   3471 	.f_flags = FILTEROP_ISFD,
   3472 	.f_attach = NULL,
   3473 	.f_detach = filt_audioread_detach,
   3474 	.f_event = filt_audioread_event,
   3475 };
   3476 
   3477 static void
   3478 filt_audioread_detach(struct knote *kn)
   3479 {
   3480 	struct audio_softc *sc;
   3481 	audio_file_t *file;
   3482 
   3483 	file = kn->kn_hook;
   3484 	sc = file->sc;
   3485 	TRACEF(3, file, "called");
   3486 
   3487 	mutex_enter(sc->sc_lock);
   3488 	selremove_knote(&sc->sc_rsel, kn);
   3489 	mutex_exit(sc->sc_lock);
   3490 }
   3491 
   3492 static int
   3493 filt_audioread_event(struct knote *kn, long hint)
   3494 {
   3495 	audio_file_t *file;
   3496 	audio_track_t *track;
   3497 
   3498 	file = kn->kn_hook;
   3499 	track = file->rtrack;
   3500 
   3501 	/*
   3502 	 * kn_data must contain the number of bytes can be read.
   3503 	 * The return value indicates whether the event occurs or not.
   3504 	 */
   3505 
   3506 	if (track == NULL) {
   3507 		/* can not read with this descriptor. */
   3508 		kn->kn_data = 0;
   3509 		return 0;
   3510 	}
   3511 
   3512 	kn->kn_data = audio_track_readablebytes(track);
   3513 	TRACEF(3, file, "data=%" PRId64, kn->kn_data);
   3514 	return kn->kn_data > 0;
   3515 }
   3516 
   3517 static const struct filterops audiowrite_filtops = {
   3518 	.f_flags = FILTEROP_ISFD,
   3519 	.f_attach = NULL,
   3520 	.f_detach = filt_audiowrite_detach,
   3521 	.f_event = filt_audiowrite_event,
   3522 };
   3523 
   3524 static void
   3525 filt_audiowrite_detach(struct knote *kn)
   3526 {
   3527 	struct audio_softc *sc;
   3528 	audio_file_t *file;
   3529 
   3530 	file = kn->kn_hook;
   3531 	sc = file->sc;
   3532 	TRACEF(3, file, "called");
   3533 
   3534 	mutex_enter(sc->sc_lock);
   3535 	selremove_knote(&sc->sc_wsel, kn);
   3536 	mutex_exit(sc->sc_lock);
   3537 }
   3538 
   3539 static int
   3540 filt_audiowrite_event(struct knote *kn, long hint)
   3541 {
   3542 	audio_file_t *file;
   3543 	audio_track_t *track;
   3544 
   3545 	file = kn->kn_hook;
   3546 	track = file->ptrack;
   3547 
   3548 	/*
   3549 	 * kn_data must contain the number of bytes can be write.
   3550 	 * The return value indicates whether the event occurs or not.
   3551 	 */
   3552 
   3553 	if (track == NULL) {
   3554 		/* can not write with this descriptor. */
   3555 		kn->kn_data = 0;
   3556 		return 0;
   3557 	}
   3558 
   3559 	kn->kn_data = track->usrbuf_usedhigh - track->usrbuf.used;
   3560 	TRACEF(3, file, "data=%" PRId64, kn->kn_data);
   3561 	return (track->usrbuf.used < track->usrbuf_usedlow);
   3562 }
   3563 
   3564 /*
   3565  * Must be called without sc_lock nor sc_exlock held.
   3566  */
   3567 int
   3568 audio_kqfilter(struct audio_softc *sc, audio_file_t *file, struct knote *kn)
   3569 {
   3570 	struct selinfo *sip;
   3571 
   3572 	TRACEF(3, file, "kn=%p kn_filter=%x", kn, (int)kn->kn_filter);
   3573 
   3574 	switch (kn->kn_filter) {
   3575 	case EVFILT_READ:
   3576 		sip = &sc->sc_rsel;
   3577 		kn->kn_fop = &audioread_filtops;
   3578 		break;
   3579 
   3580 	case EVFILT_WRITE:
   3581 		sip = &sc->sc_wsel;
   3582 		kn->kn_fop = &audiowrite_filtops;
   3583 		break;
   3584 
   3585 	default:
   3586 		return EINVAL;
   3587 	}
   3588 
   3589 	kn->kn_hook = file;
   3590 
   3591 	mutex_enter(sc->sc_lock);
   3592 	selrecord_knote(sip, kn);
   3593 	mutex_exit(sc->sc_lock);
   3594 
   3595 	return 0;
   3596 }
   3597 
   3598 /*
   3599  * Must be called without sc_lock nor sc_exlock held.
   3600  */
   3601 int
   3602 audio_mmap(struct audio_softc *sc, off_t *offp, size_t len, int prot,
   3603 	int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp,
   3604 	audio_file_t *file)
   3605 {
   3606 	audio_track_t *track;
   3607 	struct uvm_object *uobj;
   3608 	vaddr_t vstart;
   3609 	vsize_t vsize;
   3610 	int error;
   3611 
   3612 	TRACEF(1, file, "off=%jd, len=%ju, prot=%d",
   3613 	    (intmax_t)(*offp), (uintmax_t)len, prot);
   3614 
   3615 	KASSERT(len > 0);
   3616 
   3617 	if (*offp < 0)
   3618 		return EINVAL;
   3619 
   3620 #if 0
   3621 	/* XXX
   3622 	 * The idea here was to use the protection to determine if
   3623 	 * we are mapping the read or write buffer, but it fails.
   3624 	 * The VM system is broken in (at least) two ways.
   3625 	 * 1) If you map memory VM_PROT_WRITE you SIGSEGV
   3626 	 *    when writing to it, so VM_PROT_READ|VM_PROT_WRITE
   3627 	 *    has to be used for mmapping the play buffer.
   3628 	 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
   3629 	 *    audio_mmap will get called at some point with VM_PROT_READ
   3630 	 *    only.
   3631 	 * So, alas, we always map the play buffer for now.
   3632 	 */
   3633 	if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
   3634 	    prot == VM_PROT_WRITE)
   3635 		track = file->ptrack;
   3636 	else if (prot == VM_PROT_READ)
   3637 		track = file->rtrack;
   3638 	else
   3639 		return EINVAL;
   3640 #else
   3641 	track = file->ptrack;
   3642 #endif
   3643 	if (track == NULL)
   3644 		return EACCES;
   3645 
   3646 	/* XXX TODO: what happens when mmap twice. */
   3647 	if (track->mmapped)
   3648 		return EIO;
   3649 
   3650 	/* Create a uvm anonymous object */
   3651 	vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
   3652 	if (*offp + len > vsize)
   3653 		return EOVERFLOW;
   3654 	uobj = uao_create(vsize, 0);
   3655 
   3656 	/* Map it into the kernel virtual address space */
   3657 	vstart = 0;
   3658 	error = uvm_map(kernel_map, &vstart, vsize, uobj, 0, 0,
   3659 	    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
   3660 	    UVM_ADV_RANDOM, 0));
   3661 	if (error) {
   3662 		device_printf(sc->sc_dev, "uvm_map failed: errno=%d\n", error);
   3663 		uao_detach(uobj);	/* release reference */
   3664 		return error;
   3665 	}
   3666 
   3667 	error = uvm_map_pageable(kernel_map, vstart, vstart + vsize,
   3668 	    false, 0);
   3669 	if (error) {
   3670 		device_printf(sc->sc_dev, "uvm_map_pageable failed: errno=%d\n",
   3671 		    error);
   3672 		goto abort;
   3673 	}
   3674 
   3675 	error = audio_exlock_mutex_enter(sc);
   3676 	if (error)
   3677 		goto abort;
   3678 
   3679 	/*
   3680 	 * mmap() will start playing immediately.  XXX Maybe we lack API...
   3681 	 * If no one has played yet, start pmixer here.
   3682 	 */
   3683 	if (sc->sc_pbusy == false)
   3684 		audio_pmixer_start(sc, true);
   3685 	audio_exlock_mutex_exit(sc);
   3686 
   3687 	/* Finally, replace the usrbuf from kmem to uvm. */
   3688 	audio_track_lock_enter(track);
   3689 	kmem_free(track->usrbuf.mem, track->usrbuf_allocsize);
   3690 	track->usrbuf.mem = (void *)vstart;
   3691 	track->usrbuf_allocsize = vsize;
   3692 	memset(track->usrbuf.mem, 0, vsize);
   3693 	track->mmapped = true;
   3694 	audio_track_lock_exit(track);
   3695 
   3696 	/* Acquire a reference for the mmap.  munmap will release. */
   3697 	uao_reference(uobj);
   3698 	*uobjp = uobj;
   3699 	*maxprotp = prot;
   3700 	*advicep = UVM_ADV_RANDOM;
   3701 	*flagsp = MAP_SHARED;
   3702 
   3703 	return 0;
   3704 
   3705 abort:
   3706 	uvm_unmap(kernel_map, vstart, vstart + vsize);
   3707 	/* uvm_unmap also detach uobj */
   3708 	return error;
   3709 }
   3710 
   3711 /*
   3712  * /dev/audioctl has to be able to open at any time without interference
   3713  * with any /dev/audio or /dev/sound.
   3714  * Must be called with sc_exlock held and without sc_lock held.
   3715  */
   3716 static int
   3717 audioctl_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   3718 	struct lwp *l)
   3719 {
   3720 	struct file *fp;
   3721 	audio_file_t *af;
   3722 	int fd;
   3723 	int error;
   3724 
   3725 	KASSERT(sc->sc_exlock);
   3726 
   3727 	TRACE(1, "called");
   3728 
   3729 	error = fd_allocfile(&fp, &fd);
   3730 	if (error)
   3731 		return error;
   3732 
   3733 	af = kmem_zalloc(sizeof(*af), KM_SLEEP);
   3734 	af->sc = sc;
   3735 	af->dev = dev;
   3736 
   3737 	mutex_enter(sc->sc_lock);
   3738 	if (sc->sc_dying) {
   3739 		mutex_exit(sc->sc_lock);
   3740 		kmem_free(af, sizeof(*af));
   3741 		fd_abort(curproc, fp, fd);
   3742 		return ENXIO;
   3743 	}
   3744 	mutex_enter(sc->sc_intr_lock);
   3745 	SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
   3746 	mutex_exit(sc->sc_intr_lock);
   3747 	mutex_exit(sc->sc_lock);
   3748 
   3749 	error = fd_clone(fp, fd, flags, &audio_fileops, af);
   3750 	KASSERTMSG(error == EMOVEFD, "error=%d", error);
   3751 
   3752 	return error;
   3753 }
   3754 
   3755 /*
   3756  * Free 'mem' if available, and initialize the pointer.
   3757  * For this reason, this is implemented as macro.
   3758  */
   3759 #define audio_free(mem)	do {	\
   3760 	if (mem != NULL) {	\
   3761 		kern_free(mem);	\
   3762 		mem = NULL;	\
   3763 	}	\
   3764 } while (0)
   3765 
   3766 /*
   3767  * (Re)allocate 'memblock' with specified 'bytes'.
   3768  * bytes must not be 0.
   3769  * This function never returns NULL.
   3770  */
   3771 static void *
   3772 audio_realloc(void *memblock, size_t bytes)
   3773 {
   3774 
   3775 	KASSERT(bytes != 0);
   3776 	if (memblock)
   3777 		kern_free(memblock);
   3778 	return kern_malloc(bytes, M_WAITOK);
   3779 }
   3780 
   3781 /*
   3782  * Free usrbuf (if available).
   3783  */
   3784 static void
   3785 audio_free_usrbuf(audio_track_t *track)
   3786 {
   3787 	vaddr_t vstart;
   3788 	vsize_t vsize;
   3789 
   3790 	if (track->usrbuf_allocsize != 0) {
   3791 		if (track->mmapped) {
   3792 			/*
   3793 			 * Unmap the kernel mapping.  uvm_unmap releases the
   3794 			 * reference to the uvm object, and this should be the
   3795 			 * last virtual mapping of the uvm object, so no need
   3796 			 * to explicitly release (`detach') the object.
   3797 			 */
   3798 			vstart = (vaddr_t)track->usrbuf.mem;
   3799 			vsize = track->usrbuf_allocsize;
   3800 			uvm_unmap(kernel_map, vstart, vstart + vsize);
   3801 			track->mmapped = false;
   3802 		} else {
   3803 			kmem_free(track->usrbuf.mem, track->usrbuf_allocsize);
   3804 		}
   3805 	}
   3806 	track->usrbuf.mem = NULL;
   3807 	track->usrbuf.capacity = 0;
   3808 	track->usrbuf_allocsize = 0;
   3809 }
   3810 
   3811 /*
   3812  * This filter changes the volume for each channel.
   3813  * arg->context points track->ch_volume[].
   3814  */
   3815 static void
   3816 audio_track_chvol(audio_filter_arg_t *arg)
   3817 {
   3818 	int16_t *ch_volume;
   3819 	const aint_t *s;
   3820 	aint_t *d;
   3821 	u_int i;
   3822 	u_int ch;
   3823 	u_int channels;
   3824 
   3825 	DIAGNOSTIC_filter_arg(arg);
   3826 	KASSERTMSG(arg->srcfmt->channels == arg->dstfmt->channels,
   3827 	    "arg->srcfmt->channels=%d, arg->dstfmt->channels=%d",
   3828 	    arg->srcfmt->channels, arg->dstfmt->channels);
   3829 	KASSERT(arg->context != NULL);
   3830 	KASSERTMSG(arg->srcfmt->channels <= AUDIO_MAX_CHANNELS,
   3831 	    "arg->srcfmt->channels=%d", arg->srcfmt->channels);
   3832 
   3833 	s = arg->src;
   3834 	d = arg->dst;
   3835 	ch_volume = arg->context;
   3836 
   3837 	channels = arg->srcfmt->channels;
   3838 	for (i = 0; i < arg->count; i++) {
   3839 		for (ch = 0; ch < channels; ch++) {
   3840 			aint2_t val;
   3841 			val = *s++;
   3842 			val = AUDIO_SCALEDOWN(val * ch_volume[ch], 8);
   3843 			*d++ = (aint_t)val;
   3844 		}
   3845 	}
   3846 }
   3847 
   3848 /*
   3849  * This filter performs conversion from stereo (or more channels) to mono.
   3850  */
   3851 static void
   3852 audio_track_chmix_mixLR(audio_filter_arg_t *arg)
   3853 {
   3854 	const aint_t *s;
   3855 	aint_t *d;
   3856 	u_int i;
   3857 
   3858 	DIAGNOSTIC_filter_arg(arg);
   3859 
   3860 	s = arg->src;
   3861 	d = arg->dst;
   3862 
   3863 	for (i = 0; i < arg->count; i++) {
   3864 		*d++ = AUDIO_SCALEDOWN(s[0], 1) + AUDIO_SCALEDOWN(s[1], 1);
   3865 		s += arg->srcfmt->channels;
   3866 	}
   3867 }
   3868 
   3869 /*
   3870  * This filter performs conversion from mono to stereo (or more channels).
   3871  */
   3872 static void
   3873 audio_track_chmix_dupLR(audio_filter_arg_t *arg)
   3874 {
   3875 	const aint_t *s;
   3876 	aint_t *d;
   3877 	u_int i;
   3878 	u_int ch;
   3879 	u_int dstchannels;
   3880 
   3881 	DIAGNOSTIC_filter_arg(arg);
   3882 
   3883 	s = arg->src;
   3884 	d = arg->dst;
   3885 	dstchannels = arg->dstfmt->channels;
   3886 
   3887 	for (i = 0; i < arg->count; i++) {
   3888 		d[0] = s[0];
   3889 		d[1] = s[0];
   3890 		s++;
   3891 		d += dstchannels;
   3892 	}
   3893 	if (dstchannels > 2) {
   3894 		d = arg->dst;
   3895 		for (i = 0; i < arg->count; i++) {
   3896 			for (ch = 2; ch < dstchannels; ch++) {
   3897 				d[ch] = 0;
   3898 			}
   3899 			d += dstchannels;
   3900 		}
   3901 	}
   3902 }
   3903 
   3904 /*
   3905  * This filter shrinks M channels into N channels.
   3906  * Extra channels are discarded.
   3907  */
   3908 static void
   3909 audio_track_chmix_shrink(audio_filter_arg_t *arg)
   3910 {
   3911 	const aint_t *s;
   3912 	aint_t *d;
   3913 	u_int i;
   3914 	u_int ch;
   3915 
   3916 	DIAGNOSTIC_filter_arg(arg);
   3917 
   3918 	s = arg->src;
   3919 	d = arg->dst;
   3920 
   3921 	for (i = 0; i < arg->count; i++) {
   3922 		for (ch = 0; ch < arg->dstfmt->channels; ch++) {
   3923 			*d++ = s[ch];
   3924 		}
   3925 		s += arg->srcfmt->channels;
   3926 	}
   3927 }
   3928 
   3929 /*
   3930  * This filter expands M channels into N channels.
   3931  * Silence is inserted for missing channels.
   3932  */
   3933 static void
   3934 audio_track_chmix_expand(audio_filter_arg_t *arg)
   3935 {
   3936 	const aint_t *s;
   3937 	aint_t *d;
   3938 	u_int i;
   3939 	u_int ch;
   3940 	u_int srcchannels;
   3941 	u_int dstchannels;
   3942 
   3943 	DIAGNOSTIC_filter_arg(arg);
   3944 
   3945 	s = arg->src;
   3946 	d = arg->dst;
   3947 
   3948 	srcchannels = arg->srcfmt->channels;
   3949 	dstchannels = arg->dstfmt->channels;
   3950 	for (i = 0; i < arg->count; i++) {
   3951 		for (ch = 0; ch < srcchannels; ch++) {
   3952 			*d++ = *s++;
   3953 		}
   3954 		for (; ch < dstchannels; ch++) {
   3955 			*d++ = 0;
   3956 		}
   3957 	}
   3958 }
   3959 
   3960 /*
   3961  * This filter performs frequency conversion (up sampling).
   3962  * It uses linear interpolation.
   3963  */
   3964 static void
   3965 audio_track_freq_up(audio_filter_arg_t *arg)
   3966 {
   3967 	audio_track_t *track;
   3968 	audio_ring_t *src;
   3969 	audio_ring_t *dst;
   3970 	const aint_t *s;
   3971 	aint_t *d;
   3972 	aint_t prev[AUDIO_MAX_CHANNELS];
   3973 	aint_t curr[AUDIO_MAX_CHANNELS];
   3974 	aint_t grad[AUDIO_MAX_CHANNELS];
   3975 	u_int i;
   3976 	u_int t;
   3977 	u_int step;
   3978 	u_int channels;
   3979 	u_int ch;
   3980 	int srcused;
   3981 
   3982 	track = arg->context;
   3983 	KASSERT(track);
   3984 	src = &track->freq.srcbuf;
   3985 	dst = track->freq.dst;
   3986 	DIAGNOSTIC_ring(dst);
   3987 	DIAGNOSTIC_ring(src);
   3988 	KASSERT(src->used > 0);
   3989 	KASSERTMSG(src->fmt.channels == dst->fmt.channels,
   3990 	    "src->fmt.channels=%d dst->fmt.channels=%d",
   3991 	    src->fmt.channels, dst->fmt.channels);
   3992 	KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
   3993 	    "src->head=%d track->mixer->frames_per_block=%d",
   3994 	    src->head, track->mixer->frames_per_block);
   3995 
   3996 	s = arg->src;
   3997 	d = arg->dst;
   3998 
   3999 	/*
   4000 	 * In order to facilitate interpolation for each block, slide (delay)
   4001 	 * input by one sample.  As a result, strictly speaking, the output
   4002 	 * phase is delayed by 1/dstfreq.  However, I believe there is no
   4003 	 * observable impact.
   4004 	 *
   4005 	 * Example)
   4006 	 * srcfreq:dstfreq = 1:3
   4007 	 *
   4008 	 *  A - -
   4009 	 *  |
   4010 	 *  |
   4011 	 *  |     B - -
   4012 	 *  +-----+-----> input timeframe
   4013 	 *  0     1
   4014 	 *
   4015 	 *  0     1
   4016 	 *  +-----+-----> input timeframe
   4017 	 *  |     A
   4018 	 *  |   x   x
   4019 	 *  | x       x
   4020 	 *  x          (B)
   4021 	 *  +-+-+-+-+-+-> output timeframe
   4022 	 *  0 1 2 3 4 5
   4023 	 */
   4024 
   4025 	/* Last samples in previous block */
   4026 	channels = src->fmt.channels;
   4027 	for (ch = 0; ch < channels; ch++) {
   4028 		prev[ch] = track->freq_prev[ch];
   4029 		curr[ch] = track->freq_curr[ch];
   4030 		grad[ch] = curr[ch] - prev[ch];
   4031 	}
   4032 
   4033 	step = track->freq_step;
   4034 	t = track->freq_current;
   4035 //#define FREQ_DEBUG
   4036 #if defined(FREQ_DEBUG)
   4037 #define PRINTF(fmt...)	printf(fmt)
   4038 #else
   4039 #define PRINTF(fmt...)	do { } while (0)
   4040 #endif
   4041 	srcused = src->used;
   4042 	PRINTF("upstart step=%d leap=%d", step, track->freq_leap);
   4043 	PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
   4044 	PRINTF(" prev=%d curr=%d grad=%d", prev[0], curr[0], grad[0]);
   4045 	PRINTF(" t=%d\n", t);
   4046 
   4047 	for (i = 0; i < arg->count; i++) {
   4048 		PRINTF("i=%d t=%5d", i, t);
   4049 		if (t >= 65536) {
   4050 			for (ch = 0; ch < channels; ch++) {
   4051 				prev[ch] = curr[ch];
   4052 				curr[ch] = *s++;
   4053 				grad[ch] = curr[ch] - prev[ch];
   4054 			}
   4055 			PRINTF(" prev=%d s[%d]=%d",
   4056 			    prev[0], src->used - srcused, curr[0]);
   4057 
   4058 			/* Update */
   4059 			t -= 65536;
   4060 			srcused--;
   4061 			if (srcused < 0) {
   4062 				PRINTF(" break\n");
   4063 				break;
   4064 			}
   4065 		}
   4066 
   4067 		for (ch = 0; ch < channels; ch++) {
   4068 			*d++ = prev[ch] + (aint2_t)grad[ch] * t / 65536;
   4069 #if defined(FREQ_DEBUG)
   4070 			if (ch == 0)
   4071 				printf(" t=%5d *d=%d", t, d[-1]);
   4072 #endif
   4073 		}
   4074 		t += step;
   4075 
   4076 		PRINTF("\n");
   4077 	}
   4078 	PRINTF("end prev=%d curr=%d\n", prev[0], curr[0]);
   4079 
   4080 	auring_take(src, src->used);
   4081 	auring_push(dst, i);
   4082 
   4083 	/* Adjust */
   4084 	t += track->freq_leap;
   4085 
   4086 	track->freq_current = t;
   4087 	for (ch = 0; ch < channels; ch++) {
   4088 		track->freq_prev[ch] = prev[ch];
   4089 		track->freq_curr[ch] = curr[ch];
   4090 	}
   4091 }
   4092 
   4093 /*
   4094  * This filter performs frequency conversion (down sampling).
   4095  * It uses simple thinning.
   4096  */
   4097 static void
   4098 audio_track_freq_down(audio_filter_arg_t *arg)
   4099 {
   4100 	audio_track_t *track;
   4101 	audio_ring_t *src;
   4102 	audio_ring_t *dst;
   4103 	const aint_t *s0;
   4104 	aint_t *d;
   4105 	u_int i;
   4106 	u_int t;
   4107 	u_int step;
   4108 	u_int ch;
   4109 	u_int channels;
   4110 
   4111 	track = arg->context;
   4112 	KASSERT(track);
   4113 	src = &track->freq.srcbuf;
   4114 	dst = track->freq.dst;
   4115 
   4116 	DIAGNOSTIC_ring(dst);
   4117 	DIAGNOSTIC_ring(src);
   4118 	KASSERT(src->used > 0);
   4119 	KASSERTMSG(src->fmt.channels == dst->fmt.channels,
   4120 	    "src->fmt.channels=%d dst->fmt.channels=%d",
   4121 	    src->fmt.channels, dst->fmt.channels);
   4122 	KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
   4123 	    "src->head=%d track->mixer->frames_per_block=%d",
   4124 	    src->head, track->mixer->frames_per_block);
   4125 
   4126 	s0 = arg->src;
   4127 	d = arg->dst;
   4128 	t = track->freq_current;
   4129 	step = track->freq_step;
   4130 	channels = dst->fmt.channels;
   4131 	PRINTF("downstart step=%d leap=%d", step, track->freq_leap);
   4132 	PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
   4133 	PRINTF(" t=%d\n", t);
   4134 
   4135 	for (i = 0; i < arg->count && t / 65536 < src->used; i++) {
   4136 		const aint_t *s;
   4137 		PRINTF("i=%4d t=%10d", i, t);
   4138 		s = s0 + (t / 65536) * channels;
   4139 		PRINTF(" s=%5ld", (s - s0) / channels);
   4140 		for (ch = 0; ch < channels; ch++) {
   4141 			if (ch == 0) PRINTF(" *s=%d", s[ch]);
   4142 			*d++ = s[ch];
   4143 		}
   4144 		PRINTF("\n");
   4145 		t += step;
   4146 	}
   4147 	t += track->freq_leap;
   4148 	PRINTF("end t=%d\n", t);
   4149 	auring_take(src, src->used);
   4150 	auring_push(dst, i);
   4151 	track->freq_current = t % 65536;
   4152 }
   4153 
   4154 /*
   4155  * Creates track and returns it.
   4156  * Must be called without sc_lock held.
   4157  */
   4158 audio_track_t *
   4159 audio_track_create(struct audio_softc *sc, audio_trackmixer_t *mixer)
   4160 {
   4161 	audio_track_t *track;
   4162 	static int newid = 0;
   4163 
   4164 	track = kmem_zalloc(sizeof(*track), KM_SLEEP);
   4165 
   4166 	track->id = newid++;
   4167 	track->mixer = mixer;
   4168 	track->mode = mixer->mode;
   4169 
   4170 	/* Do TRACE after id is assigned. */
   4171 	TRACET(3, track, "for %s",
   4172 	    mixer->mode == AUMODE_PLAY ? "playback" : "recording");
   4173 
   4174 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   4175 	track->volume = 256;
   4176 #endif
   4177 	for (int i = 0; i < AUDIO_MAX_CHANNELS; i++) {
   4178 		track->ch_volume[i] = 256;
   4179 	}
   4180 
   4181 	return track;
   4182 }
   4183 
   4184 /*
   4185  * Release all resources of the track and track itself.
   4186  * track must not be NULL.  Don't specify the track within the file
   4187  * structure linked from sc->sc_files.
   4188  */
   4189 static void
   4190 audio_track_destroy(audio_track_t *track)
   4191 {
   4192 
   4193 	KASSERT(track);
   4194 
   4195 	audio_free_usrbuf(track);
   4196 	audio_free(track->codec.srcbuf.mem);
   4197 	audio_free(track->chvol.srcbuf.mem);
   4198 	audio_free(track->chmix.srcbuf.mem);
   4199 	audio_free(track->freq.srcbuf.mem);
   4200 	audio_free(track->outbuf.mem);
   4201 
   4202 	kmem_free(track, sizeof(*track));
   4203 }
   4204 
   4205 /*
   4206  * It returns encoding conversion filter according to src and dst format.
   4207  * If it is not a convertible pair, it returns NULL.  Either src or dst
   4208  * must be internal format.
   4209  */
   4210 static audio_filter_t
   4211 audio_track_get_codec(audio_track_t *track, const audio_format2_t *src,
   4212 	const audio_format2_t *dst)
   4213 {
   4214 
   4215 	if (audio_format2_is_internal(src)) {
   4216 		if (dst->encoding == AUDIO_ENCODING_ULAW) {
   4217 			return audio_internal_to_mulaw;
   4218 		} else if (dst->encoding == AUDIO_ENCODING_ALAW) {
   4219 			return audio_internal_to_alaw;
   4220 		} else if (audio_format2_is_linear(dst)) {
   4221 			switch (dst->stride) {
   4222 			case 8:
   4223 				return audio_internal_to_linear8;
   4224 			case 16:
   4225 				return audio_internal_to_linear16;
   4226 #if defined(AUDIO_SUPPORT_LINEAR24)
   4227 			case 24:
   4228 				return audio_internal_to_linear24;
   4229 #endif
   4230 			case 32:
   4231 				return audio_internal_to_linear32;
   4232 			default:
   4233 				TRACET(1, track, "unsupported %s stride %d",
   4234 				    "dst", dst->stride);
   4235 				goto abort;
   4236 			}
   4237 		}
   4238 	} else if (audio_format2_is_internal(dst)) {
   4239 		if (src->encoding == AUDIO_ENCODING_ULAW) {
   4240 			return audio_mulaw_to_internal;
   4241 		} else if (src->encoding == AUDIO_ENCODING_ALAW) {
   4242 			return audio_alaw_to_internal;
   4243 		} else if (audio_format2_is_linear(src)) {
   4244 			switch (src->stride) {
   4245 			case 8:
   4246 				return audio_linear8_to_internal;
   4247 			case 16:
   4248 				return audio_linear16_to_internal;
   4249 #if defined(AUDIO_SUPPORT_LINEAR24)
   4250 			case 24:
   4251 				return audio_linear24_to_internal;
   4252 #endif
   4253 			case 32:
   4254 				return audio_linear32_to_internal;
   4255 			default:
   4256 				TRACET(1, track, "unsupported %s stride %d",
   4257 				    "src", src->stride);
   4258 				goto abort;
   4259 			}
   4260 		}
   4261 	}
   4262 
   4263 	TRACET(1, track, "unsupported encoding");
   4264 abort:
   4265 #if defined(AUDIO_DEBUG)
   4266 	if (audiodebug >= 2) {
   4267 		char buf[100];
   4268 		audio_format2_tostr(buf, sizeof(buf), src);
   4269 		TRACET(2, track, "src %s", buf);
   4270 		audio_format2_tostr(buf, sizeof(buf), dst);
   4271 		TRACET(2, track, "dst %s", buf);
   4272 	}
   4273 #endif
   4274 	return NULL;
   4275 }
   4276 
   4277 /*
   4278  * Initialize the codec stage of this track as necessary.
   4279  * If successful, it initializes the codec stage as necessary, stores updated
   4280  * last_dst in *last_dstp in any case, and returns 0.
   4281  * Otherwise, it returns errno without modifying *last_dstp.
   4282  */
   4283 static int
   4284 audio_track_init_codec(audio_track_t *track, audio_ring_t **last_dstp)
   4285 {
   4286 	audio_ring_t *last_dst;
   4287 	audio_ring_t *srcbuf;
   4288 	audio_format2_t *srcfmt;
   4289 	audio_format2_t *dstfmt;
   4290 	audio_filter_arg_t *arg;
   4291 	u_int len;
   4292 	int error;
   4293 
   4294 	KASSERT(track);
   4295 
   4296 	last_dst = *last_dstp;
   4297 	dstfmt = &last_dst->fmt;
   4298 	srcfmt = &track->inputfmt;
   4299 	srcbuf = &track->codec.srcbuf;
   4300 	error = 0;
   4301 
   4302 	if (srcfmt->encoding != dstfmt->encoding
   4303 	 || srcfmt->precision != dstfmt->precision
   4304 	 || srcfmt->stride != dstfmt->stride) {
   4305 		track->codec.dst = last_dst;
   4306 
   4307 		srcbuf->fmt = *dstfmt;
   4308 		srcbuf->fmt.encoding = srcfmt->encoding;
   4309 		srcbuf->fmt.precision = srcfmt->precision;
   4310 		srcbuf->fmt.stride = srcfmt->stride;
   4311 
   4312 		track->codec.filter = audio_track_get_codec(track,
   4313 		    &srcbuf->fmt, dstfmt);
   4314 		if (track->codec.filter == NULL) {
   4315 			error = EINVAL;
   4316 			goto abort;
   4317 		}
   4318 
   4319 		srcbuf->head = 0;
   4320 		srcbuf->used = 0;
   4321 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4322 		len = auring_bytelen(srcbuf);
   4323 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4324 
   4325 		arg = &track->codec.arg;
   4326 		arg->srcfmt = &srcbuf->fmt;
   4327 		arg->dstfmt = dstfmt;
   4328 		arg->context = NULL;
   4329 
   4330 		*last_dstp = srcbuf;
   4331 		return 0;
   4332 	}
   4333 
   4334 abort:
   4335 	track->codec.filter = NULL;
   4336 	audio_free(srcbuf->mem);
   4337 	return error;
   4338 }
   4339 
   4340 /*
   4341  * Initialize the chvol stage of this track as necessary.
   4342  * If successful, it initializes the chvol stage as necessary, stores updated
   4343  * last_dst in *last_dstp in any case, and returns 0.
   4344  * Otherwise, it returns errno without modifying *last_dstp.
   4345  */
   4346 static int
   4347 audio_track_init_chvol(audio_track_t *track, audio_ring_t **last_dstp)
   4348 {
   4349 	audio_ring_t *last_dst;
   4350 	audio_ring_t *srcbuf;
   4351 	audio_format2_t *srcfmt;
   4352 	audio_format2_t *dstfmt;
   4353 	audio_filter_arg_t *arg;
   4354 	u_int len;
   4355 	int error;
   4356 
   4357 	KASSERT(track);
   4358 
   4359 	last_dst = *last_dstp;
   4360 	dstfmt = &last_dst->fmt;
   4361 	srcfmt = &track->inputfmt;
   4362 	srcbuf = &track->chvol.srcbuf;
   4363 	error = 0;
   4364 
   4365 	/* Check whether channel volume conversion is necessary. */
   4366 	bool use_chvol = false;
   4367 	for (int ch = 0; ch < srcfmt->channels; ch++) {
   4368 		if (track->ch_volume[ch] != 256) {
   4369 			use_chvol = true;
   4370 			break;
   4371 		}
   4372 	}
   4373 
   4374 	if (use_chvol == true) {
   4375 		track->chvol.dst = last_dst;
   4376 		track->chvol.filter = audio_track_chvol;
   4377 
   4378 		srcbuf->fmt = *dstfmt;
   4379 		/* no format conversion occurs */
   4380 
   4381 		srcbuf->head = 0;
   4382 		srcbuf->used = 0;
   4383 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4384 		len = auring_bytelen(srcbuf);
   4385 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4386 
   4387 		arg = &track->chvol.arg;
   4388 		arg->srcfmt = &srcbuf->fmt;
   4389 		arg->dstfmt = dstfmt;
   4390 		arg->context = track->ch_volume;
   4391 
   4392 		*last_dstp = srcbuf;
   4393 		return 0;
   4394 	}
   4395 
   4396 	track->chvol.filter = NULL;
   4397 	audio_free(srcbuf->mem);
   4398 	return error;
   4399 }
   4400 
   4401 /*
   4402  * Initialize the chmix stage of this track as necessary.
   4403  * If successful, it initializes the chmix stage as necessary, stores updated
   4404  * last_dst in *last_dstp in any case, and returns 0.
   4405  * Otherwise, it returns errno without modifying *last_dstp.
   4406  */
   4407 static int
   4408 audio_track_init_chmix(audio_track_t *track, audio_ring_t **last_dstp)
   4409 {
   4410 	audio_ring_t *last_dst;
   4411 	audio_ring_t *srcbuf;
   4412 	audio_format2_t *srcfmt;
   4413 	audio_format2_t *dstfmt;
   4414 	audio_filter_arg_t *arg;
   4415 	u_int srcch;
   4416 	u_int dstch;
   4417 	u_int len;
   4418 	int error;
   4419 
   4420 	KASSERT(track);
   4421 
   4422 	last_dst = *last_dstp;
   4423 	dstfmt = &last_dst->fmt;
   4424 	srcfmt = &track->inputfmt;
   4425 	srcbuf = &track->chmix.srcbuf;
   4426 	error = 0;
   4427 
   4428 	srcch = srcfmt->channels;
   4429 	dstch = dstfmt->channels;
   4430 	if (srcch != dstch) {
   4431 		track->chmix.dst = last_dst;
   4432 
   4433 		if (srcch >= 2 && dstch == 1) {
   4434 			track->chmix.filter = audio_track_chmix_mixLR;
   4435 		} else if (srcch == 1 && dstch >= 2) {
   4436 			track->chmix.filter = audio_track_chmix_dupLR;
   4437 		} else if (srcch > dstch) {
   4438 			track->chmix.filter = audio_track_chmix_shrink;
   4439 		} else {
   4440 			track->chmix.filter = audio_track_chmix_expand;
   4441 		}
   4442 
   4443 		srcbuf->fmt = *dstfmt;
   4444 		srcbuf->fmt.channels = srcch;
   4445 
   4446 		srcbuf->head = 0;
   4447 		srcbuf->used = 0;
   4448 		/* XXX The buffer size should be able to calculate. */
   4449 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4450 		len = auring_bytelen(srcbuf);
   4451 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4452 
   4453 		arg = &track->chmix.arg;
   4454 		arg->srcfmt = &srcbuf->fmt;
   4455 		arg->dstfmt = dstfmt;
   4456 		arg->context = NULL;
   4457 
   4458 		*last_dstp = srcbuf;
   4459 		return 0;
   4460 	}
   4461 
   4462 	track->chmix.filter = NULL;
   4463 	audio_free(srcbuf->mem);
   4464 	return error;
   4465 }
   4466 
   4467 /*
   4468  * Initialize the freq stage of this track as necessary.
   4469  * If successful, it initializes the freq stage as necessary, stores updated
   4470  * last_dst in *last_dstp in any case, and returns 0.
   4471  * Otherwise, it returns errno without modifying *last_dstp.
   4472  */
   4473 static int
   4474 audio_track_init_freq(audio_track_t *track, audio_ring_t **last_dstp)
   4475 {
   4476 	audio_ring_t *last_dst;
   4477 	audio_ring_t *srcbuf;
   4478 	audio_format2_t *srcfmt;
   4479 	audio_format2_t *dstfmt;
   4480 	audio_filter_arg_t *arg;
   4481 	uint32_t srcfreq;
   4482 	uint32_t dstfreq;
   4483 	u_int dst_capacity;
   4484 	u_int mod;
   4485 	u_int len;
   4486 	int error;
   4487 
   4488 	KASSERT(track);
   4489 
   4490 	last_dst = *last_dstp;
   4491 	dstfmt = &last_dst->fmt;
   4492 	srcfmt = &track->inputfmt;
   4493 	srcbuf = &track->freq.srcbuf;
   4494 	error = 0;
   4495 
   4496 	srcfreq = srcfmt->sample_rate;
   4497 	dstfreq = dstfmt->sample_rate;
   4498 	if (srcfreq != dstfreq) {
   4499 		track->freq.dst = last_dst;
   4500 
   4501 		memset(track->freq_prev, 0, sizeof(track->freq_prev));
   4502 		memset(track->freq_curr, 0, sizeof(track->freq_curr));
   4503 
   4504 		/* freq_step is the ratio of src/dst when let dst 65536. */
   4505 		track->freq_step = (uint64_t)srcfreq * 65536 / dstfreq;
   4506 
   4507 		dst_capacity = frame_per_block(track->mixer, dstfmt);
   4508 		mod = (uint64_t)srcfreq * 65536 % dstfreq;
   4509 		track->freq_leap = (mod * dst_capacity + dstfreq / 2) / dstfreq;
   4510 
   4511 		if (track->freq_step < 65536) {
   4512 			track->freq.filter = audio_track_freq_up;
   4513 			/* In order to carry at the first time. */
   4514 			track->freq_current = 65536;
   4515 		} else {
   4516 			track->freq.filter = audio_track_freq_down;
   4517 			track->freq_current = 0;
   4518 		}
   4519 
   4520 		srcbuf->fmt = *dstfmt;
   4521 		srcbuf->fmt.sample_rate = srcfreq;
   4522 
   4523 		srcbuf->head = 0;
   4524 		srcbuf->used = 0;
   4525 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4526 		len = auring_bytelen(srcbuf);
   4527 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4528 
   4529 		arg = &track->freq.arg;
   4530 		arg->srcfmt = &srcbuf->fmt;
   4531 		arg->dstfmt = dstfmt;
   4532 		arg->context = track;
   4533 
   4534 		*last_dstp = srcbuf;
   4535 		return 0;
   4536 	}
   4537 
   4538 	track->freq.filter = NULL;
   4539 	audio_free(srcbuf->mem);
   4540 	return error;
   4541 }
   4542 
   4543 /*
   4544  * There are two unit of buffers; A block buffer and a byte buffer.  Both use
   4545  * audio_ring_t.  Internally, audio data is always handled in block unit.
   4546  * Converting format, sythesizing tracks, transferring from/to the hardware,
   4547  * and etc.  Only one exception is usrbuf.  To transfer with userland, usrbuf
   4548  * is buffered in byte unit.
   4549  * For playing back, write(2) writes arbitrary length of data to usrbuf.
   4550  * When one block is filled, it is sent to the next stage (converting and/or
   4551  * synthesizing).
   4552  * For recording, the rmixer writes one block length of data to input buffer
   4553  * (the bottom stage buffer) each time.  read(2) (converts one block if usrbuf
   4554  * is empty and then) reads arbitrary length of data from usrbuf.
   4555  *
   4556  * The following charts show the data flow and buffer types for playback and
   4557  * recording track.  In this example, both have two conversion stages, codec
   4558  * and freq.  Every [**] represents a buffer described below.
   4559  *
   4560  * On playback track:
   4561  *
   4562  *               write(2)
   4563  *                |
   4564  *                | uiomove
   4565  *                v
   4566  *  usrbuf       [BB|BB ... BB|BB]     .. Byte ring buffer
   4567  *                |
   4568  *                | memcpy one block
   4569  *                v
   4570  *  codec.srcbuf [FF]                  .. 1 block (ring) buffer
   4571  *       .dst ----+
   4572  *                |
   4573  *                | convert
   4574  *                v
   4575  *  freq.srcbuf  [FF]                  .. 1 block (ring) buffer
   4576  *      .dst  ----+
   4577  *                |
   4578  *                | convert
   4579  *                v
   4580  *  outbuf       [FF|FF|FF|FF]         .. NBLKOUT blocks ring buffer
   4581  *                |
   4582  *                v
   4583  *               pmixer
   4584  *
   4585  * There are three different types of buffers:
   4586  *
   4587  *  [BB|BB ... BB|BB]  usrbuf.  Is the buffer closest to userland.  Mandatory.
   4588  *                     This is a byte buffer and its length is basically less
   4589  *                     than or equal to 64KB or at least AUMINNOBLK blocks.
   4590  *
   4591  *  [FF]               Interim conversion stage's srcbuf if necessary.
   4592  *                     This is one block (ring) buffer counted in frames.
   4593  *
   4594  *  [FF|FF|FF|FF]      outbuf.  Is the buffer closest to pmixer.  Mandatory.
   4595  *                     This is NBLKOUT blocks ring buffer counted in frames.
   4596  *
   4597  *
   4598  * On recording track:
   4599  *
   4600  *               read(2)
   4601  *                ^
   4602  *                | uiomove
   4603  *                |
   4604  *  usrbuf       [BB]                  .. Byte (ring) buffer
   4605  *                ^
   4606  *                | memcpy one block
   4607  *                |
   4608  *  outbuf       [FF]                  .. 1 block (ring) buffer
   4609  *                ^
   4610  *                | convert
   4611  *                |
   4612  *  codec.dst ----+
   4613  *       .srcbuf [FF]                  .. 1 block (ring) buffer
   4614  *                ^
   4615  *                | convert
   4616  *                |
   4617  *  freq.dst  ----+
   4618  *      .srcbuf  [FF|FF ... FF|FF]     .. NBLKIN blocks ring buffer
   4619  *                ^
   4620  *                |
   4621  *               rmixer
   4622  *
   4623  * There are also three different types of buffers.
   4624  *
   4625  *  [BB]               usrbuf.  Is the buffer closest to userland.  Mandatory.
   4626  *                     This is a byte buffer and its length is one block.
   4627  *                     This buffer holds only "fragment".
   4628  *
   4629  *  [FF]               Interim conversion stage's srcbuf (or outbuf).
   4630  *                     This is one block (ring) buffer counted in frames.
   4631  *
   4632  *  [FF|FF ... FF|FF]  The bottom conversion stage's srcbuf (or outbuf).
   4633  *                     This is the buffer closest to rmixer, and mandatory.
   4634  *                     This is NBLKIN blocks ring buffer counted in frames.
   4635  *                     Also pointed by *input.
   4636  */
   4637 
   4638 /*
   4639  * Set the userland format of this track.
   4640  * usrfmt argument should have been previously verified by
   4641  * audio_track_setinfo_check().
   4642  * This function may release and reallocate all internal conversion buffers.
   4643  * It returns 0 if successful.  Otherwise it returns errno with clearing all
   4644  * internal buffers.
   4645  * It must be called without sc_intr_lock since uvm_* routines require non
   4646  * intr_lock state.
   4647  * It must be called with track lock held since it may release and reallocate
   4648  * outbuf.
   4649  */
   4650 static int
   4651 audio_track_set_format(audio_track_t *track, audio_format2_t *usrfmt)
   4652 {
   4653 	audio_ring_t *last_dst;
   4654 	int is_playback;
   4655 	u_int newbufsize;
   4656 	u_int newvsize;
   4657 	u_int len;
   4658 	int error;
   4659 
   4660 	KASSERT(track);
   4661 
   4662 	is_playback = audio_track_is_playback(track);
   4663 
   4664 	/* Once mmap is called, the track format cannot be changed. */
   4665 	if (track->mmapped)
   4666 		return EIO;
   4667 
   4668 	/* usrbuf is the closest buffer to the userland. */
   4669 	track->usrbuf.fmt = *usrfmt;
   4670 
   4671 	/*
   4672 	 * Usrbuf.
   4673 	 * On the playback track, its capacity is less than or equal to 64KB
   4674 	 * (for historical reason) and must be a multiple of a block
   4675 	 * (constraint in this implementation).  But at least AUMINNOBLK
   4676 	 * blocks.
   4677 	 * On the recording track, its capacity is one block.
   4678 	 */
   4679 	/*
   4680 	 * For references, one block size (in 40msec) is:
   4681 	 *  320 bytes    = 204 blocks/64KB for mulaw/8kHz/1ch
   4682 	 *  7680 bytes   = 8 blocks/64KB for s16/48kHz/2ch
   4683 	 *  30720 bytes  = 90 KB/3blocks for s16/48kHz/8ch
   4684 	 *  61440 bytes  = 180 KB/3blocks for s16/96kHz/8ch
   4685 	 *  245760 bytes = 720 KB/3blocks for s32/192kHz/8ch
   4686 	 *
   4687 	 * For example,
   4688 	 * 1) If usrbuf_blksize = 7056 (s16/44.1k/2ch) and PAGE_SIZE = 8192,
   4689 	 *     newbufsize = rounddown(65536 / 7056) = 63504
   4690 	 *     newvsize = roundup2(63504, PAGE_SIZE) = 65536
   4691 	 *    Therefore it maps 8 * 8K pages and usrbuf->capacity = 63504.
   4692 	 *
   4693 	 * 2) If usrbuf_blksize = 7680 (s16/48k/2ch) and PAGE_SIZE = 4096,
   4694 	 *     newbufsize = rounddown(65536 / 7680) = 61440
   4695 	 *     newvsize = roundup2(61440, PAGE_SIZE) = 61440 (= 15 pages)
   4696 	 *    Therefore it maps 15 * 4K pages and usrbuf->capacity = 61440.
   4697 	 */
   4698 	track->usrbuf_blksize = frametobyte(&track->usrbuf.fmt,
   4699 	    frame_per_block(track->mixer, &track->usrbuf.fmt));
   4700 	track->usrbuf.head = 0;
   4701 	track->usrbuf.used = 0;
   4702 	if (is_playback) {
   4703 		newbufsize = track->usrbuf_blksize * AUMINNOBLK;
   4704 		if (newbufsize < 65536)
   4705 			newbufsize = rounddown(65536, track->usrbuf_blksize);
   4706 		newvsize = roundup2(newbufsize, PAGE_SIZE);
   4707 	} else {
   4708 		newbufsize = track->usrbuf_blksize;
   4709 		newvsize = track->usrbuf_blksize;
   4710 	}
   4711 	/*
   4712 	 * Reallocate only if the number of pages changes.
   4713 	 * This is because we expect kmem to allocate memory on per page
   4714 	 * basis if the request size is about 64KB.
   4715 	 */
   4716 	if (newvsize != track->usrbuf_allocsize) {
   4717 		if (track->usrbuf_allocsize != 0) {
   4718 			kmem_free(track->usrbuf.mem, track->usrbuf_allocsize);
   4719 		}
   4720 		TRACET(2, track, "usrbuf_allocsize %d -> %d",
   4721 		    track->usrbuf_allocsize, newvsize);
   4722 		track->usrbuf.mem = kmem_alloc(newvsize, KM_SLEEP);
   4723 		track->usrbuf_allocsize = newvsize;
   4724 	}
   4725 	track->usrbuf.capacity = newbufsize;
   4726 
   4727 	/* Recalc water mark. */
   4728 	if (is_playback) {
   4729 		/* Set high at 100%, low at 75%. */
   4730 		track->usrbuf_usedhigh = track->usrbuf.capacity;
   4731 		track->usrbuf_usedlow = track->usrbuf.capacity * 3 / 4;
   4732 	} else {
   4733 		/* Set high at 100%, low at 0%. (But not used) */
   4734 		track->usrbuf_usedhigh = track->usrbuf.capacity;
   4735 		track->usrbuf_usedlow = 0;
   4736 	}
   4737 
   4738 	/* Stage buffer */
   4739 	last_dst = &track->outbuf;
   4740 	if (is_playback) {
   4741 		/* On playback, initialize from the mixer side in order. */
   4742 		track->inputfmt = *usrfmt;
   4743 		track->outbuf.fmt =  track->mixer->track_fmt;
   4744 
   4745 		if ((error = audio_track_init_freq(track, &last_dst)) != 0)
   4746 			goto error;
   4747 		if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
   4748 			goto error;
   4749 		if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
   4750 			goto error;
   4751 		if ((error = audio_track_init_codec(track, &last_dst)) != 0)
   4752 			goto error;
   4753 	} else {
   4754 		/* On recording, initialize from userland side in order. */
   4755 		track->inputfmt = track->mixer->track_fmt;
   4756 		track->outbuf.fmt = *usrfmt;
   4757 
   4758 		if ((error = audio_track_init_codec(track, &last_dst)) != 0)
   4759 			goto error;
   4760 		if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
   4761 			goto error;
   4762 		if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
   4763 			goto error;
   4764 		if ((error = audio_track_init_freq(track, &last_dst)) != 0)
   4765 			goto error;
   4766 	}
   4767 #if 0
   4768 	/* debug */
   4769 	if (track->freq.filter) {
   4770 		audio_print_format2("freq src", &track->freq.srcbuf.fmt);
   4771 		audio_print_format2("freq dst", &track->freq.dst->fmt);
   4772 	}
   4773 	if (track->chmix.filter) {
   4774 		audio_print_format2("chmix src", &track->chmix.srcbuf.fmt);
   4775 		audio_print_format2("chmix dst", &track->chmix.dst->fmt);
   4776 	}
   4777 	if (track->chvol.filter) {
   4778 		audio_print_format2("chvol src", &track->chvol.srcbuf.fmt);
   4779 		audio_print_format2("chvol dst", &track->chvol.dst->fmt);
   4780 	}
   4781 	if (track->codec.filter) {
   4782 		audio_print_format2("codec src", &track->codec.srcbuf.fmt);
   4783 		audio_print_format2("codec dst", &track->codec.dst->fmt);
   4784 	}
   4785 #endif
   4786 
   4787 	/* Stage input buffer */
   4788 	track->input = last_dst;
   4789 
   4790 	/*
   4791 	 * Output buffer.
   4792 	 * On the playback track, its capacity is NBLKOUT blocks.
   4793 	 * On the recording track, its capacity is 1 block.
   4794 	 */
   4795 	track->outbuf.head = 0;
   4796 	track->outbuf.used = 0;
   4797 	track->outbuf.capacity = frame_per_block(track->mixer,
   4798 	    &track->outbuf.fmt);
   4799 	if (is_playback)
   4800 		track->outbuf.capacity *= NBLKOUT;
   4801 	len = auring_bytelen(&track->outbuf);
   4802 	track->outbuf.mem = audio_realloc(track->outbuf.mem, len);
   4803 
   4804 	/*
   4805 	 * On the recording track, expand the input stage buffer, which is
   4806 	 * the closest buffer to rmixer, to NBLKIN blocks.
   4807 	 * Note that input buffer may point to outbuf.
   4808 	 */
   4809 	if (!is_playback) {
   4810 		int input_fpb;
   4811 
   4812 		input_fpb = frame_per_block(track->mixer, &track->input->fmt);
   4813 		track->input->capacity = input_fpb * NBLKIN;
   4814 		len = auring_bytelen(track->input);
   4815 		track->input->mem = audio_realloc(track->input->mem, len);
   4816 	}
   4817 
   4818 #if defined(AUDIO_DEBUG)
   4819 	if (audiodebug >= 3) {
   4820 		struct audio_track_debugbuf m;
   4821 
   4822 		memset(&m, 0, sizeof(m));
   4823 		snprintf(m.outbuf, sizeof(m.outbuf), " out=%d",
   4824 		    track->outbuf.capacity * frametobyte(&track->outbuf.fmt,1));
   4825 		if (track->freq.filter)
   4826 			snprintf(m.freq, sizeof(m.freq), " freq=%d",
   4827 			    track->freq.srcbuf.capacity *
   4828 			    frametobyte(&track->freq.srcbuf.fmt, 1));
   4829 		if (track->chmix.filter)
   4830 			snprintf(m.chmix, sizeof(m.chmix), " chmix=%d",
   4831 			    track->chmix.srcbuf.capacity *
   4832 			    frametobyte(&track->chmix.srcbuf.fmt, 1));
   4833 		if (track->chvol.filter)
   4834 			snprintf(m.chvol, sizeof(m.chvol), " chvol=%d",
   4835 			    track->chvol.srcbuf.capacity *
   4836 			    frametobyte(&track->chvol.srcbuf.fmt, 1));
   4837 		if (track->codec.filter)
   4838 			snprintf(m.codec, sizeof(m.codec), " codec=%d",
   4839 			    track->codec.srcbuf.capacity *
   4840 			    frametobyte(&track->codec.srcbuf.fmt, 1));
   4841 		snprintf(m.usrbuf, sizeof(m.usrbuf),
   4842 		    " usr=%d", track->usrbuf.capacity);
   4843 
   4844 		if (is_playback) {
   4845 			TRACET(0, track, "bufsize%s%s%s%s%s%s",
   4846 			    m.outbuf, m.freq, m.chmix,
   4847 			    m.chvol, m.codec, m.usrbuf);
   4848 		} else {
   4849 			TRACET(0, track, "bufsize%s%s%s%s%s%s",
   4850 			    m.freq, m.chmix, m.chvol,
   4851 			    m.codec, m.outbuf, m.usrbuf);
   4852 		}
   4853 	}
   4854 #endif
   4855 	return 0;
   4856 
   4857 error:
   4858 	audio_free_usrbuf(track);
   4859 	audio_free(track->codec.srcbuf.mem);
   4860 	audio_free(track->chvol.srcbuf.mem);
   4861 	audio_free(track->chmix.srcbuf.mem);
   4862 	audio_free(track->freq.srcbuf.mem);
   4863 	audio_free(track->outbuf.mem);
   4864 	return error;
   4865 }
   4866 
   4867 /*
   4868  * Fill silence frames (as the internal format) up to 1 block
   4869  * if the ring is not empty and less than 1 block.
   4870  * It returns the number of appended frames.
   4871  */
   4872 static int
   4873 audio_append_silence(audio_track_t *track, audio_ring_t *ring)
   4874 {
   4875 	int fpb;
   4876 	int n;
   4877 
   4878 	KASSERT(track);
   4879 	KASSERT(audio_format2_is_internal(&ring->fmt));
   4880 
   4881 	/* XXX is n correct? */
   4882 	/* XXX memset uses frametobyte()? */
   4883 
   4884 	if (ring->used == 0)
   4885 		return 0;
   4886 
   4887 	fpb = frame_per_block(track->mixer, &ring->fmt);
   4888 	if (ring->used >= fpb)
   4889 		return 0;
   4890 
   4891 	n = (ring->capacity - ring->used) % fpb;
   4892 
   4893 	KASSERTMSG(auring_get_contig_free(ring) >= n,
   4894 	    "auring_get_contig_free(ring)=%d n=%d",
   4895 	    auring_get_contig_free(ring), n);
   4896 
   4897 	memset(auring_tailptr_aint(ring), 0,
   4898 	    n * ring->fmt.channels * sizeof(aint_t));
   4899 	auring_push(ring, n);
   4900 	return n;
   4901 }
   4902 
   4903 /*
   4904  * Execute the conversion stage.
   4905  * It prepares arg from this stage and executes stage->filter.
   4906  * It must be called only if stage->filter is not NULL.
   4907  *
   4908  * For stages other than frequency conversion, the function increments
   4909  * src and dst counters here.  For frequency conversion stage, on the
   4910  * other hand, the function does not touch src and dst counters and
   4911  * filter side has to increment them.
   4912  */
   4913 static void
   4914 audio_apply_stage(audio_track_t *track, audio_stage_t *stage, bool isfreq)
   4915 {
   4916 	audio_filter_arg_t *arg;
   4917 	int srccount;
   4918 	int dstcount;
   4919 	int count;
   4920 
   4921 	KASSERT(track);
   4922 	KASSERT(stage->filter);
   4923 
   4924 	srccount = auring_get_contig_used(&stage->srcbuf);
   4925 	dstcount = auring_get_contig_free(stage->dst);
   4926 
   4927 	if (isfreq) {
   4928 		KASSERTMSG(srccount > 0, "freq but srccount=%d", srccount);
   4929 		count = uimin(dstcount, track->mixer->frames_per_block);
   4930 	} else {
   4931 		count = uimin(srccount, dstcount);
   4932 	}
   4933 
   4934 	if (count > 0) {
   4935 		arg = &stage->arg;
   4936 		arg->src = auring_headptr(&stage->srcbuf);
   4937 		arg->dst = auring_tailptr(stage->dst);
   4938 		arg->count = count;
   4939 
   4940 		stage->filter(arg);
   4941 
   4942 		if (!isfreq) {
   4943 			auring_take(&stage->srcbuf, count);
   4944 			auring_push(stage->dst, count);
   4945 		}
   4946 	}
   4947 }
   4948 
   4949 /*
   4950  * Produce output buffer for playback from user input buffer.
   4951  * It must be called only if usrbuf is not empty and outbuf is
   4952  * available at least one free block.
   4953  */
   4954 static void
   4955 audio_track_play(audio_track_t *track)
   4956 {
   4957 	audio_ring_t *usrbuf;
   4958 	audio_ring_t *input;
   4959 	int count;
   4960 	int framesize;
   4961 	int bytes;
   4962 
   4963 	KASSERT(track);
   4964 	KASSERT(track->lock);
   4965 	TRACET(4, track, "start pstate=%d", track->pstate);
   4966 
   4967 	/* At this point usrbuf must not be empty. */
   4968 	KASSERT(track->usrbuf.used > 0);
   4969 	/* Also, outbuf must be available at least one block. */
   4970 	count = auring_get_contig_free(&track->outbuf);
   4971 	KASSERTMSG(count >= frame_per_block(track->mixer, &track->outbuf.fmt),
   4972 	    "count=%d fpb=%d",
   4973 	    count, frame_per_block(track->mixer, &track->outbuf.fmt));
   4974 
   4975 	usrbuf = &track->usrbuf;
   4976 	input = track->input;
   4977 
   4978 	/*
   4979 	 * framesize is always 1 byte or more since all formats supported as
   4980 	 * usrfmt(=input) have 8bit or more stride.
   4981 	 */
   4982 	framesize = frametobyte(&input->fmt, 1);
   4983 	KASSERT(framesize >= 1);
   4984 
   4985 	/* The next stage of usrbuf (=input) must be available. */
   4986 	KASSERT(auring_get_contig_free(input) > 0);
   4987 
   4988 	/*
   4989 	 * Copy usrbuf up to 1block to input buffer.
   4990 	 * count is the number of frames to copy from usrbuf.
   4991 	 * bytes is the number of bytes to copy from usrbuf.  However it is
   4992 	 * not copied less than one frame.
   4993 	 */
   4994 	count = uimin(usrbuf->used, track->usrbuf_blksize) / framesize;
   4995 	bytes = count * framesize;
   4996 
   4997 	if (usrbuf->head + bytes < usrbuf->capacity) {
   4998 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   4999 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   5000 		    bytes);
   5001 		auring_push(input, count);
   5002 		auring_take(usrbuf, bytes);
   5003 	} else {
   5004 		int bytes1;
   5005 		int bytes2;
   5006 
   5007 		bytes1 = auring_get_contig_used(usrbuf);
   5008 		KASSERTMSG(bytes1 % framesize == 0,
   5009 		    "bytes1=%d framesize=%d", bytes1, framesize);
   5010 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   5011 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   5012 		    bytes1);
   5013 		auring_push(input, bytes1 / framesize);
   5014 		auring_take(usrbuf, bytes1);
   5015 
   5016 		bytes2 = bytes - bytes1;
   5017 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   5018 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   5019 		    bytes2);
   5020 		auring_push(input, bytes2 / framesize);
   5021 		auring_take(usrbuf, bytes2);
   5022 	}
   5023 
   5024 	/* Encoding conversion */
   5025 	if (track->codec.filter)
   5026 		audio_apply_stage(track, &track->codec, false);
   5027 
   5028 	/* Channel volume */
   5029 	if (track->chvol.filter)
   5030 		audio_apply_stage(track, &track->chvol, false);
   5031 
   5032 	/* Channel mix */
   5033 	if (track->chmix.filter)
   5034 		audio_apply_stage(track, &track->chmix, false);
   5035 
   5036 	/* Frequency conversion */
   5037 	/*
   5038 	 * Since the frequency conversion needs correction for each block,
   5039 	 * it rounds up to 1 block.
   5040 	 */
   5041 	if (track->freq.filter) {
   5042 		int n;
   5043 		n = audio_append_silence(track, &track->freq.srcbuf);
   5044 		if (n > 0) {
   5045 			TRACET(4, track,
   5046 			    "freq.srcbuf add silence %d -> %d/%d/%d",
   5047 			    n,
   5048 			    track->freq.srcbuf.head,
   5049 			    track->freq.srcbuf.used,
   5050 			    track->freq.srcbuf.capacity);
   5051 		}
   5052 		if (track->freq.srcbuf.used > 0) {
   5053 			audio_apply_stage(track, &track->freq, true);
   5054 		}
   5055 	}
   5056 
   5057 	if (bytes < track->usrbuf_blksize) {
   5058 		/*
   5059 		 * Clear all conversion buffer pointer if the conversion was
   5060 		 * not exactly one block.  These conversion stage buffers are
   5061 		 * certainly circular buffers because of symmetry with the
   5062 		 * previous and next stage buffer.  However, since they are
   5063 		 * treated as simple contiguous buffers in operation, so head
   5064 		 * always should point 0.  This may happen during drain-age.
   5065 		 */
   5066 		TRACET(4, track, "reset stage");
   5067 		if (track->codec.filter) {
   5068 			KASSERT(track->codec.srcbuf.used == 0);
   5069 			track->codec.srcbuf.head = 0;
   5070 		}
   5071 		if (track->chvol.filter) {
   5072 			KASSERT(track->chvol.srcbuf.used == 0);
   5073 			track->chvol.srcbuf.head = 0;
   5074 		}
   5075 		if (track->chmix.filter) {
   5076 			KASSERT(track->chmix.srcbuf.used == 0);
   5077 			track->chmix.srcbuf.head = 0;
   5078 		}
   5079 		if (track->freq.filter) {
   5080 			KASSERT(track->freq.srcbuf.used == 0);
   5081 			track->freq.srcbuf.head = 0;
   5082 		}
   5083 	}
   5084 
   5085 	track->stamp++;
   5086 
   5087 #if defined(AUDIO_DEBUG)
   5088 	if (audiodebug >= 3) {
   5089 		struct audio_track_debugbuf m;
   5090 		audio_track_bufstat(track, &m);
   5091 		TRACET(0, track, "end%s%s%s%s%s%s",
   5092 		    m.outbuf, m.freq, m.chvol, m.chmix, m.codec, m.usrbuf);
   5093 	}
   5094 #endif
   5095 }
   5096 
   5097 /*
   5098  * Produce user output buffer for recording from input buffer.
   5099  */
   5100 static void
   5101 audio_track_record(audio_track_t *track)
   5102 {
   5103 	audio_ring_t *outbuf;
   5104 	audio_ring_t *usrbuf;
   5105 	int count;
   5106 	int bytes;
   5107 	int framesize;
   5108 
   5109 	KASSERT(track);
   5110 	KASSERT(track->lock);
   5111 
   5112 	if (auring_get_contig_used(track->input) == 0) {
   5113 		TRACET(4, track, "input->used == 0");
   5114 		return;
   5115 	}
   5116 
   5117 	/* Frequency conversion */
   5118 	if (track->freq.filter) {
   5119 		if (track->freq.srcbuf.used > 0) {
   5120 			audio_apply_stage(track, &track->freq, true);
   5121 			/* XXX should input of freq be from beginning of buf? */
   5122 		}
   5123 	}
   5124 
   5125 	/* Channel mix */
   5126 	if (track->chmix.filter)
   5127 		audio_apply_stage(track, &track->chmix, false);
   5128 
   5129 	/* Channel volume */
   5130 	if (track->chvol.filter)
   5131 		audio_apply_stage(track, &track->chvol, false);
   5132 
   5133 	/* Encoding conversion */
   5134 	if (track->codec.filter)
   5135 		audio_apply_stage(track, &track->codec, false);
   5136 
   5137 	/* Copy outbuf to usrbuf */
   5138 	outbuf = &track->outbuf;
   5139 	usrbuf = &track->usrbuf;
   5140 	/* usrbuf should be empty. */
   5141 	KASSERT(usrbuf->used == 0);
   5142 	/*
   5143 	 * framesize is always 1 byte or more since all formats supported
   5144 	 * as usrfmt(=output) have 8bit or more stride.
   5145 	 */
   5146 	framesize = frametobyte(&outbuf->fmt, 1);
   5147 	KASSERT(framesize >= 1);
   5148 	/*
   5149 	 * count is the number of frames to copy to usrbuf.
   5150 	 * bytes is the number of bytes to copy to usrbuf.
   5151 	 */
   5152 	count = outbuf->used;
   5153 	count = uimin(count, track->usrbuf_blksize / framesize);
   5154 	bytes = count * framesize;
   5155 	if (auring_tail(usrbuf) + bytes < usrbuf->capacity) {
   5156 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   5157 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   5158 		    bytes);
   5159 		auring_push(usrbuf, bytes);
   5160 		auring_take(outbuf, count);
   5161 	} else {
   5162 		int bytes1;
   5163 		int bytes2;
   5164 
   5165 		bytes1 = auring_get_contig_free(usrbuf);
   5166 		KASSERTMSG(bytes1 % framesize == 0,
   5167 		    "bytes1=%d framesize=%d", bytes1, framesize);
   5168 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   5169 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   5170 		    bytes1);
   5171 		auring_push(usrbuf, bytes1);
   5172 		auring_take(outbuf, bytes1 / framesize);
   5173 
   5174 		bytes2 = bytes - bytes1;
   5175 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   5176 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   5177 		    bytes2);
   5178 		auring_push(usrbuf, bytes2);
   5179 		auring_take(outbuf, bytes2 / framesize);
   5180 	}
   5181 
   5182 #if defined(AUDIO_DEBUG)
   5183 	if (audiodebug >= 3) {
   5184 		struct audio_track_debugbuf m;
   5185 		audio_track_bufstat(track, &m);
   5186 		TRACET(0, track, "end%s%s%s%s%s%s",
   5187 		    m.freq, m.chvol, m.chmix, m.codec, m.outbuf, m.usrbuf);
   5188 	}
   5189 #endif
   5190 }
   5191 
   5192 /*
   5193  * Calculate blktime [msec] from mixer(.hwbuf.fmt).
   5194  * Must be called with sc_exlock held.
   5195  */
   5196 static u_int
   5197 audio_mixer_calc_blktime(struct audio_softc *sc, audio_trackmixer_t *mixer)
   5198 {
   5199 	audio_format2_t *fmt;
   5200 	u_int blktime;
   5201 	u_int frames_per_block;
   5202 
   5203 	KASSERT(sc->sc_exlock);
   5204 
   5205 	fmt = &mixer->hwbuf.fmt;
   5206 	blktime = sc->sc_blk_ms;
   5207 
   5208 	/*
   5209 	 * If stride is not multiples of 8, special treatment is necessary.
   5210 	 * For now, it is only x68k's vs(4), 4 bit/sample ADPCM.
   5211 	 */
   5212 	if (fmt->stride == 4) {
   5213 		frames_per_block = fmt->sample_rate * blktime / 1000;
   5214 		if ((frames_per_block & 1) != 0)
   5215 			blktime *= 2;
   5216 	}
   5217 #ifdef DIAGNOSTIC
   5218 	else if (fmt->stride % NBBY != 0) {
   5219 		panic("unsupported HW stride %d", fmt->stride);
   5220 	}
   5221 #endif
   5222 
   5223 	return blktime;
   5224 }
   5225 
   5226 /*
   5227  * Initialize the mixer corresponding to the mode.
   5228  * Set AUMODE_PLAY to the 'mode' for playback or AUMODE_RECORD for recording.
   5229  * sc->sc_[pr]mixer (corresponding to the 'mode') must be zero-filled.
   5230  * This function returns 0 on successful.  Otherwise returns errno.
   5231  * Must be called with sc_exlock held and without sc_lock held.
   5232  */
   5233 static int
   5234 audio_mixer_init(struct audio_softc *sc, int mode,
   5235 	const audio_format2_t *hwfmt, const audio_filter_reg_t *reg)
   5236 {
   5237 	char codecbuf[64];
   5238 	char blkdmsbuf[8];
   5239 	audio_trackmixer_t *mixer;
   5240 	void (*softint_handler)(void *);
   5241 	int len;
   5242 	int blksize;
   5243 	int capacity;
   5244 	size_t bufsize;
   5245 	int hwblks;
   5246 	int blkms;
   5247 	int blkdms;
   5248 	int error;
   5249 
   5250 	KASSERT(hwfmt != NULL);
   5251 	KASSERT(reg != NULL);
   5252 	KASSERT(sc->sc_exlock);
   5253 
   5254 	error = 0;
   5255 	if (mode == AUMODE_PLAY)
   5256 		mixer = sc->sc_pmixer;
   5257 	else
   5258 		mixer = sc->sc_rmixer;
   5259 
   5260 	mixer->sc = sc;
   5261 	mixer->mode = mode;
   5262 
   5263 	mixer->hwbuf.fmt = *hwfmt;
   5264 	mixer->volume = 256;
   5265 	mixer->blktime_d = 1000;
   5266 	mixer->blktime_n = audio_mixer_calc_blktime(sc, mixer);
   5267 	sc->sc_blk_ms = mixer->blktime_n;
   5268 	hwblks = NBLKHW;
   5269 
   5270 	mixer->frames_per_block = frame_per_block(mixer, &mixer->hwbuf.fmt);
   5271 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   5272 	if (sc->hw_if->round_blocksize) {
   5273 		int rounded;
   5274 		audio_params_t p = format2_to_params(&mixer->hwbuf.fmt);
   5275 		mutex_enter(sc->sc_lock);
   5276 		rounded = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
   5277 		    mode, &p);
   5278 		mutex_exit(sc->sc_lock);
   5279 		TRACE(1, "round_blocksize %d -> %d", blksize, rounded);
   5280 		if (rounded != blksize) {
   5281 			if ((rounded * NBBY) % (mixer->hwbuf.fmt.stride *
   5282 			    mixer->hwbuf.fmt.channels) != 0) {
   5283 				audio_printf(sc,
   5284 				    "round_blocksize returned blocksize "
   5285 				    "indivisible by framesize: "
   5286 				    "blksize=%d rounded=%d "
   5287 				    "stride=%ubit channels=%u\n",
   5288 				    blksize, rounded,
   5289 				    mixer->hwbuf.fmt.stride,
   5290 				    mixer->hwbuf.fmt.channels);
   5291 				return EINVAL;
   5292 			}
   5293 			/* Recalculation */
   5294 			blksize = rounded;
   5295 			mixer->frames_per_block = blksize * NBBY /
   5296 			    (mixer->hwbuf.fmt.stride *
   5297 			     mixer->hwbuf.fmt.channels);
   5298 		}
   5299 	}
   5300 	mixer->blktime_n = mixer->frames_per_block;
   5301 	mixer->blktime_d = mixer->hwbuf.fmt.sample_rate;
   5302 
   5303 	capacity = mixer->frames_per_block * hwblks;
   5304 	bufsize = frametobyte(&mixer->hwbuf.fmt, capacity);
   5305 	if (sc->hw_if->round_buffersize) {
   5306 		size_t rounded;
   5307 		mutex_enter(sc->sc_lock);
   5308 		rounded = sc->hw_if->round_buffersize(sc->hw_hdl, mode,
   5309 		    bufsize);
   5310 		mutex_exit(sc->sc_lock);
   5311 		TRACE(1, "round_buffersize %zd -> %zd", bufsize, rounded);
   5312 		if (rounded < bufsize) {
   5313 			/* buffersize needs NBLKHW blocks at least. */
   5314 			audio_printf(sc,
   5315 			    "round_buffersize returned too small buffersize: "
   5316 			    "buffersize=%zd blksize=%d\n",
   5317 			    rounded, blksize);
   5318 			return EINVAL;
   5319 		}
   5320 		if (rounded % blksize != 0) {
   5321 			/* buffersize/blksize constraint mismatch? */
   5322 			audio_printf(sc,
   5323 			    "round_buffersize returned buffersize indivisible "
   5324 			    "by blksize: buffersize=%zu blksize=%d\n",
   5325 			    rounded, blksize);
   5326 			return EINVAL;
   5327 		}
   5328 		if (rounded != bufsize) {
   5329 			/* Recalculation */
   5330 			bufsize = rounded;
   5331 			hwblks = bufsize / blksize;
   5332 			capacity = mixer->frames_per_block * hwblks;
   5333 		}
   5334 	}
   5335 	TRACE(1, "buffersize for %s = %zu",
   5336 	    (mode == AUMODE_PLAY) ? "playback" : "recording",
   5337 	    bufsize);
   5338 	mixer->hwbuf.capacity = capacity;
   5339 
   5340 	if (sc->hw_if->allocm) {
   5341 		/* sc_lock is not necessary for allocm */
   5342 		mixer->hwbuf.mem = sc->hw_if->allocm(sc->hw_hdl, mode, bufsize);
   5343 		if (mixer->hwbuf.mem == NULL) {
   5344 			audio_printf(sc, "allocm(%zu) failed\n", bufsize);
   5345 			return ENOMEM;
   5346 		}
   5347 	} else {
   5348 		mixer->hwbuf.mem = kmem_alloc(bufsize, KM_SLEEP);
   5349 	}
   5350 
   5351 	/* From here, audio_mixer_destroy is necessary to exit. */
   5352 	if (mode == AUMODE_PLAY) {
   5353 		cv_init(&mixer->outcv, "audiowr");
   5354 	} else {
   5355 		cv_init(&mixer->outcv, "audiord");
   5356 	}
   5357 
   5358 	if (mode == AUMODE_PLAY) {
   5359 		softint_handler = audio_softintr_wr;
   5360 	} else {
   5361 		softint_handler = audio_softintr_rd;
   5362 	}
   5363 	mixer->sih = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
   5364 	    softint_handler, sc);
   5365 	if (mixer->sih == NULL) {
   5366 		device_printf(sc->sc_dev, "softint_establish failed\n");
   5367 		goto abort;
   5368 	}
   5369 
   5370 	mixer->track_fmt.encoding = AUDIO_ENCODING_SLINEAR_NE;
   5371 	mixer->track_fmt.precision = AUDIO_INTERNAL_BITS;
   5372 	mixer->track_fmt.stride = AUDIO_INTERNAL_BITS;
   5373 	mixer->track_fmt.channels = mixer->hwbuf.fmt.channels;
   5374 	mixer->track_fmt.sample_rate = mixer->hwbuf.fmt.sample_rate;
   5375 
   5376 	if (mixer->hwbuf.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
   5377 	    mixer->hwbuf.fmt.precision == AUDIO_INTERNAL_BITS) {
   5378 		mixer->swap_endian = true;
   5379 		TRACE(1, "swap_endian");
   5380 	}
   5381 
   5382 	if (mode == AUMODE_PLAY) {
   5383 		/* Mixing buffer */
   5384 		mixer->mixfmt = mixer->track_fmt;
   5385 		mixer->mixfmt.precision *= 2;
   5386 		mixer->mixfmt.stride *= 2;
   5387 		/* XXX TODO: use some macros? */
   5388 		len = mixer->frames_per_block * mixer->mixfmt.channels *
   5389 		    mixer->mixfmt.stride / NBBY;
   5390 		mixer->mixsample = audio_realloc(mixer->mixsample, len);
   5391 	} else if (reg->codec == NULL) {
   5392 		/*
   5393 		 * Recording requires an input conversion buffer
   5394 		 * unless the hardware provides a codec itself
   5395 		 */
   5396 		mixer->mixfmt = mixer->track_fmt;
   5397 		len = mixer->frames_per_block * mixer->mixfmt.channels *
   5398 		    mixer->mixfmt.stride / NBBY;
   5399 		mixer->mixsample = audio_realloc(mixer->mixsample, len);
   5400 	}
   5401 
   5402 	if (reg->codec) {
   5403 		mixer->codec = reg->codec;
   5404 		mixer->codecarg.context = reg->context;
   5405 		if (mode == AUMODE_PLAY) {
   5406 			mixer->codecarg.srcfmt = &mixer->track_fmt;
   5407 			mixer->codecarg.dstfmt = &mixer->hwbuf.fmt;
   5408 		} else {
   5409 			mixer->codecarg.srcfmt = &mixer->hwbuf.fmt;
   5410 			mixer->codecarg.dstfmt = &mixer->track_fmt;
   5411 		}
   5412 		mixer->codecbuf.fmt = mixer->track_fmt;
   5413 		mixer->codecbuf.capacity = mixer->frames_per_block;
   5414 		len = auring_bytelen(&mixer->codecbuf);
   5415 		mixer->codecbuf.mem = audio_realloc(mixer->codecbuf.mem, len);
   5416 	}
   5417 
   5418 	/* Succeeded so display it. */
   5419 	codecbuf[0] = '\0';
   5420 	if (mixer->codec || mixer->swap_endian) {
   5421 		snprintf(codecbuf, sizeof(codecbuf), " %s %s:%d",
   5422 		    (mode == AUMODE_PLAY) ? "->" : "<-",
   5423 		    audio_encoding_name(mixer->hwbuf.fmt.encoding),
   5424 		    mixer->hwbuf.fmt.precision);
   5425 	}
   5426 	blkms = mixer->blktime_n * 1000 / mixer->blktime_d;
   5427 	blkdms = (mixer->blktime_n * 10000 / mixer->blktime_d) % 10;
   5428 	blkdmsbuf[0] = '\0';
   5429 	if (blkdms != 0) {
   5430 		snprintf(blkdmsbuf, sizeof(blkdmsbuf), ".%1d", blkdms);
   5431 	}
   5432 	aprint_normal_dev(sc->sc_dev,
   5433 	    "%s:%d%s %dch %dHz, blk %d bytes (%d%sms) for %s\n",
   5434 	    audio_encoding_name(mixer->track_fmt.encoding),
   5435 	    mixer->track_fmt.precision,
   5436 	    codecbuf,
   5437 	    mixer->track_fmt.channels,
   5438 	    mixer->track_fmt.sample_rate,
   5439 	    blksize,
   5440 	    blkms, blkdmsbuf,
   5441 	    (mode == AUMODE_PLAY) ? "playback" : "recording");
   5442 
   5443 	return 0;
   5444 
   5445 abort:
   5446 	audio_mixer_destroy(sc, mixer);
   5447 	return error;
   5448 }
   5449 
   5450 /*
   5451  * Releases all resources of 'mixer'.
   5452  * Note that it does not release the memory area of 'mixer' itself.
   5453  * Must be called with sc_exlock held and without sc_lock held.
   5454  */
   5455 static void
   5456 audio_mixer_destroy(struct audio_softc *sc, audio_trackmixer_t *mixer)
   5457 {
   5458 	int bufsize;
   5459 
   5460 	KASSERT(sc->sc_exlock == 1);
   5461 
   5462 	bufsize = frametobyte(&mixer->hwbuf.fmt, mixer->hwbuf.capacity);
   5463 
   5464 	if (mixer->hwbuf.mem != NULL) {
   5465 		if (sc->hw_if->freem) {
   5466 			/* sc_lock is not necessary for freem */
   5467 			sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, bufsize);
   5468 		} else {
   5469 			kmem_free(mixer->hwbuf.mem, bufsize);
   5470 		}
   5471 		mixer->hwbuf.mem = NULL;
   5472 	}
   5473 
   5474 	audio_free(mixer->codecbuf.mem);
   5475 	audio_free(mixer->mixsample);
   5476 
   5477 	cv_destroy(&mixer->outcv);
   5478 
   5479 	if (mixer->sih) {
   5480 		softint_disestablish(mixer->sih);
   5481 		mixer->sih = NULL;
   5482 	}
   5483 }
   5484 
   5485 /*
   5486  * Starts playback mixer.
   5487  * Must be called only if sc_pbusy is false.
   5488  * Must be called with sc_lock && sc_exlock held.
   5489  * Must not be called from the interrupt context.
   5490  */
   5491 static void
   5492 audio_pmixer_start(struct audio_softc *sc, bool force)
   5493 {
   5494 	audio_trackmixer_t *mixer;
   5495 	int minimum;
   5496 
   5497 	KASSERT(mutex_owned(sc->sc_lock));
   5498 	KASSERT(sc->sc_exlock);
   5499 	KASSERT(sc->sc_pbusy == false);
   5500 
   5501 	mutex_enter(sc->sc_intr_lock);
   5502 
   5503 	mixer = sc->sc_pmixer;
   5504 	TRACE(2, "%smixseq=%d hwseq=%d hwbuf=%d/%d/%d%s",
   5505 	    (audiodebug >= 3) ? "begin " : "",
   5506 	    (int)mixer->mixseq, (int)mixer->hwseq,
   5507 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
   5508 	    force ? " force" : "");
   5509 
   5510 	/* Need two blocks to start normally. */
   5511 	minimum = (force) ? 1 : 2;
   5512 	while (mixer->hwbuf.used < mixer->frames_per_block * minimum) {
   5513 		audio_pmixer_process(sc);
   5514 	}
   5515 
   5516 	/* Start output */
   5517 	audio_pmixer_output(sc);
   5518 	sc->sc_pbusy = true;
   5519 
   5520 	TRACE(3, "end   mixseq=%d hwseq=%d hwbuf=%d/%d/%d",
   5521 	    (int)mixer->mixseq, (int)mixer->hwseq,
   5522 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5523 
   5524 	mutex_exit(sc->sc_intr_lock);
   5525 }
   5526 
   5527 /*
   5528  * When playing back with MD filter:
   5529  *
   5530  *           track track ...
   5531  *               v v
   5532  *                +  mix (with aint2_t)
   5533  *                |  master volume (with aint2_t)
   5534  *                v
   5535  *    mixsample [::::]                  wide-int 1 block (ring) buffer
   5536  *                |
   5537  *                |  convert aint2_t -> aint_t
   5538  *                v
   5539  *    codecbuf  [....]                  1 block (ring) buffer
   5540  *                |
   5541  *                |  convert to hw format
   5542  *                v
   5543  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5544  *
   5545  * When playing back without MD filter:
   5546  *
   5547  *    mixsample [::::]                  wide-int 1 block (ring) buffer
   5548  *                |
   5549  *                |  convert aint2_t -> aint_t
   5550  *                |  (with byte swap if necessary)
   5551  *                v
   5552  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5553  *
   5554  * mixsample: slinear_NE, wide internal precision, HW ch, HW freq.
   5555  * codecbuf:  slinear_NE, internal precision,      HW ch, HW freq.
   5556  * hwbuf:     HW encoding, HW precision,           HW ch, HW freq.
   5557  */
   5558 
   5559 /*
   5560  * Performs track mixing and converts it to hwbuf.
   5561  * Note that this function doesn't transfer hwbuf to hardware.
   5562  * Must be called with sc_intr_lock held.
   5563  */
   5564 static void
   5565 audio_pmixer_process(struct audio_softc *sc)
   5566 {
   5567 	audio_trackmixer_t *mixer;
   5568 	audio_file_t *f;
   5569 	int frame_count;
   5570 	int sample_count;
   5571 	int mixed;
   5572 	int i;
   5573 	aint2_t *m;
   5574 	aint_t *h;
   5575 
   5576 	mixer = sc->sc_pmixer;
   5577 
   5578 	frame_count = mixer->frames_per_block;
   5579 	KASSERTMSG(auring_get_contig_free(&mixer->hwbuf) >= frame_count,
   5580 	    "auring_get_contig_free()=%d frame_count=%d",
   5581 	    auring_get_contig_free(&mixer->hwbuf), frame_count);
   5582 	sample_count = frame_count * mixer->mixfmt.channels;
   5583 
   5584 	mixer->mixseq++;
   5585 
   5586 	/* Mix all tracks */
   5587 	mixed = 0;
   5588 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   5589 		audio_track_t *track = f->ptrack;
   5590 
   5591 		if (track == NULL)
   5592 			continue;
   5593 
   5594 		if (track->is_pause) {
   5595 			TRACET(4, track, "skip; paused");
   5596 			continue;
   5597 		}
   5598 
   5599 		/* Skip if the track is used by process context. */
   5600 		if (audio_track_lock_tryenter(track) == false) {
   5601 			TRACET(4, track, "skip; in use");
   5602 			continue;
   5603 		}
   5604 
   5605 		/* Emulate mmap'ped track */
   5606 		if (track->mmapped) {
   5607 			auring_push(&track->usrbuf, track->usrbuf_blksize);
   5608 			TRACET(4, track, "mmap; usr=%d/%d/C%d",
   5609 			    track->usrbuf.head,
   5610 			    track->usrbuf.used,
   5611 			    track->usrbuf.capacity);
   5612 		}
   5613 
   5614 		if (track->outbuf.used < mixer->frames_per_block &&
   5615 		    track->usrbuf.used > 0) {
   5616 			TRACET(4, track, "process");
   5617 			audio_track_play(track);
   5618 		}
   5619 
   5620 		if (track->outbuf.used > 0) {
   5621 			mixed = audio_pmixer_mix_track(mixer, track, mixed);
   5622 		} else {
   5623 			TRACET(4, track, "skip; empty");
   5624 		}
   5625 
   5626 		audio_track_lock_exit(track);
   5627 	}
   5628 
   5629 	if (mixed == 0) {
   5630 		/* Silence */
   5631 		memset(mixer->mixsample, 0,
   5632 		    frametobyte(&mixer->mixfmt, frame_count));
   5633 	} else {
   5634 		if (mixed > 1) {
   5635 			/* If there are multiple tracks, do auto gain control */
   5636 			audio_pmixer_agc(mixer, sample_count);
   5637 		}
   5638 
   5639 		/* Apply master volume */
   5640 		if (mixer->volume < 256) {
   5641 			m = mixer->mixsample;
   5642 			for (i = 0; i < sample_count; i++) {
   5643 				*m = AUDIO_SCALEDOWN(*m * mixer->volume, 8);
   5644 				m++;
   5645 			}
   5646 
   5647 			/*
   5648 			 * Recover the volume gradually at the pace of
   5649 			 * several times per second.  If it's too fast, you
   5650 			 * can recognize that the volume changes up and down
   5651 			 * quickly and it's not so comfortable.
   5652 			 */
   5653 			mixer->voltimer += mixer->blktime_n;
   5654 			if (mixer->voltimer * 4 >= mixer->blktime_d) {
   5655 				mixer->volume++;
   5656 				mixer->voltimer = 0;
   5657 #if defined(AUDIO_DEBUG_AGC)
   5658 				TRACE(1, "volume recover: %d", mixer->volume);
   5659 #endif
   5660 			}
   5661 		}
   5662 	}
   5663 
   5664 	/*
   5665 	 * The rest is the hardware part.
   5666 	 */
   5667 
   5668 	m = mixer->mixsample;
   5669 
   5670 	if (mixer->codec) {
   5671 		TRACE(4, "codec count=%d", frame_count);
   5672 
   5673 		h = auring_tailptr_aint(&mixer->codecbuf);
   5674 		for (i=0; i<sample_count; ++i)
   5675 			*h++ = *m++;
   5676 
   5677 		/* Hardware driver's codec */
   5678 		auring_push(&mixer->codecbuf, frame_count);
   5679 		mixer->codecarg.src = auring_headptr(&mixer->codecbuf);
   5680 		mixer->codecarg.dst = auring_tailptr(&mixer->hwbuf);
   5681 		mixer->codecarg.count = frame_count;
   5682 		mixer->codec(&mixer->codecarg);
   5683 		auring_take(&mixer->codecbuf, mixer->codecarg.count);
   5684 	} else {
   5685 		TRACE(4, "direct count=%d", frame_count);
   5686 
   5687 		/* Direct conversion to linear output */
   5688 		mixer->codecarg.src = m;
   5689 		mixer->codecarg.dst = auring_tailptr(&mixer->hwbuf);
   5690 		mixer->codecarg.count = frame_count;
   5691 		mixer->codecarg.srcfmt = &mixer->mixfmt;
   5692 		mixer->codecarg.dstfmt = &mixer->hwbuf.fmt;
   5693 		audio_mixsample_to_linear(&mixer->codecarg);
   5694 	}
   5695 
   5696 	auring_push(&mixer->hwbuf, frame_count);
   5697 
   5698 	TRACE(4, "done mixseq=%d hwbuf=%d/%d/%d%s",
   5699 	    (int)mixer->mixseq,
   5700 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
   5701 	    (mixed == 0) ? " silent" : "");
   5702 }
   5703 
   5704 /*
   5705  * Do auto gain control.
   5706  * Must be called sc_intr_lock held.
   5707  */
   5708 static void
   5709 audio_pmixer_agc(audio_trackmixer_t *mixer, int sample_count)
   5710 {
   5711 	struct audio_softc *sc __unused;
   5712 	aint2_t val;
   5713 	aint2_t maxval;
   5714 	aint2_t minval;
   5715 	aint2_t over_plus;
   5716 	aint2_t over_minus;
   5717 	aint2_t *m;
   5718 	int newvol;
   5719 	int i;
   5720 
   5721 	sc = mixer->sc;
   5722 
   5723 	/* Overflow detection */
   5724 	maxval = AINT_T_MAX;
   5725 	minval = AINT_T_MIN;
   5726 	m = mixer->mixsample;
   5727 	for (i = 0; i < sample_count; i++) {
   5728 		val = *m++;
   5729 		if (val > maxval)
   5730 			maxval = val;
   5731 		else if (val < minval)
   5732 			minval = val;
   5733 	}
   5734 
   5735 	/* Absolute value of overflowed amount */
   5736 	over_plus = maxval - AINT_T_MAX;
   5737 	over_minus = AINT_T_MIN - minval;
   5738 
   5739 	if (over_plus > 0 || over_minus > 0) {
   5740 		if (over_plus > over_minus) {
   5741 			newvol = (int)((aint2_t)AINT_T_MAX * 256 / maxval);
   5742 		} else {
   5743 			newvol = (int)((aint2_t)AINT_T_MIN * 256 / minval);
   5744 		}
   5745 
   5746 		/*
   5747 		 * Change the volume only if new one is smaller.
   5748 		 * Reset the timer even if the volume isn't changed.
   5749 		 */
   5750 		if (newvol <= mixer->volume) {
   5751 			mixer->volume = newvol;
   5752 			mixer->voltimer = 0;
   5753 #if defined(AUDIO_DEBUG_AGC)
   5754 			TRACE(1, "auto volume adjust: %d", mixer->volume);
   5755 #endif
   5756 		}
   5757 	}
   5758 }
   5759 
   5760 /*
   5761  * Mix one track.
   5762  * 'mixed' specifies the number of tracks mixed so far.
   5763  * It returns the number of tracks mixed.  In other words, it returns
   5764  * mixed + 1 if this track is mixed.
   5765  */
   5766 static int
   5767 audio_pmixer_mix_track(audio_trackmixer_t *mixer, audio_track_t *track,
   5768 	int mixed)
   5769 {
   5770 	int count;
   5771 	int sample_count;
   5772 	int remain;
   5773 	int i;
   5774 	const aint_t *s;
   5775 	aint2_t *d;
   5776 
   5777 	/* XXX TODO: Is this necessary for now? */
   5778 	if (mixer->mixseq < track->seq)
   5779 		return mixed;
   5780 
   5781 	count = auring_get_contig_used(&track->outbuf);
   5782 	count = uimin(count, mixer->frames_per_block);
   5783 
   5784 	s = auring_headptr_aint(&track->outbuf);
   5785 	d = mixer->mixsample;
   5786 
   5787 	/*
   5788 	 * Apply track volume with double-sized integer and perform
   5789 	 * additive synthesis.
   5790 	 *
   5791 	 * XXX If you limit the track volume to 1.0 or less (<= 256),
   5792 	 *     it would be better to do this in the track conversion stage
   5793 	 *     rather than here.  However, if you accept the volume to
   5794 	 *     be greater than 1.0 (> 256), it's better to do it here.
   5795 	 *     Because the operation here is done by double-sized integer.
   5796 	 */
   5797 	sample_count = count * mixer->mixfmt.channels;
   5798 	if (mixed == 0) {
   5799 		/* If this is the first track, assignment can be used. */
   5800 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   5801 		if (track->volume != 256) {
   5802 			for (i = 0; i < sample_count; i++) {
   5803 				aint2_t v;
   5804 				v = *s++;
   5805 				*d++ = AUDIO_SCALEDOWN(v * track->volume, 8)
   5806 			}
   5807 		} else
   5808 #endif
   5809 		{
   5810 			for (i = 0; i < sample_count; i++) {
   5811 				*d++ = ((aint2_t)*s++);
   5812 			}
   5813 		}
   5814 		/* Fill silence if the first track is not filled. */
   5815 		for (; i < mixer->frames_per_block * mixer->mixfmt.channels; i++)
   5816 			*d++ = 0;
   5817 	} else {
   5818 		/* If this is the second or later, add it. */
   5819 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   5820 		if (track->volume != 256) {
   5821 			for (i = 0; i < sample_count; i++) {
   5822 				aint2_t v;
   5823 				v = *s++;
   5824 				*d++ += AUDIO_SCALEDOWN(v * track->volume, 8);
   5825 			}
   5826 		} else
   5827 #endif
   5828 		{
   5829 			for (i = 0; i < sample_count; i++) {
   5830 				*d++ += ((aint2_t)*s++);
   5831 			}
   5832 		}
   5833 	}
   5834 
   5835 	auring_take(&track->outbuf, count);
   5836 	/*
   5837 	 * The counters have to align block even if outbuf is less than
   5838 	 * one block. XXX Is this still necessary?
   5839 	 */
   5840 	remain = mixer->frames_per_block - count;
   5841 	if (__predict_false(remain != 0)) {
   5842 		auring_push(&track->outbuf, remain);
   5843 		auring_take(&track->outbuf, remain);
   5844 	}
   5845 
   5846 	/*
   5847 	 * Update track sequence.
   5848 	 * mixseq has previous value yet at this point.
   5849 	 */
   5850 	track->seq = mixer->mixseq + 1;
   5851 
   5852 	return mixed + 1;
   5853 }
   5854 
   5855 /*
   5856  * Output one block from hwbuf to HW.
   5857  * Must be called with sc_intr_lock held.
   5858  */
   5859 static void
   5860 audio_pmixer_output(struct audio_softc *sc)
   5861 {
   5862 	audio_trackmixer_t *mixer;
   5863 	audio_params_t params;
   5864 	void *start;
   5865 	void *end;
   5866 	int blksize;
   5867 	int error;
   5868 
   5869 	mixer = sc->sc_pmixer;
   5870 	TRACE(4, "pbusy=%d hwbuf=%d/%d/%d",
   5871 	    sc->sc_pbusy,
   5872 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5873 	KASSERTMSG(mixer->hwbuf.used >= mixer->frames_per_block,
   5874 	    "mixer->hwbuf.used=%d mixer->frames_per_block=%d",
   5875 	    mixer->hwbuf.used, mixer->frames_per_block);
   5876 
   5877 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   5878 
   5879 	if (sc->hw_if->trigger_output) {
   5880 		/* trigger (at once) */
   5881 		if (!sc->sc_pbusy) {
   5882 			start = mixer->hwbuf.mem;
   5883 			end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
   5884 			params = format2_to_params(&mixer->hwbuf.fmt);
   5885 
   5886 			error = sc->hw_if->trigger_output(sc->hw_hdl,
   5887 			    start, end, blksize, audio_pintr, sc, &params);
   5888 			if (error) {
   5889 				audio_printf(sc,
   5890 				    "trigger_output failed: errno=%d\n",
   5891 				    error);
   5892 				return;
   5893 			}
   5894 		}
   5895 	} else {
   5896 		/* start (everytime) */
   5897 		start = auring_headptr(&mixer->hwbuf);
   5898 
   5899 		error = sc->hw_if->start_output(sc->hw_hdl,
   5900 		    start, blksize, audio_pintr, sc);
   5901 		if (error) {
   5902 			audio_printf(sc,
   5903 			    "start_output failed: errno=%d\n", error);
   5904 			return;
   5905 		}
   5906 	}
   5907 }
   5908 
   5909 /*
   5910  * This is an interrupt handler for playback.
   5911  * It is called with sc_intr_lock held.
   5912  *
   5913  * It is usually called from hardware interrupt.  However, note that
   5914  * for some drivers (e.g. uaudio) it is called from software interrupt.
   5915  */
   5916 static void
   5917 audio_pintr(void *arg)
   5918 {
   5919 	struct audio_softc *sc;
   5920 	audio_trackmixer_t *mixer;
   5921 
   5922 	sc = arg;
   5923 	KASSERT(mutex_owned(sc->sc_intr_lock));
   5924 
   5925 	if (sc->sc_dying)
   5926 		return;
   5927 	if (sc->sc_pbusy == false) {
   5928 #if defined(DIAGNOSTIC)
   5929 		audio_printf(sc, "DIAGNOSTIC: %s raised stray interrupt\n",
   5930 		    device_xname(sc->hw_dev));
   5931 #endif
   5932 		return;
   5933 	}
   5934 
   5935 	mixer = sc->sc_pmixer;
   5936 	mixer->hw_complete_counter += mixer->frames_per_block;
   5937 	mixer->hwseq++;
   5938 
   5939 	auring_take(&mixer->hwbuf, mixer->frames_per_block);
   5940 
   5941 	TRACE(4,
   5942 	    "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
   5943 	    mixer->hwseq, mixer->hw_complete_counter,
   5944 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5945 
   5946 #if defined(AUDIO_HW_SINGLE_BUFFER)
   5947 	/*
   5948 	 * Create a new block here and output it immediately.
   5949 	 * It makes a latency lower but needs machine power.
   5950 	 */
   5951 	audio_pmixer_process(sc);
   5952 	audio_pmixer_output(sc);
   5953 #else
   5954 	/*
   5955 	 * It is called when block N output is done.
   5956 	 * Output immediately block N+1 created by the last interrupt.
   5957 	 * And then create block N+2 for the next interrupt.
   5958 	 * This method makes playback robust even on slower machines.
   5959 	 * Instead the latency is increased by one block.
   5960 	 */
   5961 
   5962 	/* At first, output ready block. */
   5963 	if (mixer->hwbuf.used >= mixer->frames_per_block) {
   5964 		audio_pmixer_output(sc);
   5965 	}
   5966 
   5967 	bool later = false;
   5968 
   5969 	if (mixer->hwbuf.used < mixer->frames_per_block) {
   5970 		later = true;
   5971 	}
   5972 
   5973 	/* Then, process next block. */
   5974 	audio_pmixer_process(sc);
   5975 
   5976 	if (later) {
   5977 		audio_pmixer_output(sc);
   5978 	}
   5979 #endif
   5980 
   5981 	/*
   5982 	 * When this interrupt is the real hardware interrupt, disabling
   5983 	 * preemption here is not necessary.  But some drivers (e.g. uaudio)
   5984 	 * emulate it by software interrupt, so kpreempt_disable is necessary.
   5985 	 */
   5986 	kpreempt_disable();
   5987 	softint_schedule(mixer->sih);
   5988 	kpreempt_enable();
   5989 }
   5990 
   5991 /*
   5992  * Starts record mixer.
   5993  * Must be called only if sc_rbusy is false.
   5994  * Must be called with sc_lock && sc_exlock held.
   5995  * Must not be called from the interrupt context.
   5996  */
   5997 static void
   5998 audio_rmixer_start(struct audio_softc *sc)
   5999 {
   6000 
   6001 	KASSERT(mutex_owned(sc->sc_lock));
   6002 	KASSERT(sc->sc_exlock);
   6003 	KASSERT(sc->sc_rbusy == false);
   6004 
   6005 	mutex_enter(sc->sc_intr_lock);
   6006 
   6007 	TRACE(2, "%s", (audiodebug >= 3) ? "begin" : "");
   6008 	audio_rmixer_input(sc);
   6009 	sc->sc_rbusy = true;
   6010 	TRACE(3, "end");
   6011 
   6012 	mutex_exit(sc->sc_intr_lock);
   6013 }
   6014 
   6015 /*
   6016  * When recording with MD filter:
   6017  *
   6018  *    hwbuf     [............]          NBLKHW blocks ring buffer
   6019  *                |
   6020  *                | convert from hw format
   6021  *                v
   6022  *    codecbuf  [....]                  1 block (ring) buffer
   6023  *               |  |
   6024  *               v  v
   6025  *            track track ...
   6026  *
   6027  * When recording without MD filter:
   6028  *
   6029  *    hwbuf     [............]          NBLKHW blocks ring buffer
   6030  *               |  |
   6031  *               v  v
   6032  *            track track ...
   6033  *
   6034  * hwbuf:     HW encoding, HW precision, HW ch, HW freq.
   6035  * codecbuf:  slinear_NE, internal precision, HW ch, HW freq.
   6036  */
   6037 
   6038 /*
   6039  * Distribute a recorded block to all recording tracks.
   6040  */
   6041 static void
   6042 audio_rmixer_process(struct audio_softc *sc)
   6043 {
   6044 	audio_trackmixer_t *mixer;
   6045 	audio_ring_t *mixersrc;
   6046 	audio_ring_t tmpsrc;
   6047 	audio_filter_t codec;
   6048 	audio_filter_arg_t codecarg;
   6049 	audio_file_t *f;
   6050 	int count;
   6051 	int bytes;
   6052 
   6053 	mixer = sc->sc_rmixer;
   6054 
   6055 	/*
   6056 	 * count is the number of frames to be retrieved this time.
   6057 	 * count should be one block.
   6058 	 */
   6059 	count = auring_get_contig_used(&mixer->hwbuf);
   6060 	count = uimin(count, mixer->frames_per_block);
   6061 	if (count <= 0) {
   6062 		TRACE(4, "count %d: too short", count);
   6063 		return;
   6064 	}
   6065 	bytes = frametobyte(&mixer->track_fmt, count);
   6066 
   6067 	/* Hardware driver's codec */
   6068 	if (mixer->codec) {
   6069 		TRACE(4, "codec count=%d", count);
   6070 		mixer->codecarg.src = auring_headptr(&mixer->hwbuf);
   6071 		mixer->codecarg.dst = auring_tailptr(&mixer->codecbuf);
   6072 		mixer->codecarg.count = count;
   6073 		mixer->codec(&mixer->codecarg);
   6074 		mixersrc = &mixer->codecbuf;
   6075 	} else {
   6076 		TRACE(4, "direct count=%d", count);
   6077 		/* temporary ring using mixsample buffer */
   6078 		tmpsrc.fmt = mixer->mixfmt;
   6079 		tmpsrc.capacity = mixer->frames_per_block;
   6080 		tmpsrc.mem = mixer->mixsample;
   6081 		tmpsrc.head = 0;
   6082 		tmpsrc.used = 0;
   6083 
   6084 		/* ad-hoc codec */
   6085 		codecarg.srcfmt = &mixer->hwbuf.fmt;
   6086 		codecarg.dstfmt = &mixer->mixfmt;
   6087 		codec = NULL;
   6088 		if (audio_format2_is_linear(codecarg.srcfmt)) {
   6089 			switch (codecarg.srcfmt->stride) {
   6090 			case 8:
   6091 				codec = audio_linear8_to_internal;
   6092 				break;
   6093 			case 16:
   6094 				codec = audio_linear16_to_internal;
   6095 				break;
   6096 #if defined(AUDIO_SUPPORT_LINEAR24)
   6097 			case 24:
   6098 				codec = audio_linear24_to_internal;
   6099 				break;
   6100 #endif
   6101 			case 32:
   6102 				codec = audio_linear32_to_internal;
   6103 				break;
   6104 			}
   6105 		}
   6106 		if (codec == NULL) {
   6107 			TRACE(4, "unsupported hw format");
   6108 			return;
   6109 		}
   6110 
   6111 		codecarg.src = auring_headptr(&mixer->hwbuf);
   6112 		codecarg.dst = auring_tailptr(&tmpsrc);
   6113 		codecarg.count = count;
   6114 		codec(&codecarg);
   6115 		mixersrc = &tmpsrc;
   6116 	}
   6117 
   6118 	auring_take(&mixer->hwbuf, count);
   6119 	auring_push(mixersrc, count);
   6120 
   6121 	TRACE(4, "distribute");
   6122 
   6123 	/* Distribute to all tracks. */
   6124 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   6125 		audio_track_t *track = f->rtrack;
   6126 		audio_ring_t *input;
   6127 
   6128 		if (track == NULL)
   6129 			continue;
   6130 
   6131 		if (track->is_pause) {
   6132 			TRACET(4, track, "skip; paused");
   6133 			continue;
   6134 		}
   6135 
   6136 		if (audio_track_lock_tryenter(track) == false) {
   6137 			TRACET(4, track, "skip; in use");
   6138 			continue;
   6139 		}
   6140 
   6141 		/*
   6142 		 * If the track buffer has less than one block of free space,
   6143 		 * make one block free.
   6144 		 */
   6145 		input = track->input;
   6146 		if (input->capacity - input->used < mixer->frames_per_block) {
   6147 			int drops = mixer->frames_per_block -
   6148 			    (input->capacity - input->used);
   6149 			track->dropframes += drops;
   6150 			TRACET(4, track, "drop %d frames: inp=%d/%d/%d",
   6151 			    drops,
   6152 			    input->head, input->used, input->capacity);
   6153 			auring_take(input, drops);
   6154 		}
   6155 
   6156 		KASSERTMSG(auring_tail(input) % mixer->frames_per_block == 0,
   6157 		    "inputtail=%d mixer->frames_per_block=%d",
   6158 		    auring_tail(input), mixer->frames_per_block);
   6159 		memcpy(auring_tailptr_aint(input),
   6160 		    auring_headptr_aint(mixersrc),
   6161 		    bytes);
   6162 		auring_push(input, count);
   6163 
   6164 		track->stamp++;
   6165 
   6166 		audio_track_lock_exit(track);
   6167 	}
   6168 
   6169 	auring_take(mixersrc, count);
   6170 }
   6171 
   6172 /*
   6173  * Input one block from HW to hwbuf.
   6174  * Must be called with sc_intr_lock held.
   6175  */
   6176 static void
   6177 audio_rmixer_input(struct audio_softc *sc)
   6178 {
   6179 	audio_trackmixer_t *mixer;
   6180 	audio_params_t params;
   6181 	void *start;
   6182 	void *end;
   6183 	int blksize;
   6184 	int error;
   6185 
   6186 	mixer = sc->sc_rmixer;
   6187 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   6188 
   6189 	if (sc->hw_if->trigger_input) {
   6190 		/* trigger (at once) */
   6191 		if (!sc->sc_rbusy) {
   6192 			start = mixer->hwbuf.mem;
   6193 			end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
   6194 			params = format2_to_params(&mixer->hwbuf.fmt);
   6195 
   6196 			error = sc->hw_if->trigger_input(sc->hw_hdl,
   6197 			    start, end, blksize, audio_rintr, sc, &params);
   6198 			if (error) {
   6199 				audio_printf(sc,
   6200 				    "trigger_input failed: errno=%d\n",
   6201 				    error);
   6202 				return;
   6203 			}
   6204 		}
   6205 	} else {
   6206 		/* start (everytime) */
   6207 		start = auring_tailptr(&mixer->hwbuf);
   6208 
   6209 		error = sc->hw_if->start_input(sc->hw_hdl,
   6210 		    start, blksize, audio_rintr, sc);
   6211 		if (error) {
   6212 			audio_printf(sc,
   6213 			    "start_input failed: errno=%d\n", error);
   6214 			return;
   6215 		}
   6216 	}
   6217 }
   6218 
   6219 /*
   6220  * This is an interrupt handler for recording.
   6221  * It is called with sc_intr_lock.
   6222  *
   6223  * It is usually called from hardware interrupt.  However, note that
   6224  * for some drivers (e.g. uaudio) it is called from software interrupt.
   6225  */
   6226 static void
   6227 audio_rintr(void *arg)
   6228 {
   6229 	struct audio_softc *sc;
   6230 	audio_trackmixer_t *mixer;
   6231 
   6232 	sc = arg;
   6233 	KASSERT(mutex_owned(sc->sc_intr_lock));
   6234 
   6235 	if (sc->sc_dying)
   6236 		return;
   6237 	if (sc->sc_rbusy == false) {
   6238 #if defined(DIAGNOSTIC)
   6239 		audio_printf(sc, "DIAGNOSTIC: %s raised stray interrupt\n",
   6240 		    device_xname(sc->hw_dev));
   6241 #endif
   6242 		return;
   6243 	}
   6244 
   6245 	mixer = sc->sc_rmixer;
   6246 	mixer->hw_complete_counter += mixer->frames_per_block;
   6247 	mixer->hwseq++;
   6248 
   6249 	auring_push(&mixer->hwbuf, mixer->frames_per_block);
   6250 
   6251 	TRACE(4,
   6252 	    "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
   6253 	    mixer->hwseq, mixer->hw_complete_counter,
   6254 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   6255 
   6256 	/* Distrubute recorded block */
   6257 	audio_rmixer_process(sc);
   6258 
   6259 	/* Request next block */
   6260 	audio_rmixer_input(sc);
   6261 
   6262 	/*
   6263 	 * When this interrupt is the real hardware interrupt, disabling
   6264 	 * preemption here is not necessary.  But some drivers (e.g. uaudio)
   6265 	 * emulate it by software interrupt, so kpreempt_disable is necessary.
   6266 	 */
   6267 	kpreempt_disable();
   6268 	softint_schedule(mixer->sih);
   6269 	kpreempt_enable();
   6270 }
   6271 
   6272 /*
   6273  * Halts playback mixer.
   6274  * This function also clears related parameters, so call this function
   6275  * instead of calling halt_output directly.
   6276  * Must be called only if sc_pbusy is true.
   6277  * Must be called with sc_lock && sc_exlock held.
   6278  */
   6279 static int
   6280 audio_pmixer_halt(struct audio_softc *sc)
   6281 {
   6282 	int error;
   6283 
   6284 	TRACE(2, "called");
   6285 	KASSERT(mutex_owned(sc->sc_lock));
   6286 	KASSERT(sc->sc_exlock);
   6287 
   6288 	mutex_enter(sc->sc_intr_lock);
   6289 	error = sc->hw_if->halt_output(sc->hw_hdl);
   6290 
   6291 	/* Halts anyway even if some error has occurred. */
   6292 	sc->sc_pbusy = false;
   6293 	sc->sc_pmixer->hwbuf.head = 0;
   6294 	sc->sc_pmixer->hwbuf.used = 0;
   6295 	sc->sc_pmixer->mixseq = 0;
   6296 	sc->sc_pmixer->hwseq = 0;
   6297 	mutex_exit(sc->sc_intr_lock);
   6298 
   6299 	return error;
   6300 }
   6301 
   6302 /*
   6303  * Halts recording mixer.
   6304  * This function also clears related parameters, so call this function
   6305  * instead of calling halt_input directly.
   6306  * Must be called only if sc_rbusy is true.
   6307  * Must be called with sc_lock && sc_exlock held.
   6308  */
   6309 static int
   6310 audio_rmixer_halt(struct audio_softc *sc)
   6311 {
   6312 	int error;
   6313 
   6314 	TRACE(2, "called");
   6315 	KASSERT(mutex_owned(sc->sc_lock));
   6316 	KASSERT(sc->sc_exlock);
   6317 
   6318 	mutex_enter(sc->sc_intr_lock);
   6319 	error = sc->hw_if->halt_input(sc->hw_hdl);
   6320 
   6321 	/* Halts anyway even if some error has occurred. */
   6322 	sc->sc_rbusy = false;
   6323 	sc->sc_rmixer->hwbuf.head = 0;
   6324 	sc->sc_rmixer->hwbuf.used = 0;
   6325 	sc->sc_rmixer->mixseq = 0;
   6326 	sc->sc_rmixer->hwseq = 0;
   6327 	mutex_exit(sc->sc_intr_lock);
   6328 
   6329 	return error;
   6330 }
   6331 
   6332 /*
   6333  * Flush this track.
   6334  * Halts all operations, clears all buffers, reset error counters.
   6335  * XXX I'm not sure...
   6336  */
   6337 static void
   6338 audio_track_clear(struct audio_softc *sc, audio_track_t *track)
   6339 {
   6340 
   6341 	KASSERT(track);
   6342 	TRACET(3, track, "clear");
   6343 
   6344 	audio_track_lock_enter(track);
   6345 
   6346 	/* Clear all internal parameters. */
   6347 	track->usrbuf.used = 0;
   6348 	track->usrbuf.head = 0;
   6349 	if (track->codec.filter) {
   6350 		track->codec.srcbuf.used = 0;
   6351 		track->codec.srcbuf.head = 0;
   6352 	}
   6353 	if (track->chvol.filter) {
   6354 		track->chvol.srcbuf.used = 0;
   6355 		track->chvol.srcbuf.head = 0;
   6356 	}
   6357 	if (track->chmix.filter) {
   6358 		track->chmix.srcbuf.used = 0;
   6359 		track->chmix.srcbuf.head = 0;
   6360 	}
   6361 	if (track->freq.filter) {
   6362 		track->freq.srcbuf.used = 0;
   6363 		track->freq.srcbuf.head = 0;
   6364 		if (track->freq_step < 65536)
   6365 			track->freq_current = 65536;
   6366 		else
   6367 			track->freq_current = 0;
   6368 		memset(track->freq_prev, 0, sizeof(track->freq_prev));
   6369 		memset(track->freq_curr, 0, sizeof(track->freq_curr));
   6370 	}
   6371 	/* Clear buffer, then operation halts naturally. */
   6372 	track->outbuf.used = 0;
   6373 
   6374 	/* Clear counters. */
   6375 	track->stamp = 0;
   6376 	track->last_stamp = 0;
   6377 	track->dropframes = 0;
   6378 
   6379 	audio_track_lock_exit(track);
   6380 }
   6381 
   6382 /*
   6383  * Drain the track.
   6384  * track must be present and for playback.
   6385  * If successful, it returns 0.  Otherwise returns errno.
   6386  * Must be called with sc_lock held.
   6387  */
   6388 static int
   6389 audio_track_drain(struct audio_softc *sc, audio_track_t *track)
   6390 {
   6391 	audio_trackmixer_t *mixer;
   6392 	int done;
   6393 	int error;
   6394 
   6395 	KASSERT(track);
   6396 	TRACET(3, track, "start");
   6397 	mixer = track->mixer;
   6398 	KASSERT(mutex_owned(sc->sc_lock));
   6399 
   6400 	/* Ignore them if pause. */
   6401 	if (track->is_pause) {
   6402 		TRACET(3, track, "pause -> clear");
   6403 		track->pstate = AUDIO_STATE_CLEAR;
   6404 	}
   6405 	/* Terminate early here if there is no data in the track. */
   6406 	if (track->pstate == AUDIO_STATE_CLEAR) {
   6407 		TRACET(3, track, "no need to drain");
   6408 		return 0;
   6409 	}
   6410 	track->pstate = AUDIO_STATE_DRAINING;
   6411 
   6412 	for (;;) {
   6413 		/* I want to display it before condition evaluation. */
   6414 		TRACET(3, track, "pid=%d.%d trkseq=%d hwseq=%d out=%d/%d/%d",
   6415 		    (int)curproc->p_pid, (int)curlwp->l_lid,
   6416 		    (int)track->seq, (int)mixer->hwseq,
   6417 		    track->outbuf.head, track->outbuf.used,
   6418 		    track->outbuf.capacity);
   6419 
   6420 		/* Condition to terminate */
   6421 		audio_track_lock_enter(track);
   6422 		done = (track->usrbuf.used < frametobyte(&track->inputfmt, 1) &&
   6423 		    track->outbuf.used == 0 &&
   6424 		    track->seq <= mixer->hwseq);
   6425 		audio_track_lock_exit(track);
   6426 		if (done)
   6427 			break;
   6428 
   6429 		TRACET(3, track, "sleep");
   6430 		error = audio_track_waitio(sc, track, "audio_drain");
   6431 		if (error)
   6432 			return error;
   6433 
   6434 		/* XXX call audio_track_play here ? */
   6435 	}
   6436 
   6437 	track->pstate = AUDIO_STATE_CLEAR;
   6438 	TRACET(3, track, "done");
   6439 	return 0;
   6440 }
   6441 
   6442 /*
   6443  * Send signal to process.
   6444  * This is intended to be called only from audio_softintr_{rd,wr}.
   6445  * Must be called without sc_intr_lock held.
   6446  */
   6447 static inline void
   6448 audio_psignal(struct audio_softc *sc, pid_t pid, int signum)
   6449 {
   6450 	proc_t *p;
   6451 
   6452 	KASSERT(pid != 0);
   6453 
   6454 	/*
   6455 	 * psignal() must be called without spin lock held.
   6456 	 */
   6457 
   6458 	mutex_enter(&proc_lock);
   6459 	p = proc_find(pid);
   6460 	if (p)
   6461 		psignal(p, signum);
   6462 	mutex_exit(&proc_lock);
   6463 }
   6464 
   6465 /*
   6466  * This is software interrupt handler for record.
   6467  * It is called from recording hardware interrupt everytime.
   6468  * It does:
   6469  * - Deliver SIGIO for all async processes.
   6470  * - Notify to audio_read() that data has arrived.
   6471  * - selnotify() for select/poll-ing processes.
   6472  */
   6473 /*
   6474  * XXX If a process issues FIOASYNC between hardware interrupt and
   6475  *     software interrupt, (stray) SIGIO will be sent to the process
   6476  *     despite the fact that it has not receive recorded data yet.
   6477  */
   6478 static void
   6479 audio_softintr_rd(void *cookie)
   6480 {
   6481 	struct audio_softc *sc = cookie;
   6482 	audio_file_t *f;
   6483 	pid_t pid;
   6484 
   6485 	mutex_enter(sc->sc_lock);
   6486 
   6487 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   6488 		audio_track_t *track = f->rtrack;
   6489 
   6490 		if (track == NULL)
   6491 			continue;
   6492 
   6493 		TRACET(4, track, "broadcast; inp=%d/%d/%d",
   6494 		    track->input->head,
   6495 		    track->input->used,
   6496 		    track->input->capacity);
   6497 
   6498 		pid = f->async_audio;
   6499 		if (pid != 0) {
   6500 			TRACEF(4, f, "sending SIGIO %d", pid);
   6501 			audio_psignal(sc, pid, SIGIO);
   6502 		}
   6503 	}
   6504 
   6505 	/* Notify that data has arrived. */
   6506 	selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
   6507 	cv_broadcast(&sc->sc_rmixer->outcv);
   6508 
   6509 	mutex_exit(sc->sc_lock);
   6510 }
   6511 
   6512 /*
   6513  * This is software interrupt handler for playback.
   6514  * It is called from playback hardware interrupt everytime.
   6515  * It does:
   6516  * - Deliver SIGIO for all async and writable (used < lowat) processes.
   6517  * - Notify to audio_write() that outbuf block available.
   6518  * - selnotify() for select/poll-ing processes if there are any writable
   6519  *   (used < lowat) processes.  Checking each descriptor will be done by
   6520  *   filt_audiowrite_event().
   6521  */
   6522 static void
   6523 audio_softintr_wr(void *cookie)
   6524 {
   6525 	struct audio_softc *sc = cookie;
   6526 	audio_file_t *f;
   6527 	bool found;
   6528 	pid_t pid;
   6529 
   6530 	TRACE(4, "called");
   6531 	found = false;
   6532 
   6533 	mutex_enter(sc->sc_lock);
   6534 
   6535 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   6536 		audio_track_t *track = f->ptrack;
   6537 
   6538 		if (track == NULL)
   6539 			continue;
   6540 
   6541 		TRACET(4, track, "broadcast; trkseq=%d out=%d/%d/%d",
   6542 		    (int)track->seq,
   6543 		    track->outbuf.head,
   6544 		    track->outbuf.used,
   6545 		    track->outbuf.capacity);
   6546 
   6547 		/*
   6548 		 * Send a signal if the process is async mode and
   6549 		 * used is lower than lowat.
   6550 		 */
   6551 		if (track->usrbuf.used <= track->usrbuf_usedlow &&
   6552 		    !track->is_pause) {
   6553 			/* For selnotify */
   6554 			found = true;
   6555 			/* For SIGIO */
   6556 			pid = f->async_audio;
   6557 			if (pid != 0) {
   6558 				TRACEF(4, f, "sending SIGIO %d", pid);
   6559 				audio_psignal(sc, pid, SIGIO);
   6560 			}
   6561 		}
   6562 	}
   6563 
   6564 	/*
   6565 	 * Notify for select/poll when someone become writable.
   6566 	 * It needs sc_lock (and not sc_intr_lock).
   6567 	 */
   6568 	if (found) {
   6569 		TRACE(4, "selnotify");
   6570 		selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT);
   6571 	}
   6572 
   6573 	/* Notify to audio_write() that outbuf available. */
   6574 	cv_broadcast(&sc->sc_pmixer->outcv);
   6575 
   6576 	mutex_exit(sc->sc_lock);
   6577 }
   6578 
   6579 /*
   6580  * Check (and convert) the format *p came from userland.
   6581  * If successful, it writes back the converted format to *p if necessary and
   6582  * returns 0.  Otherwise returns errno (*p may be changed even in this case).
   6583  */
   6584 static int
   6585 audio_check_params(audio_format2_t *p)
   6586 {
   6587 
   6588 	/*
   6589 	 * Convert obsolete AUDIO_ENCODING_PCM encodings.
   6590 	 *
   6591 	 * AUDIO_ENCODING_PCM16 == AUDIO_ENCODING_LINEAR
   6592 	 * So, it's always signed, as in SunOS.
   6593 	 *
   6594 	 * AUDIO_ENCODING_PCM8 == AUDIO_ENCODING_LINEAR8
   6595 	 * So, it's always unsigned, as in SunOS.
   6596 	 */
   6597 	if (p->encoding == AUDIO_ENCODING_PCM16) {
   6598 		p->encoding = AUDIO_ENCODING_SLINEAR;
   6599 	} else if (p->encoding == AUDIO_ENCODING_PCM8) {
   6600 		if (p->precision == 8)
   6601 			p->encoding = AUDIO_ENCODING_ULINEAR;
   6602 		else
   6603 			return EINVAL;
   6604 	}
   6605 
   6606 	/*
   6607 	 * Convert obsoleted AUDIO_ENCODING_[SU]LINEAR without endianness
   6608 	 * suffix.
   6609 	 */
   6610 	if (p->encoding == AUDIO_ENCODING_SLINEAR)
   6611 		p->encoding = AUDIO_ENCODING_SLINEAR_NE;
   6612 	if (p->encoding == AUDIO_ENCODING_ULINEAR)
   6613 		p->encoding = AUDIO_ENCODING_ULINEAR_NE;
   6614 
   6615 	switch (p->encoding) {
   6616 	case AUDIO_ENCODING_ULAW:
   6617 	case AUDIO_ENCODING_ALAW:
   6618 		if (p->precision != 8)
   6619 			return EINVAL;
   6620 		break;
   6621 	case AUDIO_ENCODING_ADPCM:
   6622 		if (p->precision != 4 && p->precision != 8)
   6623 			return EINVAL;
   6624 		break;
   6625 	case AUDIO_ENCODING_SLINEAR_LE:
   6626 	case AUDIO_ENCODING_SLINEAR_BE:
   6627 	case AUDIO_ENCODING_ULINEAR_LE:
   6628 	case AUDIO_ENCODING_ULINEAR_BE:
   6629 		if (p->precision !=  8 && p->precision != 16 &&
   6630 		    p->precision != 24 && p->precision != 32)
   6631 			return EINVAL;
   6632 
   6633 		/* 8bit format does not have endianness. */
   6634 		if (p->precision == 8) {
   6635 			if (p->encoding == AUDIO_ENCODING_SLINEAR_OE)
   6636 				p->encoding = AUDIO_ENCODING_SLINEAR_NE;
   6637 			if (p->encoding == AUDIO_ENCODING_ULINEAR_OE)
   6638 				p->encoding = AUDIO_ENCODING_ULINEAR_NE;
   6639 		}
   6640 
   6641 		if (p->precision > p->stride)
   6642 			return EINVAL;
   6643 		break;
   6644 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   6645 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   6646 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   6647 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   6648 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   6649 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   6650 	case AUDIO_ENCODING_AC3:
   6651 		break;
   6652 	default:
   6653 		return EINVAL;
   6654 	}
   6655 
   6656 	/* sanity check # of channels*/
   6657 	if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS)
   6658 		return EINVAL;
   6659 
   6660 	return 0;
   6661 }
   6662 
   6663 /*
   6664  * Initialize playback and record mixers.
   6665  * mode (AUMODE_{PLAY,RECORD}) indicates the mixer to be initialized.
   6666  * phwfmt and rhwfmt indicate the hardware format.  pfil and rfil indicate
   6667  * the filter registration information.  These four must not be NULL.
   6668  * If successful returns 0.  Otherwise returns errno.
   6669  * Must be called with sc_exlock held and without sc_lock held.
   6670  * Must not be called if there are any tracks.
   6671  * Caller should check that the initialization succeed by whether
   6672  * sc_[pr]mixer is not NULL.
   6673  */
   6674 static int
   6675 audio_mixers_init(struct audio_softc *sc, int mode,
   6676 	const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
   6677 	const audio_filter_reg_t *pfil, const audio_filter_reg_t *rfil)
   6678 {
   6679 	int error;
   6680 
   6681 	KASSERT(phwfmt != NULL);
   6682 	KASSERT(rhwfmt != NULL);
   6683 	KASSERT(pfil != NULL);
   6684 	KASSERT(rfil != NULL);
   6685 	KASSERT(sc->sc_exlock);
   6686 
   6687 	if ((mode & AUMODE_PLAY)) {
   6688 		if (sc->sc_pmixer == NULL) {
   6689 			sc->sc_pmixer = kmem_zalloc(sizeof(*sc->sc_pmixer),
   6690 			    KM_SLEEP);
   6691 		} else {
   6692 			/* destroy() doesn't free memory. */
   6693 			audio_mixer_destroy(sc, sc->sc_pmixer);
   6694 			memset(sc->sc_pmixer, 0, sizeof(*sc->sc_pmixer));
   6695 		}
   6696 		error = audio_mixer_init(sc, AUMODE_PLAY, phwfmt, pfil);
   6697 		if (error) {
   6698 			/* audio_mixer_init already displayed error code */
   6699 			audio_printf(sc, "configuring playback mode failed\n");
   6700 			kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
   6701 			sc->sc_pmixer = NULL;
   6702 			return error;
   6703 		}
   6704 	}
   6705 	if ((mode & AUMODE_RECORD)) {
   6706 		if (sc->sc_rmixer == NULL) {
   6707 			sc->sc_rmixer = kmem_zalloc(sizeof(*sc->sc_rmixer),
   6708 			    KM_SLEEP);
   6709 		} else {
   6710 			/* destroy() doesn't free memory. */
   6711 			audio_mixer_destroy(sc, sc->sc_rmixer);
   6712 			memset(sc->sc_rmixer, 0, sizeof(*sc->sc_rmixer));
   6713 		}
   6714 		error = audio_mixer_init(sc, AUMODE_RECORD, rhwfmt, rfil);
   6715 		if (error) {
   6716 			/* audio_mixer_init already displayed error code */
   6717 			audio_printf(sc, "configuring record mode failed\n");
   6718 			kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
   6719 			sc->sc_rmixer = NULL;
   6720 			return error;
   6721 		}
   6722 	}
   6723 
   6724 	return 0;
   6725 }
   6726 
   6727 /*
   6728  * Select a frequency.
   6729  * Prioritize 48kHz and 44.1kHz.  Otherwise choose the highest one.
   6730  * XXX Better algorithm?
   6731  */
   6732 static int
   6733 audio_select_freq(const struct audio_format *fmt)
   6734 {
   6735 	int freq;
   6736 	int high;
   6737 	int low;
   6738 	int j;
   6739 
   6740 	if (fmt->frequency_type == 0) {
   6741 		low = fmt->frequency[0];
   6742 		high = fmt->frequency[1];
   6743 		freq = 48000;
   6744 		if (low <= freq && freq <= high) {
   6745 			return freq;
   6746 		}
   6747 		freq = 44100;
   6748 		if (low <= freq && freq <= high) {
   6749 			return freq;
   6750 		}
   6751 		return high;
   6752 	} else {
   6753 		for (j = 0; j < fmt->frequency_type; j++) {
   6754 			if (fmt->frequency[j] == 48000) {
   6755 				return fmt->frequency[j];
   6756 			}
   6757 		}
   6758 		high = 0;
   6759 		for (j = 0; j < fmt->frequency_type; j++) {
   6760 			if (fmt->frequency[j] == 44100) {
   6761 				return fmt->frequency[j];
   6762 			}
   6763 			if (fmt->frequency[j] > high) {
   6764 				high = fmt->frequency[j];
   6765 			}
   6766 		}
   6767 		return high;
   6768 	}
   6769 }
   6770 
   6771 /*
   6772  * Choose the most preferred hardware format.
   6773  * If successful, it will store the chosen format into *cand and return 0.
   6774  * Otherwise, return errno.
   6775  * Must be called without sc_lock held.
   6776  */
   6777 static int
   6778 audio_hw_probe(struct audio_softc *sc, audio_format2_t *cand, int mode)
   6779 {
   6780 	audio_format_query_t query;
   6781 	int cand_score;
   6782 	int score;
   6783 	int i;
   6784 	int error;
   6785 
   6786 	/*
   6787 	 * Score each formats and choose the highest one.
   6788 	 *
   6789 	 *                 +---- priority(0-3)
   6790 	 *                 |+--- encoding/precision
   6791 	 *                 ||+-- channels
   6792 	 * score = 0x000000PEC
   6793 	 */
   6794 
   6795 	cand_score = 0;
   6796 	for (i = 0; ; i++) {
   6797 		memset(&query, 0, sizeof(query));
   6798 		query.index = i;
   6799 
   6800 		mutex_enter(sc->sc_lock);
   6801 		error = sc->hw_if->query_format(sc->hw_hdl, &query);
   6802 		mutex_exit(sc->sc_lock);
   6803 		if (error == EINVAL)
   6804 			break;
   6805 		if (error)
   6806 			return error;
   6807 
   6808 #if defined(AUDIO_DEBUG)
   6809 		DPRINTF(1, "fmt[%d] %c%c pri=%d %s,%d/%dbit,%dch,", i,
   6810 		    (query.fmt.mode & AUMODE_PLAY)   ? 'P' : '-',
   6811 		    (query.fmt.mode & AUMODE_RECORD) ? 'R' : '-',
   6812 		    query.fmt.priority,
   6813 		    audio_encoding_name(query.fmt.encoding),
   6814 		    query.fmt.validbits,
   6815 		    query.fmt.precision,
   6816 		    query.fmt.channels);
   6817 		if (query.fmt.frequency_type == 0) {
   6818 			DPRINTF(1, "{%d-%d",
   6819 			    query.fmt.frequency[0], query.fmt.frequency[1]);
   6820 		} else {
   6821 			int j;
   6822 			for (j = 0; j < query.fmt.frequency_type; j++) {
   6823 				DPRINTF(1, "%c%d",
   6824 				    (j == 0) ? '{' : ',',
   6825 				    query.fmt.frequency[j]);
   6826 			}
   6827 		}
   6828 		DPRINTF(1, "}\n");
   6829 #endif
   6830 
   6831 		if ((query.fmt.mode & mode) == 0) {
   6832 			DPRINTF(1, "fmt[%d] skip; mode not match %d\n", i,
   6833 			    mode);
   6834 			continue;
   6835 		}
   6836 
   6837 		if (query.fmt.priority < 0) {
   6838 			DPRINTF(1, "fmt[%d] skip; unsupported encoding\n", i);
   6839 			continue;
   6840 		}
   6841 
   6842 		/* Score */
   6843 		score = (query.fmt.priority & 3) * 0x100;
   6844 		if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_NE &&
   6845 		    query.fmt.validbits == AUDIO_INTERNAL_BITS &&
   6846 		    query.fmt.precision == AUDIO_INTERNAL_BITS) {
   6847 			score += 0x20;
   6848 		} else if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
   6849 		    query.fmt.validbits == AUDIO_INTERNAL_BITS &&
   6850 		    query.fmt.precision == AUDIO_INTERNAL_BITS) {
   6851 			score += 0x10;
   6852 		}
   6853 
   6854 		/* Do not prefer surround formats */
   6855 		if (query.fmt.channels <= 2)
   6856 			score += query.fmt.channels;
   6857 
   6858 		if (score < cand_score) {
   6859 			DPRINTF(1, "fmt[%d] skip; score 0x%x < 0x%x\n", i,
   6860 			    score, cand_score);
   6861 			continue;
   6862 		}
   6863 
   6864 		/* Update candidate */
   6865 		cand_score = score;
   6866 		cand->encoding    = query.fmt.encoding;
   6867 		cand->precision   = query.fmt.validbits;
   6868 		cand->stride      = query.fmt.precision;
   6869 		cand->channels    = query.fmt.channels;
   6870 		cand->sample_rate = audio_select_freq(&query.fmt);
   6871 		DPRINTF(1, "fmt[%d] candidate (score=0x%x)"
   6872 		    " pri=%d %s,%d/%d,%dch,%dHz\n", i,
   6873 		    cand_score, query.fmt.priority,
   6874 		    audio_encoding_name(query.fmt.encoding),
   6875 		    cand->precision, cand->stride,
   6876 		    cand->channels, cand->sample_rate);
   6877 	}
   6878 
   6879 	if (cand_score == 0) {
   6880 		DPRINTF(1, "%s no fmt\n", __func__);
   6881 		return ENXIO;
   6882 	}
   6883 	DPRINTF(1, "%s selected: %s,%d/%d,%dch,%dHz\n", __func__,
   6884 	    audio_encoding_name(cand->encoding),
   6885 	    cand->precision, cand->stride, cand->channels, cand->sample_rate);
   6886 	return 0;
   6887 }
   6888 
   6889 /*
   6890  * Validate fmt with query_format.
   6891  * If fmt is included in the result of query_format, returns 0.
   6892  * Otherwise returns EINVAL.
   6893  * Must be called without sc_lock held.
   6894  */
   6895 static int
   6896 audio_hw_validate_format(struct audio_softc *sc, int mode,
   6897 	const audio_format2_t *fmt)
   6898 {
   6899 	audio_format_query_t query;
   6900 	struct audio_format *q;
   6901 	int index;
   6902 	int error;
   6903 	int j;
   6904 
   6905 	for (index = 0; ; index++) {
   6906 		query.index = index;
   6907 		mutex_enter(sc->sc_lock);
   6908 		error = sc->hw_if->query_format(sc->hw_hdl, &query);
   6909 		mutex_exit(sc->sc_lock);
   6910 		if (error == EINVAL)
   6911 			break;
   6912 		if (error)
   6913 			return error;
   6914 
   6915 		q = &query.fmt;
   6916 		/*
   6917 		 * Note that fmt is audio_format2_t (precision/stride) but
   6918 		 * q is audio_format_t (validbits/precision).
   6919 		 */
   6920 		if ((q->mode & mode) == 0) {
   6921 			continue;
   6922 		}
   6923 		if (fmt->encoding != q->encoding) {
   6924 			continue;
   6925 		}
   6926 		if (fmt->precision != q->validbits) {
   6927 			continue;
   6928 		}
   6929 		if (fmt->stride != q->precision) {
   6930 			continue;
   6931 		}
   6932 		if (fmt->channels != q->channels) {
   6933 			continue;
   6934 		}
   6935 		if (q->frequency_type == 0) {
   6936 			if (fmt->sample_rate < q->frequency[0] ||
   6937 			    fmt->sample_rate > q->frequency[1]) {
   6938 				continue;
   6939 			}
   6940 		} else {
   6941 			for (j = 0; j < q->frequency_type; j++) {
   6942 				if (fmt->sample_rate == q->frequency[j])
   6943 					break;
   6944 			}
   6945 			if (j == query.fmt.frequency_type) {
   6946 				continue;
   6947 			}
   6948 		}
   6949 
   6950 		/* Matched. */
   6951 		return 0;
   6952 	}
   6953 
   6954 	return EINVAL;
   6955 }
   6956 
   6957 /*
   6958  * Set track mixer's format depending on ai->mode.
   6959  * If AUMODE_PLAY is set in ai->mode, it set up the playback mixer
   6960  * with ai.play.*.
   6961  * If AUMODE_RECORD is set in ai->mode, it set up the recording mixer
   6962  * with ai.record.*.
   6963  * All other fields in ai are ignored.
   6964  * If successful returns 0.  Otherwise returns errno.
   6965  * This function does not roll back even if it fails.
   6966  * Must be called with sc_exlock held and without sc_lock held.
   6967  */
   6968 static int
   6969 audio_mixers_set_format(struct audio_softc *sc, const struct audio_info *ai)
   6970 {
   6971 	audio_format2_t phwfmt;
   6972 	audio_format2_t rhwfmt;
   6973 	audio_filter_reg_t pfil;
   6974 	audio_filter_reg_t rfil;
   6975 	int mode;
   6976 	int error;
   6977 
   6978 	KASSERT(sc->sc_exlock);
   6979 
   6980 	/*
   6981 	 * Even when setting either one of playback and recording,
   6982 	 * both must be halted.
   6983 	 */
   6984 	if (sc->sc_popens + sc->sc_ropens > 0)
   6985 		return EBUSY;
   6986 
   6987 	if (!SPECIFIED(ai->mode) || ai->mode == 0)
   6988 		return ENOTTY;
   6989 
   6990 	mode = ai->mode;
   6991 	if ((mode & AUMODE_PLAY)) {
   6992 		phwfmt.encoding    = ai->play.encoding;
   6993 		phwfmt.precision   = ai->play.precision;
   6994 		phwfmt.stride      = ai->play.precision;
   6995 		phwfmt.channels    = ai->play.channels;
   6996 		phwfmt.sample_rate = ai->play.sample_rate;
   6997 	}
   6998 	if ((mode & AUMODE_RECORD)) {
   6999 		rhwfmt.encoding    = ai->record.encoding;
   7000 		rhwfmt.precision   = ai->record.precision;
   7001 		rhwfmt.stride      = ai->record.precision;
   7002 		rhwfmt.channels    = ai->record.channels;
   7003 		rhwfmt.sample_rate = ai->record.sample_rate;
   7004 	}
   7005 
   7006 	/* On non-independent devices, use the same format for both. */
   7007 	if ((sc->sc_props & AUDIO_PROP_INDEPENDENT) == 0) {
   7008 		if (mode == AUMODE_RECORD) {
   7009 			phwfmt = rhwfmt;
   7010 		} else {
   7011 			rhwfmt = phwfmt;
   7012 		}
   7013 		mode = AUMODE_PLAY | AUMODE_RECORD;
   7014 	}
   7015 
   7016 	/* Then, unset the direction not exist on the hardware. */
   7017 	if ((sc->sc_props & AUDIO_PROP_PLAYBACK) == 0)
   7018 		mode &= ~AUMODE_PLAY;
   7019 	if ((sc->sc_props & AUDIO_PROP_CAPTURE) == 0)
   7020 		mode &= ~AUMODE_RECORD;
   7021 
   7022 	/* debug */
   7023 	if ((mode & AUMODE_PLAY)) {
   7024 		TRACE(1, "play=%s/%d/%d/%dch/%dHz",
   7025 		    audio_encoding_name(phwfmt.encoding),
   7026 		    phwfmt.precision,
   7027 		    phwfmt.stride,
   7028 		    phwfmt.channels,
   7029 		    phwfmt.sample_rate);
   7030 	}
   7031 	if ((mode & AUMODE_RECORD)) {
   7032 		TRACE(1, "rec =%s/%d/%d/%dch/%dHz",
   7033 		    audio_encoding_name(rhwfmt.encoding),
   7034 		    rhwfmt.precision,
   7035 		    rhwfmt.stride,
   7036 		    rhwfmt.channels,
   7037 		    rhwfmt.sample_rate);
   7038 	}
   7039 
   7040 	/* Check the format */
   7041 	if ((mode & AUMODE_PLAY)) {
   7042 		if (audio_hw_validate_format(sc, AUMODE_PLAY, &phwfmt)) {
   7043 			TRACE(1, "invalid format");
   7044 			return EINVAL;
   7045 		}
   7046 	}
   7047 	if ((mode & AUMODE_RECORD)) {
   7048 		if (audio_hw_validate_format(sc, AUMODE_RECORD, &rhwfmt)) {
   7049 			TRACE(1, "invalid format");
   7050 			return EINVAL;
   7051 		}
   7052 	}
   7053 
   7054 	/* Configure the mixers. */
   7055 	memset(&pfil, 0, sizeof(pfil));
   7056 	memset(&rfil, 0, sizeof(rfil));
   7057 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   7058 	if (error)
   7059 		return error;
   7060 
   7061 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   7062 	if (error)
   7063 		return error;
   7064 
   7065 	/*
   7066 	 * Reinitialize the sticky parameters for /dev/sound.
   7067 	 * If the number of the hardware channels becomes less than the number
   7068 	 * of channels that sticky parameters remember, subsequent /dev/sound
   7069 	 * open will fail.  To prevent this, reinitialize the sticky
   7070 	 * parameters whenever the hardware format is changed.
   7071 	 */
   7072 	sc->sc_sound_pparams = params_to_format2(&audio_default);
   7073 	sc->sc_sound_rparams = params_to_format2(&audio_default);
   7074 	sc->sc_sound_ppause = false;
   7075 	sc->sc_sound_rpause = false;
   7076 
   7077 	return 0;
   7078 }
   7079 
   7080 /*
   7081  * Store current mixers format into *ai.
   7082  * Must be called with sc_exlock held.
   7083  */
   7084 static void
   7085 audio_mixers_get_format(struct audio_softc *sc, struct audio_info *ai)
   7086 {
   7087 
   7088 	KASSERT(sc->sc_exlock);
   7089 
   7090 	/*
   7091 	 * There is no stride information in audio_info but it doesn't matter.
   7092 	 * trackmixer always treats stride and precision as the same.
   7093 	 */
   7094 	AUDIO_INITINFO(ai);
   7095 	ai->mode = 0;
   7096 	if (sc->sc_pmixer) {
   7097 		audio_format2_t *fmt = &sc->sc_pmixer->track_fmt;
   7098 		ai->play.encoding    = fmt->encoding;
   7099 		ai->play.precision   = fmt->precision;
   7100 		ai->play.channels    = fmt->channels;
   7101 		ai->play.sample_rate = fmt->sample_rate;
   7102 		ai->mode |= AUMODE_PLAY;
   7103 	}
   7104 	if (sc->sc_rmixer) {
   7105 		audio_format2_t *fmt = &sc->sc_rmixer->track_fmt;
   7106 		ai->record.encoding    = fmt->encoding;
   7107 		ai->record.precision   = fmt->precision;
   7108 		ai->record.channels    = fmt->channels;
   7109 		ai->record.sample_rate = fmt->sample_rate;
   7110 		ai->mode |= AUMODE_RECORD;
   7111 	}
   7112 }
   7113 
   7114 /*
   7115  * audio_info details:
   7116  *
   7117  * ai.{play,record}.sample_rate		(R/W)
   7118  * ai.{play,record}.encoding		(R/W)
   7119  * ai.{play,record}.precision		(R/W)
   7120  * ai.{play,record}.channels		(R/W)
   7121  *	These specify the playback or recording format.
   7122  *	Ignore members within an inactive track.
   7123  *
   7124  * ai.mode				(R/W)
   7125  *	It specifies the playback or recording mode, AUMODE_*.
   7126  *	Currently, a mode change operation by ai.mode after opening is
   7127  *	prohibited.  In addition, AUMODE_PLAY_ALL no longer makes sense.
   7128  *	However, it's possible to get or to set for backward compatibility.
   7129  *
   7130  * ai.{hiwat,lowat}			(R/W)
   7131  *	These specify the high water mark and low water mark for playback
   7132  *	track.  The unit is block.
   7133  *
   7134  * ai.{play,record}.gain		(R/W)
   7135  *	It specifies the HW mixer volume in 0-255.
   7136  *	It is historical reason that the gain is connected to HW mixer.
   7137  *
   7138  * ai.{play,record}.balance		(R/W)
   7139  *	It specifies the left-right balance of HW mixer in 0-64.
   7140  *	32 means the center.
   7141  *	It is historical reason that the balance is connected to HW mixer.
   7142  *
   7143  * ai.{play,record}.port		(R/W)
   7144  *	It specifies the input/output port of HW mixer.
   7145  *
   7146  * ai.monitor_gain			(R/W)
   7147  *	It specifies the recording monitor gain(?) of HW mixer.
   7148  *
   7149  * ai.{play,record}.pause		(R/W)
   7150  *	Non-zero means the track is paused.
   7151  *
   7152  * ai.play.seek				(R/-)
   7153  *	It indicates the number of bytes written but not processed.
   7154  * ai.record.seek			(R/-)
   7155  *	It indicates the number of bytes to be able to read.
   7156  *
   7157  * ai.{play,record}.avail_ports		(R/-)
   7158  *	Mixer info.
   7159  *
   7160  * ai.{play,record}.buffer_size		(R/-)
   7161  *	It indicates the buffer size in bytes.  Internally it means usrbuf.
   7162  *
   7163  * ai.{play,record}.samples		(R/-)
   7164  *	It indicates the total number of bytes played or recorded.
   7165  *
   7166  * ai.{play,record}.eof			(R/-)
   7167  *	It indicates the number of times reached EOF(?).
   7168  *
   7169  * ai.{play,record}.error		(R/-)
   7170  *	Non-zero indicates overflow/underflow has occurred.
   7171  *
   7172  * ai.{play,record}.waiting		(R/-)
   7173  *	Non-zero indicates that other process waits to open.
   7174  *	It will never happen anymore.
   7175  *
   7176  * ai.{play,record}.open		(R/-)
   7177  *	Non-zero indicates the direction is opened by this process(?).
   7178  *	XXX Is this better to indicate that "the device is opened by
   7179  *	at least one process"?
   7180  *
   7181  * ai.{play,record}.active		(R/-)
   7182  *	Non-zero indicates that I/O is currently active.
   7183  *
   7184  * ai.blocksize				(R/-)
   7185  *	It indicates the block size in bytes.
   7186  *	XXX The blocksize of playback and recording may be different.
   7187  */
   7188 
   7189 /*
   7190  * Pause consideration:
   7191  *
   7192  * Pausing/unpausing never affect [pr]mixer.  This single rule makes
   7193  * operation simple.  Note that playback and recording are asymmetric.
   7194  *
   7195  * For playback,
   7196  *  1. Any playback open doesn't start pmixer regardless of initial pause
   7197  *     state of this track.
   7198  *  2. The first write access among playback tracks only starts pmixer
   7199  *     regardless of this track's pause state.
   7200  *  3. Even a pause of the last playback track doesn't stop pmixer.
   7201  *  4. The last close of all playback tracks only stops pmixer.
   7202  *
   7203  * For recording,
   7204  *  1. The first recording open only starts rmixer regardless of initial
   7205  *     pause state of this track.
   7206  *  2. Even a pause of the last track doesn't stop rmixer.
   7207  *  3. The last close of all recording tracks only stops rmixer.
   7208  */
   7209 
   7210 /*
   7211  * Set both track's parameters within a file depending on ai.
   7212  * Update sc_sound_[pr]* if set.
   7213  * Must be called with sc_exlock held and without sc_lock held.
   7214  */
   7215 static int
   7216 audio_file_setinfo(struct audio_softc *sc, audio_file_t *file,
   7217 	const struct audio_info *ai)
   7218 {
   7219 	const struct audio_prinfo *pi;
   7220 	const struct audio_prinfo *ri;
   7221 	audio_track_t *ptrack;
   7222 	audio_track_t *rtrack;
   7223 	audio_format2_t pfmt;
   7224 	audio_format2_t rfmt;
   7225 	int pchanges;
   7226 	int rchanges;
   7227 	int mode;
   7228 	struct audio_info saved_ai;
   7229 	audio_format2_t saved_pfmt;
   7230 	audio_format2_t saved_rfmt;
   7231 	int error;
   7232 
   7233 	KASSERT(sc->sc_exlock);
   7234 
   7235 	pi = &ai->play;
   7236 	ri = &ai->record;
   7237 	pchanges = 0;
   7238 	rchanges = 0;
   7239 
   7240 	ptrack = file->ptrack;
   7241 	rtrack = file->rtrack;
   7242 
   7243 #if defined(AUDIO_DEBUG)
   7244 	if (audiodebug >= 2) {
   7245 		char buf[256];
   7246 		char p[64];
   7247 		int buflen;
   7248 		int plen;
   7249 #define SPRINTF(var, fmt...) do {	\
   7250 	var##len += snprintf(var + var##len, sizeof(var) - var##len, fmt); \
   7251 } while (0)
   7252 
   7253 		buflen = 0;
   7254 		plen = 0;
   7255 		if (SPECIFIED(pi->encoding))
   7256 			SPRINTF(p, "/%s", audio_encoding_name(pi->encoding));
   7257 		if (SPECIFIED(pi->precision))
   7258 			SPRINTF(p, "/%dbit", pi->precision);
   7259 		if (SPECIFIED(pi->channels))
   7260 			SPRINTF(p, "/%dch", pi->channels);
   7261 		if (SPECIFIED(pi->sample_rate))
   7262 			SPRINTF(p, "/%dHz", pi->sample_rate);
   7263 		if (plen > 0)
   7264 			SPRINTF(buf, ",play.param=%s", p + 1);
   7265 
   7266 		plen = 0;
   7267 		if (SPECIFIED(ri->encoding))
   7268 			SPRINTF(p, "/%s", audio_encoding_name(ri->encoding));
   7269 		if (SPECIFIED(ri->precision))
   7270 			SPRINTF(p, "/%dbit", ri->precision);
   7271 		if (SPECIFIED(ri->channels))
   7272 			SPRINTF(p, "/%dch", ri->channels);
   7273 		if (SPECIFIED(ri->sample_rate))
   7274 			SPRINTF(p, "/%dHz", ri->sample_rate);
   7275 		if (plen > 0)
   7276 			SPRINTF(buf, ",record.param=%s", p + 1);
   7277 
   7278 		if (SPECIFIED(ai->mode))
   7279 			SPRINTF(buf, ",mode=%d", ai->mode);
   7280 		if (SPECIFIED(ai->hiwat))
   7281 			SPRINTF(buf, ",hiwat=%d", ai->hiwat);
   7282 		if (SPECIFIED(ai->lowat))
   7283 			SPRINTF(buf, ",lowat=%d", ai->lowat);
   7284 		if (SPECIFIED(ai->play.gain))
   7285 			SPRINTF(buf, ",play.gain=%d", ai->play.gain);
   7286 		if (SPECIFIED(ai->record.gain))
   7287 			SPRINTF(buf, ",record.gain=%d", ai->record.gain);
   7288 		if (SPECIFIED_CH(ai->play.balance))
   7289 			SPRINTF(buf, ",play.balance=%d", ai->play.balance);
   7290 		if (SPECIFIED_CH(ai->record.balance))
   7291 			SPRINTF(buf, ",record.balance=%d", ai->record.balance);
   7292 		if (SPECIFIED(ai->play.port))
   7293 			SPRINTF(buf, ",play.port=%d", ai->play.port);
   7294 		if (SPECIFIED(ai->record.port))
   7295 			SPRINTF(buf, ",record.port=%d", ai->record.port);
   7296 		if (SPECIFIED(ai->monitor_gain))
   7297 			SPRINTF(buf, ",monitor_gain=%d", ai->monitor_gain);
   7298 		if (SPECIFIED_CH(ai->play.pause))
   7299 			SPRINTF(buf, ",play.pause=%d", ai->play.pause);
   7300 		if (SPECIFIED_CH(ai->record.pause))
   7301 			SPRINTF(buf, ",record.pause=%d", ai->record.pause);
   7302 
   7303 		if (buflen > 0)
   7304 			TRACE(2, "specified %s", buf + 1);
   7305 	}
   7306 #endif
   7307 
   7308 	AUDIO_INITINFO(&saved_ai);
   7309 	/* XXX shut up gcc */
   7310 	memset(&saved_pfmt, 0, sizeof(saved_pfmt));
   7311 	memset(&saved_rfmt, 0, sizeof(saved_rfmt));
   7312 
   7313 	/*
   7314 	 * Set default value and save current parameters.
   7315 	 * For backward compatibility, use sticky parameters for nonexistent
   7316 	 * track.
   7317 	 */
   7318 	if (ptrack) {
   7319 		pfmt = ptrack->usrbuf.fmt;
   7320 		saved_pfmt = ptrack->usrbuf.fmt;
   7321 		saved_ai.play.pause = ptrack->is_pause;
   7322 	} else {
   7323 		pfmt = sc->sc_sound_pparams;
   7324 	}
   7325 	if (rtrack) {
   7326 		rfmt = rtrack->usrbuf.fmt;
   7327 		saved_rfmt = rtrack->usrbuf.fmt;
   7328 		saved_ai.record.pause = rtrack->is_pause;
   7329 	} else {
   7330 		rfmt = sc->sc_sound_rparams;
   7331 	}
   7332 	saved_ai.mode = file->mode;
   7333 
   7334 	/*
   7335 	 * Overwrite if specified.
   7336 	 */
   7337 	mode = file->mode;
   7338 	if (SPECIFIED(ai->mode)) {
   7339 		/*
   7340 		 * Setting ai->mode no longer does anything because it's
   7341 		 * prohibited to change playback/recording mode after open
   7342 		 * and AUMODE_PLAY_ALL is obsoleted.  However, it still
   7343 		 * keeps the state of AUMODE_PLAY_ALL itself for backward
   7344 		 * compatibility.
   7345 		 * In the internal, only file->mode has the state of
   7346 		 * AUMODE_PLAY_ALL flag and track->mode in both track does
   7347 		 * not have.
   7348 		 */
   7349 		if ((file->mode & AUMODE_PLAY)) {
   7350 			mode = (file->mode & (AUMODE_PLAY | AUMODE_RECORD))
   7351 			    | (ai->mode & AUMODE_PLAY_ALL);
   7352 		}
   7353 	}
   7354 
   7355 	pchanges = audio_track_setinfo_check(ptrack, &pfmt, pi);
   7356 	if (pchanges == -1) {
   7357 #if defined(AUDIO_DEBUG)
   7358 		TRACEF(1, file, "check play.params failed: "
   7359 		    "%s %ubit %uch %uHz",
   7360 		    audio_encoding_name(pi->encoding),
   7361 		    pi->precision,
   7362 		    pi->channels,
   7363 		    pi->sample_rate);
   7364 #endif
   7365 		return EINVAL;
   7366 	}
   7367 
   7368 	rchanges = audio_track_setinfo_check(rtrack, &rfmt, ri);
   7369 	if (rchanges == -1) {
   7370 #if defined(AUDIO_DEBUG)
   7371 		TRACEF(1, file, "check record.params failed: "
   7372 		    "%s %ubit %uch %uHz",
   7373 		    audio_encoding_name(ri->encoding),
   7374 		    ri->precision,
   7375 		    ri->channels,
   7376 		    ri->sample_rate);
   7377 #endif
   7378 		return EINVAL;
   7379 	}
   7380 
   7381 	if (SPECIFIED(ai->mode)) {
   7382 		pchanges = 1;
   7383 		rchanges = 1;
   7384 	}
   7385 
   7386 	/*
   7387 	 * Even when setting either one of playback and recording,
   7388 	 * both track must be halted.
   7389 	 */
   7390 	if (pchanges || rchanges) {
   7391 		audio_file_clear(sc, file);
   7392 #if defined(AUDIO_DEBUG)
   7393 		char nbuf[16];
   7394 		char fmtbuf[64];
   7395 		if (pchanges) {
   7396 			if (ptrack) {
   7397 				snprintf(nbuf, sizeof(nbuf), "%d", ptrack->id);
   7398 			} else {
   7399 				snprintf(nbuf, sizeof(nbuf), "-");
   7400 			}
   7401 			audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &pfmt);
   7402 			DPRINTF(1, "audio track#%s play mode: %s\n",
   7403 			    nbuf, fmtbuf);
   7404 		}
   7405 		if (rchanges) {
   7406 			if (rtrack) {
   7407 				snprintf(nbuf, sizeof(nbuf), "%d", rtrack->id);
   7408 			} else {
   7409 				snprintf(nbuf, sizeof(nbuf), "-");
   7410 			}
   7411 			audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &rfmt);
   7412 			DPRINTF(1, "audio track#%s rec  mode: %s\n",
   7413 			    nbuf, fmtbuf);
   7414 		}
   7415 #endif
   7416 	}
   7417 
   7418 	/* Set mixer parameters */
   7419 	mutex_enter(sc->sc_lock);
   7420 	error = audio_hw_setinfo(sc, ai, &saved_ai);
   7421 	mutex_exit(sc->sc_lock);
   7422 	if (error)
   7423 		goto abort1;
   7424 
   7425 	/*
   7426 	 * Set to track and update sticky parameters.
   7427 	 */
   7428 	error = 0;
   7429 	file->mode = mode;
   7430 
   7431 	if (SPECIFIED_CH(pi->pause)) {
   7432 		if (ptrack)
   7433 			ptrack->is_pause = pi->pause;
   7434 		sc->sc_sound_ppause = pi->pause;
   7435 	}
   7436 	if (pchanges) {
   7437 		if (ptrack) {
   7438 			audio_track_lock_enter(ptrack);
   7439 			error = audio_track_set_format(ptrack, &pfmt);
   7440 			audio_track_lock_exit(ptrack);
   7441 			if (error) {
   7442 				TRACET(1, ptrack, "set play.params failed");
   7443 				goto abort2;
   7444 			}
   7445 		}
   7446 		sc->sc_sound_pparams = pfmt;
   7447 	}
   7448 	/* Change water marks after initializing the buffers. */
   7449 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   7450 		if (ptrack)
   7451 			audio_track_setinfo_water(ptrack, ai);
   7452 	}
   7453 
   7454 	if (SPECIFIED_CH(ri->pause)) {
   7455 		if (rtrack)
   7456 			rtrack->is_pause = ri->pause;
   7457 		sc->sc_sound_rpause = ri->pause;
   7458 	}
   7459 	if (rchanges) {
   7460 		if (rtrack) {
   7461 			audio_track_lock_enter(rtrack);
   7462 			error = audio_track_set_format(rtrack, &rfmt);
   7463 			audio_track_lock_exit(rtrack);
   7464 			if (error) {
   7465 				TRACET(1, rtrack, "set record.params failed");
   7466 				goto abort3;
   7467 			}
   7468 		}
   7469 		sc->sc_sound_rparams = rfmt;
   7470 	}
   7471 
   7472 	return 0;
   7473 
   7474 	/* Rollback */
   7475 abort3:
   7476 	if (error != ENOMEM) {
   7477 		rtrack->is_pause = saved_ai.record.pause;
   7478 		audio_track_lock_enter(rtrack);
   7479 		audio_track_set_format(rtrack, &saved_rfmt);
   7480 		audio_track_lock_exit(rtrack);
   7481 	}
   7482 	sc->sc_sound_rpause = saved_ai.record.pause;
   7483 	sc->sc_sound_rparams = saved_rfmt;
   7484 abort2:
   7485 	if (ptrack && error != ENOMEM) {
   7486 		ptrack->is_pause = saved_ai.play.pause;
   7487 		audio_track_lock_enter(ptrack);
   7488 		audio_track_set_format(ptrack, &saved_pfmt);
   7489 		audio_track_lock_exit(ptrack);
   7490 	}
   7491 	sc->sc_sound_ppause = saved_ai.play.pause;
   7492 	sc->sc_sound_pparams = saved_pfmt;
   7493 	file->mode = saved_ai.mode;
   7494 abort1:
   7495 	mutex_enter(sc->sc_lock);
   7496 	audio_hw_setinfo(sc, &saved_ai, NULL);
   7497 	mutex_exit(sc->sc_lock);
   7498 
   7499 	return error;
   7500 }
   7501 
   7502 /*
   7503  * Write SPECIFIED() parameters within info back to fmt.
   7504  * Note that track can be NULL here.
   7505  * Return value of 1 indicates that fmt is modified.
   7506  * Return value of 0 indicates that fmt is not modified.
   7507  * Return value of -1 indicates that error EINVAL has occurred.
   7508  */
   7509 static int
   7510 audio_track_setinfo_check(audio_track_t *track,
   7511 	audio_format2_t *fmt, const struct audio_prinfo *info)
   7512 {
   7513 	const audio_format2_t *hwfmt;
   7514 	int changes;
   7515 
   7516 	changes = 0;
   7517 	if (SPECIFIED(info->sample_rate)) {
   7518 		if (info->sample_rate < AUDIO_MIN_FREQUENCY)
   7519 			return -1;
   7520 		if (info->sample_rate > AUDIO_MAX_FREQUENCY)
   7521 			return -1;
   7522 		fmt->sample_rate = info->sample_rate;
   7523 		changes = 1;
   7524 	}
   7525 	if (SPECIFIED(info->encoding)) {
   7526 		fmt->encoding = info->encoding;
   7527 		changes = 1;
   7528 	}
   7529 	if (SPECIFIED(info->precision)) {
   7530 		fmt->precision = info->precision;
   7531 		/* we don't have API to specify stride */
   7532 		fmt->stride = info->precision;
   7533 		changes = 1;
   7534 	}
   7535 	if (SPECIFIED(info->channels)) {
   7536 		/*
   7537 		 * We can convert between monaural and stereo each other.
   7538 		 * We can reduce than the number of channels that the hardware
   7539 		 * supports.
   7540 		 */
   7541 		if (info->channels > 2) {
   7542 			if (track) {
   7543 				hwfmt = &track->mixer->hwbuf.fmt;
   7544 				if (info->channels > hwfmt->channels)
   7545 					return -1;
   7546 			} else {
   7547 				/*
   7548 				 * This should never happen.
   7549 				 * If track == NULL, channels should be <= 2.
   7550 				 */
   7551 				return -1;
   7552 			}
   7553 		}
   7554 		fmt->channels = info->channels;
   7555 		changes = 1;
   7556 	}
   7557 
   7558 	if (changes) {
   7559 		if (audio_check_params(fmt) != 0)
   7560 			return -1;
   7561 	}
   7562 
   7563 	return changes;
   7564 }
   7565 
   7566 /*
   7567  * Change water marks for playback track if specified.
   7568  */
   7569 static void
   7570 audio_track_setinfo_water(audio_track_t *track, const struct audio_info *ai)
   7571 {
   7572 	u_int blks;
   7573 	u_int maxblks;
   7574 	u_int blksize;
   7575 
   7576 	KASSERT(audio_track_is_playback(track));
   7577 
   7578 	blksize = track->usrbuf_blksize;
   7579 	maxblks = track->usrbuf.capacity / blksize;
   7580 
   7581 	if (SPECIFIED(ai->hiwat)) {
   7582 		blks = ai->hiwat;
   7583 		if (blks > maxblks)
   7584 			blks = maxblks;
   7585 		if (blks < 2)
   7586 			blks = 2;
   7587 		track->usrbuf_usedhigh = blks * blksize;
   7588 	}
   7589 	if (SPECIFIED(ai->lowat)) {
   7590 		blks = ai->lowat;
   7591 		if (blks > maxblks - 1)
   7592 			blks = maxblks - 1;
   7593 		track->usrbuf_usedlow = blks * blksize;
   7594 	}
   7595 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   7596 		if (track->usrbuf_usedlow > track->usrbuf_usedhigh - blksize) {
   7597 			track->usrbuf_usedlow = track->usrbuf_usedhigh -
   7598 			    blksize;
   7599 		}
   7600 	}
   7601 }
   7602 
   7603 /*
   7604  * Set hardware part of *newai.
   7605  * The parameters handled here are *.port, *.gain, *.balance and monitor_gain.
   7606  * If oldai is specified, previous parameters are stored.
   7607  * This function itself does not roll back if error occurred.
   7608  * Must be called with sc_lock && sc_exlock held.
   7609  */
   7610 static int
   7611 audio_hw_setinfo(struct audio_softc *sc, const struct audio_info *newai,
   7612 	struct audio_info *oldai)
   7613 {
   7614 	const struct audio_prinfo *newpi;
   7615 	const struct audio_prinfo *newri;
   7616 	struct audio_prinfo *oldpi;
   7617 	struct audio_prinfo *oldri;
   7618 	u_int pgain;
   7619 	u_int rgain;
   7620 	u_char pbalance;
   7621 	u_char rbalance;
   7622 	int error;
   7623 
   7624 	KASSERT(mutex_owned(sc->sc_lock));
   7625 	KASSERT(sc->sc_exlock);
   7626 
   7627 	/* XXX shut up gcc */
   7628 	oldpi = NULL;
   7629 	oldri = NULL;
   7630 
   7631 	newpi = &newai->play;
   7632 	newri = &newai->record;
   7633 	if (oldai) {
   7634 		oldpi = &oldai->play;
   7635 		oldri = &oldai->record;
   7636 	}
   7637 	error = 0;
   7638 
   7639 	/*
   7640 	 * It looks like unnecessary to halt HW mixers to set HW mixers.
   7641 	 * mixer_ioctl(MIXER_WRITE) also doesn't halt.
   7642 	 */
   7643 
   7644 	if (SPECIFIED(newpi->port)) {
   7645 		if (oldai)
   7646 			oldpi->port = au_get_port(sc, &sc->sc_outports);
   7647 		error = au_set_port(sc, &sc->sc_outports, newpi->port);
   7648 		if (error) {
   7649 			audio_printf(sc,
   7650 			    "setting play.port=%d failed: errno=%d\n",
   7651 			    newpi->port, error);
   7652 			goto abort;
   7653 		}
   7654 	}
   7655 	if (SPECIFIED(newri->port)) {
   7656 		if (oldai)
   7657 			oldri->port = au_get_port(sc, &sc->sc_inports);
   7658 		error = au_set_port(sc, &sc->sc_inports, newri->port);
   7659 		if (error) {
   7660 			audio_printf(sc,
   7661 			    "setting record.port=%d failed: errno=%d\n",
   7662 			    newri->port, error);
   7663 			goto abort;
   7664 		}
   7665 	}
   7666 
   7667 	/* play.{gain,balance} */
   7668 	if (SPECIFIED(newpi->gain) || SPECIFIED_CH(newpi->balance)) {
   7669 		au_get_gain(sc, &sc->sc_outports, &pgain, &pbalance);
   7670 		if (oldai) {
   7671 			oldpi->gain = pgain;
   7672 			oldpi->balance = pbalance;
   7673 		}
   7674 
   7675 		if (SPECIFIED(newpi->gain))
   7676 			pgain = newpi->gain;
   7677 		if (SPECIFIED_CH(newpi->balance))
   7678 			pbalance = newpi->balance;
   7679 		error = au_set_gain(sc, &sc->sc_outports, pgain, pbalance);
   7680 		if (error) {
   7681 			audio_printf(sc,
   7682 			    "setting play.gain=%d/balance=%d failed: "
   7683 			    "errno=%d\n",
   7684 			    pgain, pbalance, error);
   7685 			goto abort;
   7686 		}
   7687 	}
   7688 
   7689 	/* record.{gain,balance} */
   7690 	if (SPECIFIED(newri->gain) || SPECIFIED_CH(newri->balance)) {
   7691 		au_get_gain(sc, &sc->sc_inports, &rgain, &rbalance);
   7692 		if (oldai) {
   7693 			oldri->gain = rgain;
   7694 			oldri->balance = rbalance;
   7695 		}
   7696 
   7697 		if (SPECIFIED(newri->gain))
   7698 			rgain = newri->gain;
   7699 		if (SPECIFIED_CH(newri->balance))
   7700 			rbalance = newri->balance;
   7701 		error = au_set_gain(sc, &sc->sc_inports, rgain, rbalance);
   7702 		if (error) {
   7703 			audio_printf(sc,
   7704 			    "setting record.gain=%d/balance=%d failed: "
   7705 			    "errno=%d\n",
   7706 			    rgain, rbalance, error);
   7707 			goto abort;
   7708 		}
   7709 	}
   7710 
   7711 	if (SPECIFIED(newai->monitor_gain) && sc->sc_monitor_port != -1) {
   7712 		if (oldai)
   7713 			oldai->monitor_gain = au_get_monitor_gain(sc);
   7714 		error = au_set_monitor_gain(sc, newai->monitor_gain);
   7715 		if (error) {
   7716 			audio_printf(sc,
   7717 			    "setting monitor_gain=%d failed: errno=%d\n",
   7718 			    newai->monitor_gain, error);
   7719 			goto abort;
   7720 		}
   7721 	}
   7722 
   7723 	/* XXX TODO */
   7724 	/* sc->sc_ai = *ai; */
   7725 
   7726 	error = 0;
   7727 abort:
   7728 	return error;
   7729 }
   7730 
   7731 /*
   7732  * Setup the hardware with mixer format phwfmt, rhwfmt.
   7733  * The arguments have following restrictions:
   7734  * - setmode is the direction you want to set, AUMODE_PLAY or AUMODE_RECORD,
   7735  *   or both.
   7736  * - phwfmt and rhwfmt must not be NULL regardless of setmode.
   7737  * - On non-independent devices, phwfmt and rhwfmt must have the same
   7738  *   parameters.
   7739  * - pfil and rfil must be zero-filled.
   7740  * If successful,
   7741  * - pfil, rfil will be filled with filter information specified by the
   7742  *   hardware driver if necessary.
   7743  * and then returns 0.  Otherwise returns errno.
   7744  * Must be called without sc_lock held.
   7745  */
   7746 static int
   7747 audio_hw_set_format(struct audio_softc *sc, int setmode,
   7748 	const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
   7749 	audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
   7750 {
   7751 	audio_params_t pp, rp;
   7752 	int error;
   7753 
   7754 	KASSERT(phwfmt != NULL);
   7755 	KASSERT(rhwfmt != NULL);
   7756 
   7757 	pp = format2_to_params(phwfmt);
   7758 	rp = format2_to_params(rhwfmt);
   7759 
   7760 	mutex_enter(sc->sc_lock);
   7761 	error = sc->hw_if->set_format(sc->hw_hdl, setmode,
   7762 	    &pp, &rp, pfil, rfil);
   7763 	if (error) {
   7764 		mutex_exit(sc->sc_lock);
   7765 		audio_printf(sc, "set_format failed: errno=%d\n", error);
   7766 		return error;
   7767 	}
   7768 
   7769 	if (sc->hw_if->commit_settings) {
   7770 		error = sc->hw_if->commit_settings(sc->hw_hdl);
   7771 		if (error) {
   7772 			mutex_exit(sc->sc_lock);
   7773 			audio_printf(sc,
   7774 			    "commit_settings failed: errno=%d\n", error);
   7775 			return error;
   7776 		}
   7777 	}
   7778 	mutex_exit(sc->sc_lock);
   7779 
   7780 	return 0;
   7781 }
   7782 
   7783 /*
   7784  * Fill audio_info structure.  If need_mixerinfo is true, it will also
   7785  * fill the hardware mixer information.
   7786  * Must be called with sc_exlock held and without sc_lock held.
   7787  */
   7788 static int
   7789 audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int need_mixerinfo,
   7790 	audio_file_t *file)
   7791 {
   7792 	struct audio_prinfo *ri, *pi;
   7793 	audio_track_t *track;
   7794 	audio_track_t *ptrack;
   7795 	audio_track_t *rtrack;
   7796 	int gain;
   7797 
   7798 	KASSERT(sc->sc_exlock);
   7799 
   7800 	ri = &ai->record;
   7801 	pi = &ai->play;
   7802 	ptrack = file->ptrack;
   7803 	rtrack = file->rtrack;
   7804 
   7805 	memset(ai, 0, sizeof(*ai));
   7806 
   7807 	if (ptrack) {
   7808 		pi->sample_rate = ptrack->usrbuf.fmt.sample_rate;
   7809 		pi->channels    = ptrack->usrbuf.fmt.channels;
   7810 		pi->precision   = ptrack->usrbuf.fmt.precision;
   7811 		pi->encoding    = ptrack->usrbuf.fmt.encoding;
   7812 		pi->pause       = ptrack->is_pause;
   7813 	} else {
   7814 		/* Use sticky parameters if the track is not available. */
   7815 		pi->sample_rate = sc->sc_sound_pparams.sample_rate;
   7816 		pi->channels    = sc->sc_sound_pparams.channels;
   7817 		pi->precision   = sc->sc_sound_pparams.precision;
   7818 		pi->encoding    = sc->sc_sound_pparams.encoding;
   7819 		pi->pause       = sc->sc_sound_ppause;
   7820 	}
   7821 	if (rtrack) {
   7822 		ri->sample_rate = rtrack->usrbuf.fmt.sample_rate;
   7823 		ri->channels    = rtrack->usrbuf.fmt.channels;
   7824 		ri->precision   = rtrack->usrbuf.fmt.precision;
   7825 		ri->encoding    = rtrack->usrbuf.fmt.encoding;
   7826 		ri->pause       = rtrack->is_pause;
   7827 	} else {
   7828 		/* Use sticky parameters if the track is not available. */
   7829 		ri->sample_rate = sc->sc_sound_rparams.sample_rate;
   7830 		ri->channels    = sc->sc_sound_rparams.channels;
   7831 		ri->precision   = sc->sc_sound_rparams.precision;
   7832 		ri->encoding    = sc->sc_sound_rparams.encoding;
   7833 		ri->pause       = sc->sc_sound_rpause;
   7834 	}
   7835 
   7836 	if (ptrack) {
   7837 		pi->seek = ptrack->usrbuf.used;
   7838 		pi->samples = ptrack->stamp * ptrack->usrbuf_blksize;
   7839 		pi->eof = ptrack->eofcounter;
   7840 		pi->error = (ptrack->dropframes != 0) ? 1 : 0;
   7841 		pi->open = 1;
   7842 		pi->buffer_size = ptrack->usrbuf.capacity;
   7843 	}
   7844 	pi->waiting = 0;		/* open never hangs */
   7845 	pi->active = sc->sc_pbusy;
   7846 
   7847 	if (rtrack) {
   7848 		ri->seek = audio_track_readablebytes(rtrack);
   7849 		ri->samples = rtrack->stamp * rtrack->usrbuf_blksize;
   7850 		ri->eof = 0;
   7851 		ri->error = (rtrack->dropframes != 0) ? 1 : 0;
   7852 		ri->open = 1;
   7853 		ri->buffer_size = audio_track_inputblk_as_usrbyte(rtrack,
   7854 		    rtrack->input->capacity);
   7855 	}
   7856 	ri->waiting = 0;		/* open never hangs */
   7857 	ri->active = sc->sc_rbusy;
   7858 
   7859 	/*
   7860 	 * XXX There may be different number of channels between playback
   7861 	 *     and recording, so that blocksize also may be different.
   7862 	 *     But struct audio_info has an united blocksize...
   7863 	 *     Here, I use play info precedencely if ptrack is available,
   7864 	 *     otherwise record info.
   7865 	 *
   7866 	 * XXX hiwat/lowat is a playback-only parameter.  What should I
   7867 	 *     return for a record-only descriptor?
   7868 	 */
   7869 	track = ptrack ? ptrack : rtrack;
   7870 	if (track) {
   7871 		ai->blocksize = track->usrbuf_blksize;
   7872 		ai->hiwat = track->usrbuf_usedhigh / track->usrbuf_blksize;
   7873 		ai->lowat = track->usrbuf_usedlow / track->usrbuf_blksize;
   7874 	}
   7875 	ai->mode = file->mode;
   7876 
   7877 	/*
   7878 	 * For backward compatibility, we have to pad these five fields
   7879 	 * a fake non-zero value even if there are no tracks.
   7880 	 */
   7881 	if (ptrack == NULL)
   7882 		pi->buffer_size = 65536;
   7883 	if (rtrack == NULL)
   7884 		ri->buffer_size = 65536;
   7885 	if (ptrack == NULL && rtrack == NULL) {
   7886 		ai->blocksize = 2048;
   7887 		ai->hiwat = ai->play.buffer_size / ai->blocksize;
   7888 		ai->lowat = ai->hiwat * 3 / 4;
   7889 	}
   7890 
   7891 	if (need_mixerinfo) {
   7892 		mutex_enter(sc->sc_lock);
   7893 
   7894 		pi->port = au_get_port(sc, &sc->sc_outports);
   7895 		ri->port = au_get_port(sc, &sc->sc_inports);
   7896 
   7897 		pi->avail_ports = sc->sc_outports.allports;
   7898 		ri->avail_ports = sc->sc_inports.allports;
   7899 
   7900 		au_get_gain(sc, &sc->sc_outports, &pi->gain, &pi->balance);
   7901 		au_get_gain(sc, &sc->sc_inports, &ri->gain, &ri->balance);
   7902 
   7903 		if (sc->sc_monitor_port != -1) {
   7904 			gain = au_get_monitor_gain(sc);
   7905 			if (gain != -1)
   7906 				ai->monitor_gain = gain;
   7907 		}
   7908 		mutex_exit(sc->sc_lock);
   7909 	}
   7910 
   7911 	return 0;
   7912 }
   7913 
   7914 /*
   7915  * Return true if playback is configured.
   7916  * This function can be used after audioattach.
   7917  */
   7918 static bool
   7919 audio_can_playback(struct audio_softc *sc)
   7920 {
   7921 
   7922 	return (sc->sc_pmixer != NULL);
   7923 }
   7924 
   7925 /*
   7926  * Return true if recording is configured.
   7927  * This function can be used after audioattach.
   7928  */
   7929 static bool
   7930 audio_can_capture(struct audio_softc *sc)
   7931 {
   7932 
   7933 	return (sc->sc_rmixer != NULL);
   7934 }
   7935 
   7936 /*
   7937  * Get the afp->index'th item from the valid one of format[].
   7938  * If found, stores it to afp->fmt and returns 0.  Otherwise return EINVAL.
   7939  *
   7940  * This is common routines for query_format.
   7941  * If your hardware driver has struct audio_format[], the simplest case
   7942  * you can write your query_format interface as follows:
   7943  *
   7944  * struct audio_format foo_format[] = { ... };
   7945  *
   7946  * int
   7947  * foo_query_format(void *hdl, audio_format_query_t *afp)
   7948  * {
   7949  *   return audio_query_format(foo_format, __arraycount(foo_format), afp);
   7950  * }
   7951  */
   7952 int
   7953 audio_query_format(const struct audio_format *format, int nformats,
   7954 	audio_format_query_t *afp)
   7955 {
   7956 	const struct audio_format *f;
   7957 	int idx;
   7958 	int i;
   7959 
   7960 	idx = 0;
   7961 	for (i = 0; i < nformats; i++) {
   7962 		f = &format[i];
   7963 		if (!AUFMT_IS_VALID(f))
   7964 			continue;
   7965 		if (afp->index == idx) {
   7966 			afp->fmt = *f;
   7967 			return 0;
   7968 		}
   7969 		idx++;
   7970 	}
   7971 	return EINVAL;
   7972 }
   7973 
   7974 /*
   7975  * This function is provided for the hardware driver's set_format() to
   7976  * find index matches with 'param' from array of audio_format_t 'formats'.
   7977  * 'mode' is either of AUMODE_PLAY or AUMODE_RECORD.
   7978  * It returns the matched index and never fails.  Because param passed to
   7979  * set_format() is selected from query_format().
   7980  * This function will be an alternative to auconv_set_converter() to
   7981  * find index.
   7982  */
   7983 int
   7984 audio_indexof_format(const struct audio_format *formats, int nformats,
   7985 	int mode, const audio_params_t *param)
   7986 {
   7987 	const struct audio_format *f;
   7988 	int index;
   7989 	int j;
   7990 
   7991 	for (index = 0; index < nformats; index++) {
   7992 		f = &formats[index];
   7993 
   7994 		if (!AUFMT_IS_VALID(f))
   7995 			continue;
   7996 		if ((f->mode & mode) == 0)
   7997 			continue;
   7998 		if (f->encoding != param->encoding)
   7999 			continue;
   8000 		if (f->validbits != param->precision)
   8001 			continue;
   8002 		if (f->channels != param->channels)
   8003 			continue;
   8004 
   8005 		if (f->frequency_type == 0) {
   8006 			if (param->sample_rate < f->frequency[0] ||
   8007 			    param->sample_rate > f->frequency[1])
   8008 				continue;
   8009 		} else {
   8010 			for (j = 0; j < f->frequency_type; j++) {
   8011 				if (param->sample_rate == f->frequency[j])
   8012 					break;
   8013 			}
   8014 			if (j == f->frequency_type)
   8015 				continue;
   8016 		}
   8017 
   8018 		/* Then, matched */
   8019 		return index;
   8020 	}
   8021 
   8022 	/* Not matched.  This should not be happened. */
   8023 	panic("%s: cannot find matched format\n", __func__);
   8024 }
   8025 
   8026 /*
   8027  * Get or set hardware blocksize in msec.
   8028  * XXX It's for debug.
   8029  */
   8030 static int
   8031 audio_sysctl_blk_ms(SYSCTLFN_ARGS)
   8032 {
   8033 	struct sysctlnode node;
   8034 	struct audio_softc *sc;
   8035 	audio_format2_t phwfmt;
   8036 	audio_format2_t rhwfmt;
   8037 	audio_filter_reg_t pfil;
   8038 	audio_filter_reg_t rfil;
   8039 	int t;
   8040 	int old_blk_ms;
   8041 	int mode;
   8042 	int error;
   8043 
   8044 	node = *rnode;
   8045 	sc = node.sysctl_data;
   8046 
   8047 	error = audio_exlock_enter(sc);
   8048 	if (error)
   8049 		return error;
   8050 
   8051 	old_blk_ms = sc->sc_blk_ms;
   8052 	t = old_blk_ms;
   8053 	node.sysctl_data = &t;
   8054 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   8055 	if (error || newp == NULL)
   8056 		goto abort;
   8057 
   8058 	if (t < 0) {
   8059 		error = EINVAL;
   8060 		goto abort;
   8061 	}
   8062 
   8063 	if (sc->sc_popens + sc->sc_ropens > 0) {
   8064 		error = EBUSY;
   8065 		goto abort;
   8066 	}
   8067 	sc->sc_blk_ms = t;
   8068 	mode = 0;
   8069 	if (sc->sc_pmixer) {
   8070 		mode |= AUMODE_PLAY;
   8071 		phwfmt = sc->sc_pmixer->hwbuf.fmt;
   8072 	}
   8073 	if (sc->sc_rmixer) {
   8074 		mode |= AUMODE_RECORD;
   8075 		rhwfmt = sc->sc_rmixer->hwbuf.fmt;
   8076 	}
   8077 
   8078 	/* re-init hardware */
   8079 	memset(&pfil, 0, sizeof(pfil));
   8080 	memset(&rfil, 0, sizeof(rfil));
   8081 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   8082 	if (error) {
   8083 		goto abort;
   8084 	}
   8085 
   8086 	/* re-init track mixer */
   8087 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   8088 	if (error) {
   8089 		/* Rollback */
   8090 		sc->sc_blk_ms = old_blk_ms;
   8091 		audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   8092 		goto abort;
   8093 	}
   8094 	error = 0;
   8095 abort:
   8096 	audio_exlock_exit(sc);
   8097 	return error;
   8098 }
   8099 
   8100 /*
   8101  * Get or set multiuser mode.
   8102  */
   8103 static int
   8104 audio_sysctl_multiuser(SYSCTLFN_ARGS)
   8105 {
   8106 	struct sysctlnode node;
   8107 	struct audio_softc *sc;
   8108 	bool t;
   8109 	int error;
   8110 
   8111 	node = *rnode;
   8112 	sc = node.sysctl_data;
   8113 
   8114 	error = audio_exlock_enter(sc);
   8115 	if (error)
   8116 		return error;
   8117 
   8118 	t = sc->sc_multiuser;
   8119 	node.sysctl_data = &t;
   8120 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   8121 	if (error || newp == NULL)
   8122 		goto abort;
   8123 
   8124 	sc->sc_multiuser = t;
   8125 	error = 0;
   8126 abort:
   8127 	audio_exlock_exit(sc);
   8128 	return error;
   8129 }
   8130 
   8131 #if defined(AUDIO_DEBUG)
   8132 /*
   8133  * Get or set debug verbose level. (0..4)
   8134  * XXX It's for debug.
   8135  * XXX It is not separated per device.
   8136  */
   8137 static int
   8138 audio_sysctl_debug(SYSCTLFN_ARGS)
   8139 {
   8140 	struct sysctlnode node;
   8141 	int t;
   8142 	int error;
   8143 
   8144 	node = *rnode;
   8145 	t = audiodebug;
   8146 	node.sysctl_data = &t;
   8147 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   8148 	if (error || newp == NULL)
   8149 		return error;
   8150 
   8151 	if (t < 0 || t > 4)
   8152 		return EINVAL;
   8153 	audiodebug = t;
   8154 	printf("audio: audiodebug = %d\n", audiodebug);
   8155 	return 0;
   8156 }
   8157 #endif /* AUDIO_DEBUG */
   8158 
   8159 #ifdef AUDIO_PM_IDLE
   8160 static void
   8161 audio_idle(void *arg)
   8162 {
   8163 	device_t dv = arg;
   8164 	struct audio_softc *sc = device_private(dv);
   8165 
   8166 #ifdef PNP_DEBUG
   8167 	extern int pnp_debug_idle;
   8168 	if (pnp_debug_idle)
   8169 		printf("%s: idle handler called\n", device_xname(dv));
   8170 #endif
   8171 
   8172 	sc->sc_idle = true;
   8173 
   8174 	/* XXX joerg Make pmf_device_suspend handle children? */
   8175 	if (!pmf_device_suspend(dv, PMF_Q_SELF))
   8176 		return;
   8177 
   8178 	if (!pmf_device_suspend(sc->hw_dev, PMF_Q_SELF))
   8179 		pmf_device_resume(dv, PMF_Q_SELF);
   8180 }
   8181 
   8182 static void
   8183 audio_activity(device_t dv, devactive_t type)
   8184 {
   8185 	struct audio_softc *sc = device_private(dv);
   8186 
   8187 	if (type != DVA_SYSTEM)
   8188 		return;
   8189 
   8190 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   8191 
   8192 	sc->sc_idle = false;
   8193 	if (!device_is_active(dv)) {
   8194 		/* XXX joerg How to deal with a failing resume... */
   8195 		pmf_device_resume(sc->hw_dev, PMF_Q_SELF);
   8196 		pmf_device_resume(dv, PMF_Q_SELF);
   8197 	}
   8198 }
   8199 #endif
   8200 
   8201 static bool
   8202 audio_suspend(device_t dv, const pmf_qual_t *qual)
   8203 {
   8204 	struct audio_softc *sc = device_private(dv);
   8205 	int error;
   8206 
   8207 	error = audio_exlock_mutex_enter(sc);
   8208 	if (error)
   8209 		return error;
   8210 	sc->sc_suspending = true;
   8211 	audio_mixer_capture(sc);
   8212 
   8213 	if (sc->sc_pbusy) {
   8214 		audio_pmixer_halt(sc);
   8215 		/* Reuse this as need-to-restart flag while suspending */
   8216 		sc->sc_pbusy = true;
   8217 	}
   8218 	if (sc->sc_rbusy) {
   8219 		audio_rmixer_halt(sc);
   8220 		/* Reuse this as need-to-restart flag while suspending */
   8221 		sc->sc_rbusy = true;
   8222 	}
   8223 
   8224 #ifdef AUDIO_PM_IDLE
   8225 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   8226 #endif
   8227 	audio_exlock_mutex_exit(sc);
   8228 
   8229 	return true;
   8230 }
   8231 
   8232 static bool
   8233 audio_resume(device_t dv, const pmf_qual_t *qual)
   8234 {
   8235 	struct audio_softc *sc = device_private(dv);
   8236 	struct audio_info ai;
   8237 	int error;
   8238 
   8239 	error = audio_exlock_mutex_enter(sc);
   8240 	if (error)
   8241 		return error;
   8242 
   8243 	sc->sc_suspending = false;
   8244 	audio_mixer_restore(sc);
   8245 	/* XXX ? */
   8246 	AUDIO_INITINFO(&ai);
   8247 	audio_hw_setinfo(sc, &ai, NULL);
   8248 
   8249 	/*
   8250 	 * During from suspend to resume here, sc_[pr]busy is used as
   8251 	 * need-to-restart flag temporarily.  After this point,
   8252 	 * sc_[pr]busy is returned to its original usage (busy flag).
   8253 	 * And note that sc_[pr]busy must be false to call [pr]mixer_start().
   8254 	 */
   8255 	if (sc->sc_pbusy) {
   8256 		/* pmixer_start() requires pbusy is false */
   8257 		sc->sc_pbusy = false;
   8258 		audio_pmixer_start(sc, true);
   8259 	}
   8260 	if (sc->sc_rbusy) {
   8261 		/* rmixer_start() requires rbusy is false */
   8262 		sc->sc_rbusy = false;
   8263 		audio_rmixer_start(sc);
   8264 	}
   8265 
   8266 	audio_exlock_mutex_exit(sc);
   8267 
   8268 	return true;
   8269 }
   8270 
   8271 #if defined(AUDIO_DEBUG)
   8272 static void
   8273 audio_format2_tostr(char *buf, size_t bufsize, const audio_format2_t *fmt)
   8274 {
   8275 	int n;
   8276 
   8277 	n = 0;
   8278 	n += snprintf(buf + n, bufsize - n, "%s",
   8279 	    audio_encoding_name(fmt->encoding));
   8280 	if (fmt->precision == fmt->stride) {
   8281 		n += snprintf(buf + n, bufsize - n, " %dbit", fmt->precision);
   8282 	} else {
   8283 		n += snprintf(buf + n, bufsize - n, " %d/%dbit",
   8284 			fmt->precision, fmt->stride);
   8285 	}
   8286 
   8287 	snprintf(buf + n, bufsize - n, " %uch %uHz",
   8288 	    fmt->channels, fmt->sample_rate);
   8289 }
   8290 #endif
   8291 
   8292 #if defined(AUDIO_DEBUG)
   8293 static void
   8294 audio_print_format2(const char *s, const audio_format2_t *fmt)
   8295 {
   8296 	char fmtstr[64];
   8297 
   8298 	audio_format2_tostr(fmtstr, sizeof(fmtstr), fmt);
   8299 	printf("%s %s\n", s, fmtstr);
   8300 }
   8301 #endif
   8302 
   8303 #ifdef DIAGNOSTIC
   8304 void
   8305 audio_diagnostic_format2(const char *where, const audio_format2_t *fmt)
   8306 {
   8307 
   8308 	KASSERTMSG(fmt, "called from %s", where);
   8309 
   8310 	/* XXX MSM6258 vs(4) only has 4bit stride format. */
   8311 	if (fmt->encoding == AUDIO_ENCODING_ADPCM) {
   8312 		KASSERTMSG(fmt->stride == 4 || fmt->stride == 8,
   8313 		    "called from %s: fmt->stride=%d", where, fmt->stride);
   8314 	} else {
   8315 		KASSERTMSG(fmt->stride % NBBY == 0,
   8316 		    "called from %s: fmt->stride=%d", where, fmt->stride);
   8317 	}
   8318 	KASSERTMSG(fmt->precision <= fmt->stride,
   8319 	    "called from %s: fmt->precision=%d fmt->stride=%d",
   8320 	    where, fmt->precision, fmt->stride);
   8321 	KASSERTMSG(1 <= fmt->channels && fmt->channels <= AUDIO_MAX_CHANNELS,
   8322 	    "called from %s: fmt->channels=%d", where, fmt->channels);
   8323 
   8324 	/* XXX No check for encodings? */
   8325 }
   8326 
   8327 void
   8328 audio_diagnostic_filter_arg(const char *where, const audio_filter_arg_t *arg)
   8329 {
   8330 
   8331 	KASSERT(arg != NULL);
   8332 	KASSERT(arg->src != NULL);
   8333 	KASSERT(arg->dst != NULL);
   8334 	audio_diagnostic_format2(where, arg->srcfmt);
   8335 	audio_diagnostic_format2(where, arg->dstfmt);
   8336 	KASSERT(arg->count > 0);
   8337 }
   8338 
   8339 void
   8340 audio_diagnostic_ring(const char *where, const audio_ring_t *ring)
   8341 {
   8342 
   8343 	KASSERTMSG(ring, "called from %s", where);
   8344 	audio_diagnostic_format2(where, &ring->fmt);
   8345 	KASSERTMSG(0 <= ring->capacity && ring->capacity < INT_MAX / 2,
   8346 	    "called from %s: ring->capacity=%d", where, ring->capacity);
   8347 	KASSERTMSG(0 <= ring->used && ring->used <= ring->capacity,
   8348 	    "called from %s: ring->used=%d ring->capacity=%d",
   8349 	    where, ring->used, ring->capacity);
   8350 	if (ring->capacity == 0) {
   8351 		KASSERTMSG(ring->mem == NULL,
   8352 		    "called from %s: capacity == 0 but mem != NULL", where);
   8353 	} else {
   8354 		KASSERTMSG(ring->mem != NULL,
   8355 		    "called from %s: capacity != 0 but mem == NULL", where);
   8356 		KASSERTMSG(0 <= ring->head && ring->head < ring->capacity,
   8357 		    "called from %s: ring->head=%d ring->capacity=%d",
   8358 		    where, ring->head, ring->capacity);
   8359 	}
   8360 }
   8361 #endif /* DIAGNOSTIC */
   8362 
   8363 
   8364 /*
   8365  * Mixer driver
   8366  */
   8367 
   8368 /*
   8369  * Must be called without sc_lock held.
   8370  */
   8371 int
   8372 mixer_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   8373 	struct lwp *l)
   8374 {
   8375 	struct file *fp;
   8376 	audio_file_t *af;
   8377 	int error, fd;
   8378 
   8379 	TRACE(1, "flags=0x%x", flags);
   8380 
   8381 	error = fd_allocfile(&fp, &fd);
   8382 	if (error)
   8383 		return error;
   8384 
   8385 	af = kmem_zalloc(sizeof(*af), KM_SLEEP);
   8386 	af->sc = sc;
   8387 	af->dev = dev;
   8388 
   8389 	mutex_enter(sc->sc_lock);
   8390 	if (sc->sc_dying) {
   8391 		mutex_exit(sc->sc_lock);
   8392 		kmem_free(af, sizeof(*af));
   8393 		fd_abort(curproc, fp, fd);
   8394 		return ENXIO;
   8395 	}
   8396 	mutex_enter(sc->sc_intr_lock);
   8397 	SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
   8398 	mutex_exit(sc->sc_intr_lock);
   8399 	mutex_exit(sc->sc_lock);
   8400 
   8401 	error = fd_clone(fp, fd, flags, &audio_fileops, af);
   8402 	KASSERT(error == EMOVEFD);
   8403 
   8404 	return error;
   8405 }
   8406 
   8407 /*
   8408  * Add a process to those to be signalled on mixer activity.
   8409  * If the process has already been added, do nothing.
   8410  * Must be called with sc_exlock held and without sc_lock held.
   8411  */
   8412 static void
   8413 mixer_async_add(struct audio_softc *sc, pid_t pid)
   8414 {
   8415 	int i;
   8416 
   8417 	KASSERT(sc->sc_exlock);
   8418 
   8419 	/* If already exists, returns without doing anything. */
   8420 	for (i = 0; i < sc->sc_am_used; i++) {
   8421 		if (sc->sc_am[i] == pid)
   8422 			return;
   8423 	}
   8424 
   8425 	/* Extend array if necessary. */
   8426 	if (sc->sc_am_used >= sc->sc_am_capacity) {
   8427 		sc->sc_am_capacity += AM_CAPACITY;
   8428 		sc->sc_am = kern_realloc(sc->sc_am,
   8429 		    sc->sc_am_capacity * sizeof(pid_t), M_WAITOK);
   8430 		TRACE(2, "realloc am_capacity=%d", sc->sc_am_capacity);
   8431 	}
   8432 
   8433 	TRACE(2, "am[%d]=%d", sc->sc_am_used, (int)pid);
   8434 	sc->sc_am[sc->sc_am_used++] = pid;
   8435 }
   8436 
   8437 /*
   8438  * Remove a process from those to be signalled on mixer activity.
   8439  * If the process has not been added, do nothing.
   8440  * Must be called with sc_exlock held and without sc_lock held.
   8441  */
   8442 static void
   8443 mixer_async_remove(struct audio_softc *sc, pid_t pid)
   8444 {
   8445 	int i;
   8446 
   8447 	KASSERT(sc->sc_exlock);
   8448 
   8449 	for (i = 0; i < sc->sc_am_used; i++) {
   8450 		if (sc->sc_am[i] == pid) {
   8451 			sc->sc_am[i] = sc->sc_am[--sc->sc_am_used];
   8452 			TRACE(2, "am[%d](%d) removed, used=%d",
   8453 			    i, (int)pid, sc->sc_am_used);
   8454 
   8455 			/* Empty array if no longer necessary. */
   8456 			if (sc->sc_am_used == 0) {
   8457 				kern_free(sc->sc_am);
   8458 				sc->sc_am = NULL;
   8459 				sc->sc_am_capacity = 0;
   8460 				TRACE(2, "released");
   8461 			}
   8462 			return;
   8463 		}
   8464 	}
   8465 }
   8466 
   8467 /*
   8468  * Signal all processes waiting for the mixer.
   8469  * Must be called with sc_exlock held.
   8470  */
   8471 static void
   8472 mixer_signal(struct audio_softc *sc)
   8473 {
   8474 	proc_t *p;
   8475 	int i;
   8476 
   8477 	KASSERT(sc->sc_exlock);
   8478 
   8479 	for (i = 0; i < sc->sc_am_used; i++) {
   8480 		mutex_enter(&proc_lock);
   8481 		p = proc_find(sc->sc_am[i]);
   8482 		if (p)
   8483 			psignal(p, SIGIO);
   8484 		mutex_exit(&proc_lock);
   8485 	}
   8486 }
   8487 
   8488 /*
   8489  * Close a mixer device
   8490  */
   8491 int
   8492 mixer_close(struct audio_softc *sc, audio_file_t *file)
   8493 {
   8494 	int error;
   8495 
   8496 	error = audio_exlock_enter(sc);
   8497 	if (error)
   8498 		return error;
   8499 	TRACE(1, "called");
   8500 	mixer_async_remove(sc, curproc->p_pid);
   8501 	audio_exlock_exit(sc);
   8502 
   8503 	return 0;
   8504 }
   8505 
   8506 /*
   8507  * Must be called without sc_lock nor sc_exlock held.
   8508  */
   8509 int
   8510 mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
   8511 	struct lwp *l)
   8512 {
   8513 	mixer_devinfo_t *mi;
   8514 	mixer_ctrl_t *mc;
   8515 	int val;
   8516 	int error;
   8517 
   8518 #if defined(AUDIO_DEBUG)
   8519 	char pre[64];
   8520 	snprintf(pre, sizeof(pre), "pid=%d.%d",
   8521 	    (int)curproc->p_pid, (int)l->l_lid);
   8522 #endif
   8523 	error = EINVAL;
   8524 
   8525 	/* we can return cached values if we are sleeping */
   8526 	if (cmd != AUDIO_MIXER_READ) {
   8527 		mutex_enter(sc->sc_lock);
   8528 		device_active(sc->sc_dev, DVA_SYSTEM);
   8529 		mutex_exit(sc->sc_lock);
   8530 	}
   8531 
   8532 	switch (cmd) {
   8533 	case FIOASYNC:
   8534 		val = *(int *)addr;
   8535 		TRACE(2, "%s FIOASYNC %s", pre, val ? "on" : "off");
   8536 		error = audio_exlock_enter(sc);
   8537 		if (error)
   8538 			break;
   8539 		if (val) {
   8540 			mixer_async_add(sc, curproc->p_pid);
   8541 		} else {
   8542 			mixer_async_remove(sc, curproc->p_pid);
   8543 		}
   8544 		audio_exlock_exit(sc);
   8545 		break;
   8546 
   8547 	case AUDIO_GETDEV:
   8548 		TRACE(2, "%s AUDIO_GETDEV", pre);
   8549 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
   8550 		break;
   8551 
   8552 	case AUDIO_MIXER_DEVINFO:
   8553 		TRACE(2, "%s AUDIO_MIXER_DEVINFO", pre);
   8554 		mi = (mixer_devinfo_t *)addr;
   8555 
   8556 		mi->un.v.delta = 0; /* default */
   8557 		mutex_enter(sc->sc_lock);
   8558 		error = audio_query_devinfo(sc, mi);
   8559 		mutex_exit(sc->sc_lock);
   8560 		break;
   8561 
   8562 	case AUDIO_MIXER_READ:
   8563 		TRACE(2, "%s AUDIO_MIXER_READ", pre);
   8564 		mc = (mixer_ctrl_t *)addr;
   8565 
   8566 		error = audio_exlock_mutex_enter(sc);
   8567 		if (error)
   8568 			break;
   8569 		if (device_is_active(sc->hw_dev))
   8570 			error = audio_get_port(sc, mc);
   8571 		else if (mc->dev < 0 || mc->dev >= sc->sc_nmixer_states)
   8572 			error = ENXIO;
   8573 		else {
   8574 			int dev = mc->dev;
   8575 			memcpy(mc, &sc->sc_mixer_state[dev],
   8576 			    sizeof(mixer_ctrl_t));
   8577 			error = 0;
   8578 		}
   8579 		audio_exlock_mutex_exit(sc);
   8580 		break;
   8581 
   8582 	case AUDIO_MIXER_WRITE:
   8583 		TRACE(2, "%s AUDIO_MIXER_WRITE", pre);
   8584 		error = audio_exlock_mutex_enter(sc);
   8585 		if (error)
   8586 			break;
   8587 		error = audio_set_port(sc, (mixer_ctrl_t *)addr);
   8588 		if (error) {
   8589 			audio_exlock_mutex_exit(sc);
   8590 			break;
   8591 		}
   8592 
   8593 		if (sc->hw_if->commit_settings) {
   8594 			error = sc->hw_if->commit_settings(sc->hw_hdl);
   8595 			if (error) {
   8596 				audio_exlock_mutex_exit(sc);
   8597 				break;
   8598 			}
   8599 		}
   8600 		mutex_exit(sc->sc_lock);
   8601 		mixer_signal(sc);
   8602 		audio_exlock_exit(sc);
   8603 		break;
   8604 
   8605 	default:
   8606 		TRACE(2, "(%lu,'%c',%lu)",
   8607 		    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff);
   8608 		if (sc->hw_if->dev_ioctl) {
   8609 			mutex_enter(sc->sc_lock);
   8610 			error = sc->hw_if->dev_ioctl(sc->hw_hdl,
   8611 			    cmd, addr, flag, l);
   8612 			mutex_exit(sc->sc_lock);
   8613 		} else
   8614 			error = EINVAL;
   8615 		break;
   8616 	}
   8617 
   8618 	if (error)
   8619 		TRACE(2, "error=%d", error);
   8620 	return error;
   8621 }
   8622 
   8623 /*
   8624  * Must be called with sc_lock held.
   8625  */
   8626 int
   8627 au_portof(struct audio_softc *sc, char *name, int class)
   8628 {
   8629 	mixer_devinfo_t mi;
   8630 
   8631 	KASSERT(mutex_owned(sc->sc_lock));
   8632 
   8633 	for (mi.index = 0; audio_query_devinfo(sc, &mi) == 0; mi.index++) {
   8634 		if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
   8635 			return mi.index;
   8636 	}
   8637 	return -1;
   8638 }
   8639 
   8640 /*
   8641  * Must be called with sc_lock held.
   8642  */
   8643 void
   8644 au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
   8645 	mixer_devinfo_t *mi, const struct portname *tbl)
   8646 {
   8647 	int i, j;
   8648 
   8649 	KASSERT(mutex_owned(sc->sc_lock));
   8650 
   8651 	ports->index = mi->index;
   8652 	if (mi->type == AUDIO_MIXER_ENUM) {
   8653 		ports->isenum = true;
   8654 		for(i = 0; tbl[i].name; i++)
   8655 		    for(j = 0; j < mi->un.e.num_mem; j++)
   8656 			if (strcmp(mi->un.e.member[j].label.name,
   8657 						    tbl[i].name) == 0) {
   8658 				ports->allports |= tbl[i].mask;
   8659 				ports->aumask[ports->nports] = tbl[i].mask;
   8660 				ports->misel[ports->nports] =
   8661 				    mi->un.e.member[j].ord;
   8662 				ports->miport[ports->nports] =
   8663 				    au_portof(sc, mi->un.e.member[j].label.name,
   8664 				    mi->mixer_class);
   8665 				if (ports->mixerout != -1 &&
   8666 				    ports->miport[ports->nports] != -1)
   8667 					ports->isdual = true;
   8668 				++ports->nports;
   8669 			}
   8670 	} else if (mi->type == AUDIO_MIXER_SET) {
   8671 		for(i = 0; tbl[i].name; i++)
   8672 		    for(j = 0; j < mi->un.s.num_mem; j++)
   8673 			if (strcmp(mi->un.s.member[j].label.name,
   8674 						tbl[i].name) == 0) {
   8675 				ports->allports |= tbl[i].mask;
   8676 				ports->aumask[ports->nports] = tbl[i].mask;
   8677 				ports->misel[ports->nports] =
   8678 				    mi->un.s.member[j].mask;
   8679 				ports->miport[ports->nports] =
   8680 				    au_portof(sc, mi->un.s.member[j].label.name,
   8681 				    mi->mixer_class);
   8682 				++ports->nports;
   8683 			}
   8684 	}
   8685 }
   8686 
   8687 /*
   8688  * Must be called with sc_lock && sc_exlock held.
   8689  */
   8690 int
   8691 au_set_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
   8692 {
   8693 
   8694 	KASSERT(mutex_owned(sc->sc_lock));
   8695 	KASSERT(sc->sc_exlock);
   8696 
   8697 	ct->type = AUDIO_MIXER_VALUE;
   8698 	ct->un.value.num_channels = 2;
   8699 	ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   8700 	ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   8701 	if (audio_set_port(sc, ct) == 0)
   8702 		return 0;
   8703 	ct->un.value.num_channels = 1;
   8704 	ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
   8705 	return audio_set_port(sc, ct);
   8706 }
   8707 
   8708 /*
   8709  * Must be called with sc_lock && sc_exlock held.
   8710  */
   8711 int
   8712 au_get_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
   8713 {
   8714 	int error;
   8715 
   8716 	KASSERT(mutex_owned(sc->sc_lock));
   8717 	KASSERT(sc->sc_exlock);
   8718 
   8719 	ct->un.value.num_channels = 2;
   8720 	if (audio_get_port(sc, ct) == 0) {
   8721 		*l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   8722 		*r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   8723 	} else {
   8724 		ct->un.value.num_channels = 1;
   8725 		error = audio_get_port(sc, ct);
   8726 		if (error)
   8727 			return error;
   8728 		*r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   8729 	}
   8730 	return 0;
   8731 }
   8732 
   8733 /*
   8734  * Must be called with sc_lock && sc_exlock held.
   8735  */
   8736 int
   8737 au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   8738 	int gain, int balance)
   8739 {
   8740 	mixer_ctrl_t ct;
   8741 	int i, error;
   8742 	int l, r;
   8743 	u_int mask;
   8744 	int nset;
   8745 
   8746 	KASSERT(mutex_owned(sc->sc_lock));
   8747 	KASSERT(sc->sc_exlock);
   8748 
   8749 	if (balance == AUDIO_MID_BALANCE) {
   8750 		l = r = gain;
   8751 	} else if (balance < AUDIO_MID_BALANCE) {
   8752 		l = gain;
   8753 		r = (balance * gain) / AUDIO_MID_BALANCE;
   8754 	} else {
   8755 		r = gain;
   8756 		l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
   8757 		    / AUDIO_MID_BALANCE;
   8758 	}
   8759 	TRACE(2, "gain=%d balance=%d, l=%d r=%d", gain, balance, l, r);
   8760 
   8761 	if (ports->index == -1) {
   8762 	usemaster:
   8763 		if (ports->master == -1)
   8764 			return 0; /* just ignore it silently */
   8765 		ct.dev = ports->master;
   8766 		error = au_set_lr_value(sc, &ct, l, r);
   8767 	} else {
   8768 		ct.dev = ports->index;
   8769 		if (ports->isenum) {
   8770 			ct.type = AUDIO_MIXER_ENUM;
   8771 			error = audio_get_port(sc, &ct);
   8772 			if (error)
   8773 				return error;
   8774 			if (ports->isdual) {
   8775 				if (ports->cur_port == -1)
   8776 					ct.dev = ports->master;
   8777 				else
   8778 					ct.dev = ports->miport[ports->cur_port];
   8779 				error = au_set_lr_value(sc, &ct, l, r);
   8780 			} else {
   8781 				for(i = 0; i < ports->nports; i++)
   8782 				    if (ports->misel[i] == ct.un.ord) {
   8783 					    ct.dev = ports->miport[i];
   8784 					    if (ct.dev == -1 ||
   8785 						au_set_lr_value(sc, &ct, l, r))
   8786 						    goto usemaster;
   8787 					    else
   8788 						    break;
   8789 				    }
   8790 			}
   8791 		} else {
   8792 			ct.type = AUDIO_MIXER_SET;
   8793 			error = audio_get_port(sc, &ct);
   8794 			if (error)
   8795 				return error;
   8796 			mask = ct.un.mask;
   8797 			nset = 0;
   8798 			for(i = 0; i < ports->nports; i++) {
   8799 				if (ports->misel[i] & mask) {
   8800 				    ct.dev = ports->miport[i];
   8801 				    if (ct.dev != -1 &&
   8802 					au_set_lr_value(sc, &ct, l, r) == 0)
   8803 					    nset++;
   8804 				}
   8805 			}
   8806 			if (nset == 0)
   8807 				goto usemaster;
   8808 		}
   8809 	}
   8810 	if (!error)
   8811 		mixer_signal(sc);
   8812 	return error;
   8813 }
   8814 
   8815 /*
   8816  * Must be called with sc_lock && sc_exlock held.
   8817  */
   8818 void
   8819 au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   8820 	u_int *pgain, u_char *pbalance)
   8821 {
   8822 	mixer_ctrl_t ct;
   8823 	int i, l, r, n;
   8824 	int lgain, rgain;
   8825 
   8826 	KASSERT(mutex_owned(sc->sc_lock));
   8827 	KASSERT(sc->sc_exlock);
   8828 
   8829 	lgain = AUDIO_MAX_GAIN / 2;
   8830 	rgain = AUDIO_MAX_GAIN / 2;
   8831 	if (ports->index == -1) {
   8832 	usemaster:
   8833 		if (ports->master == -1)
   8834 			goto bad;
   8835 		ct.dev = ports->master;
   8836 		ct.type = AUDIO_MIXER_VALUE;
   8837 		if (au_get_lr_value(sc, &ct, &lgain, &rgain))
   8838 			goto bad;
   8839 	} else {
   8840 		ct.dev = ports->index;
   8841 		if (ports->isenum) {
   8842 			ct.type = AUDIO_MIXER_ENUM;
   8843 			if (audio_get_port(sc, &ct))
   8844 				goto bad;
   8845 			ct.type = AUDIO_MIXER_VALUE;
   8846 			if (ports->isdual) {
   8847 				if (ports->cur_port == -1)
   8848 					ct.dev = ports->master;
   8849 				else
   8850 					ct.dev = ports->miport[ports->cur_port];
   8851 				au_get_lr_value(sc, &ct, &lgain, &rgain);
   8852 			} else {
   8853 				for(i = 0; i < ports->nports; i++)
   8854 				    if (ports->misel[i] == ct.un.ord) {
   8855 					    ct.dev = ports->miport[i];
   8856 					    if (ct.dev == -1 ||
   8857 						au_get_lr_value(sc, &ct,
   8858 								&lgain, &rgain))
   8859 						    goto usemaster;
   8860 					    else
   8861 						    break;
   8862 				    }
   8863 			}
   8864 		} else {
   8865 			ct.type = AUDIO_MIXER_SET;
   8866 			if (audio_get_port(sc, &ct))
   8867 				goto bad;
   8868 			ct.type = AUDIO_MIXER_VALUE;
   8869 			lgain = rgain = n = 0;
   8870 			for(i = 0; i < ports->nports; i++) {
   8871 				if (ports->misel[i] & ct.un.mask) {
   8872 					ct.dev = ports->miport[i];
   8873 					if (ct.dev == -1 ||
   8874 					    au_get_lr_value(sc, &ct, &l, &r))
   8875 						goto usemaster;
   8876 					else {
   8877 						lgain += l;
   8878 						rgain += r;
   8879 						n++;
   8880 					}
   8881 				}
   8882 			}
   8883 			if (n != 0) {
   8884 				lgain /= n;
   8885 				rgain /= n;
   8886 			}
   8887 		}
   8888 	}
   8889 bad:
   8890 	if (lgain == rgain) {	/* handles lgain==rgain==0 */
   8891 		*pgain = lgain;
   8892 		*pbalance = AUDIO_MID_BALANCE;
   8893 	} else if (lgain < rgain) {
   8894 		*pgain = rgain;
   8895 		/* balance should be > AUDIO_MID_BALANCE */
   8896 		*pbalance = AUDIO_RIGHT_BALANCE -
   8897 			(AUDIO_MID_BALANCE * lgain) / rgain;
   8898 	} else /* lgain > rgain */ {
   8899 		*pgain = lgain;
   8900 		/* balance should be < AUDIO_MID_BALANCE */
   8901 		*pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
   8902 	}
   8903 }
   8904 
   8905 /*
   8906  * Must be called with sc_lock && sc_exlock held.
   8907  */
   8908 int
   8909 au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
   8910 {
   8911 	mixer_ctrl_t ct;
   8912 	int i, error, use_mixerout;
   8913 
   8914 	KASSERT(mutex_owned(sc->sc_lock));
   8915 	KASSERT(sc->sc_exlock);
   8916 
   8917 	use_mixerout = 1;
   8918 	if (port == 0) {
   8919 		if (ports->allports == 0)
   8920 			return 0;		/* Allow this special case. */
   8921 		else if (ports->isdual) {
   8922 			if (ports->cur_port == -1) {
   8923 				return 0;
   8924 			} else {
   8925 				port = ports->aumask[ports->cur_port];
   8926 				ports->cur_port = -1;
   8927 				use_mixerout = 0;
   8928 			}
   8929 		}
   8930 	}
   8931 	if (ports->index == -1)
   8932 		return EINVAL;
   8933 	ct.dev = ports->index;
   8934 	if (ports->isenum) {
   8935 		if (port & (port-1))
   8936 			return EINVAL; /* Only one port allowed */
   8937 		ct.type = AUDIO_MIXER_ENUM;
   8938 		error = EINVAL;
   8939 		for(i = 0; i < ports->nports; i++)
   8940 			if (ports->aumask[i] == port) {
   8941 				if (ports->isdual && use_mixerout) {
   8942 					ct.un.ord = ports->mixerout;
   8943 					ports->cur_port = i;
   8944 				} else {
   8945 					ct.un.ord = ports->misel[i];
   8946 				}
   8947 				error = audio_set_port(sc, &ct);
   8948 				break;
   8949 			}
   8950 	} else {
   8951 		ct.type = AUDIO_MIXER_SET;
   8952 		ct.un.mask = 0;
   8953 		for(i = 0; i < ports->nports; i++)
   8954 			if (ports->aumask[i] & port)
   8955 				ct.un.mask |= ports->misel[i];
   8956 		if (port != 0 && ct.un.mask == 0)
   8957 			error = EINVAL;
   8958 		else
   8959 			error = audio_set_port(sc, &ct);
   8960 	}
   8961 	if (!error)
   8962 		mixer_signal(sc);
   8963 	return error;
   8964 }
   8965 
   8966 /*
   8967  * Must be called with sc_lock && sc_exlock held.
   8968  */
   8969 int
   8970 au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
   8971 {
   8972 	mixer_ctrl_t ct;
   8973 	int i, aumask;
   8974 
   8975 	KASSERT(mutex_owned(sc->sc_lock));
   8976 	KASSERT(sc->sc_exlock);
   8977 
   8978 	if (ports->index == -1)
   8979 		return 0;
   8980 	ct.dev = ports->index;
   8981 	ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
   8982 	if (audio_get_port(sc, &ct))
   8983 		return 0;
   8984 	aumask = 0;
   8985 	if (ports->isenum) {
   8986 		if (ports->isdual && ports->cur_port != -1) {
   8987 			if (ports->mixerout == ct.un.ord)
   8988 				aumask = ports->aumask[ports->cur_port];
   8989 			else
   8990 				ports->cur_port = -1;
   8991 		}
   8992 		if (aumask == 0)
   8993 			for(i = 0; i < ports->nports; i++)
   8994 				if (ports->misel[i] == ct.un.ord)
   8995 					aumask = ports->aumask[i];
   8996 	} else {
   8997 		for(i = 0; i < ports->nports; i++)
   8998 			if (ct.un.mask & ports->misel[i])
   8999 				aumask |= ports->aumask[i];
   9000 	}
   9001 	return aumask;
   9002 }
   9003 
   9004 /*
   9005  * It returns 0 if success, otherwise errno.
   9006  * Must be called only if sc->sc_monitor_port != -1.
   9007  * Must be called with sc_lock && sc_exlock held.
   9008  */
   9009 static int
   9010 au_set_monitor_gain(struct audio_softc *sc, int monitor_gain)
   9011 {
   9012 	mixer_ctrl_t ct;
   9013 
   9014 	KASSERT(mutex_owned(sc->sc_lock));
   9015 	KASSERT(sc->sc_exlock);
   9016 
   9017 	ct.dev = sc->sc_monitor_port;
   9018 	ct.type = AUDIO_MIXER_VALUE;
   9019 	ct.un.value.num_channels = 1;
   9020 	ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = monitor_gain;
   9021 	return audio_set_port(sc, &ct);
   9022 }
   9023 
   9024 /*
   9025  * It returns monitor gain if success, otherwise -1.
   9026  * Must be called only if sc->sc_monitor_port != -1.
   9027  * Must be called with sc_lock && sc_exlock held.
   9028  */
   9029 static int
   9030 au_get_monitor_gain(struct audio_softc *sc)
   9031 {
   9032 	mixer_ctrl_t ct;
   9033 
   9034 	KASSERT(mutex_owned(sc->sc_lock));
   9035 	KASSERT(sc->sc_exlock);
   9036 
   9037 	ct.dev = sc->sc_monitor_port;
   9038 	ct.type = AUDIO_MIXER_VALUE;
   9039 	ct.un.value.num_channels = 1;
   9040 	if (audio_get_port(sc, &ct))
   9041 		return -1;
   9042 	return ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
   9043 }
   9044 
   9045 /*
   9046  * Must be called with sc_lock && sc_exlock held.
   9047  */
   9048 static int
   9049 audio_set_port(struct audio_softc *sc, mixer_ctrl_t *mc)
   9050 {
   9051 
   9052 	KASSERT(mutex_owned(sc->sc_lock));
   9053 	KASSERT(sc->sc_exlock);
   9054 
   9055 	return sc->hw_if->set_port(sc->hw_hdl, mc);
   9056 }
   9057 
   9058 /*
   9059  * Must be called with sc_lock && sc_exlock held.
   9060  */
   9061 static int
   9062 audio_get_port(struct audio_softc *sc, mixer_ctrl_t *mc)
   9063 {
   9064 
   9065 	KASSERT(mutex_owned(sc->sc_lock));
   9066 	KASSERT(sc->sc_exlock);
   9067 
   9068 	return sc->hw_if->get_port(sc->hw_hdl, mc);
   9069 }
   9070 
   9071 /*
   9072  * Must be called with sc_lock && sc_exlock held.
   9073  */
   9074 static void
   9075 audio_mixer_capture(struct audio_softc *sc)
   9076 {
   9077 	mixer_devinfo_t mi;
   9078 	mixer_ctrl_t *mc;
   9079 
   9080 	KASSERT(mutex_owned(sc->sc_lock));
   9081 	KASSERT(sc->sc_exlock);
   9082 
   9083 	for (mi.index = 0;; mi.index++) {
   9084 		if (audio_query_devinfo(sc, &mi) != 0)
   9085 			break;
   9086 		KASSERT(mi.index < sc->sc_nmixer_states);
   9087 		if (mi.type == AUDIO_MIXER_CLASS)
   9088 			continue;
   9089 		mc = &sc->sc_mixer_state[mi.index];
   9090 		mc->dev = mi.index;
   9091 		mc->type = mi.type;
   9092 		mc->un.value.num_channels = mi.un.v.num_channels;
   9093 		(void)audio_get_port(sc, mc);
   9094 	}
   9095 
   9096 	return;
   9097 }
   9098 
   9099 /*
   9100  * Must be called with sc_lock && sc_exlock held.
   9101  */
   9102 static void
   9103 audio_mixer_restore(struct audio_softc *sc)
   9104 {
   9105 	mixer_devinfo_t mi;
   9106 	mixer_ctrl_t *mc;
   9107 
   9108 	KASSERT(mutex_owned(sc->sc_lock));
   9109 	KASSERT(sc->sc_exlock);
   9110 
   9111 	for (mi.index = 0; ; mi.index++) {
   9112 		if (audio_query_devinfo(sc, &mi) != 0)
   9113 			break;
   9114 		if (mi.type == AUDIO_MIXER_CLASS)
   9115 			continue;
   9116 		mc = &sc->sc_mixer_state[mi.index];
   9117 		(void)audio_set_port(sc, mc);
   9118 	}
   9119 	if (sc->hw_if->commit_settings)
   9120 		sc->hw_if->commit_settings(sc->hw_hdl);
   9121 
   9122 	return;
   9123 }
   9124 
   9125 static void
   9126 audio_volume_down(device_t dv)
   9127 {
   9128 	struct audio_softc *sc = device_private(dv);
   9129 	mixer_devinfo_t mi;
   9130 	int newgain;
   9131 	u_int gain;
   9132 	u_char balance;
   9133 
   9134 	if (audio_exlock_mutex_enter(sc) != 0)
   9135 		return;
   9136 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   9137 		mi.index = sc->sc_outports.master;
   9138 		mi.un.v.delta = 0;
   9139 		if (audio_query_devinfo(sc, &mi) == 0) {
   9140 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   9141 			newgain = gain - mi.un.v.delta;
   9142 			if (newgain < AUDIO_MIN_GAIN)
   9143 				newgain = AUDIO_MIN_GAIN;
   9144 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   9145 		}
   9146 	}
   9147 	audio_exlock_mutex_exit(sc);
   9148 }
   9149 
   9150 static void
   9151 audio_volume_up(device_t dv)
   9152 {
   9153 	struct audio_softc *sc = device_private(dv);
   9154 	mixer_devinfo_t mi;
   9155 	u_int gain, newgain;
   9156 	u_char balance;
   9157 
   9158 	if (audio_exlock_mutex_enter(sc) != 0)
   9159 		return;
   9160 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   9161 		mi.index = sc->sc_outports.master;
   9162 		mi.un.v.delta = 0;
   9163 		if (audio_query_devinfo(sc, &mi) == 0) {
   9164 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   9165 			newgain = gain + mi.un.v.delta;
   9166 			if (newgain > AUDIO_MAX_GAIN)
   9167 				newgain = AUDIO_MAX_GAIN;
   9168 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   9169 		}
   9170 	}
   9171 	audio_exlock_mutex_exit(sc);
   9172 }
   9173 
   9174 static void
   9175 audio_volume_toggle(device_t dv)
   9176 {
   9177 	struct audio_softc *sc = device_private(dv);
   9178 	u_int gain, newgain;
   9179 	u_char balance;
   9180 
   9181 	if (audio_exlock_mutex_enter(sc) != 0)
   9182 		return;
   9183 	au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   9184 	if (gain != 0) {
   9185 		sc->sc_lastgain = gain;
   9186 		newgain = 0;
   9187 	} else
   9188 		newgain = sc->sc_lastgain;
   9189 	au_set_gain(sc, &sc->sc_outports, newgain, balance);
   9190 	audio_exlock_mutex_exit(sc);
   9191 }
   9192 
   9193 /*
   9194  * Must be called with sc_lock held.
   9195  */
   9196 static int
   9197 audio_query_devinfo(struct audio_softc *sc, mixer_devinfo_t *di)
   9198 {
   9199 
   9200 	KASSERT(mutex_owned(sc->sc_lock));
   9201 
   9202 	return sc->hw_if->query_devinfo(sc->hw_hdl, di);
   9203 }
   9204 
   9205 void
   9206 audio_mixsample_to_linear(audio_filter_arg_t *arg)
   9207 {
   9208 	const audio_format2_t *fmt;
   9209 	const aint2_t *m;
   9210 	uint8_t *p;
   9211 	u_int sample_count;
   9212 	bool swap;
   9213 	aint2_t v, xor;
   9214 	u_int i, bps;
   9215 	bool little;
   9216 
   9217 	DIAGNOSTIC_filter_arg(arg);
   9218 	KASSERT(audio_format2_is_linear(arg->dstfmt));
   9219 	KASSERT(arg->srcfmt->channels == arg->dstfmt->channels);
   9220 
   9221 	fmt = arg->dstfmt;
   9222 	m = arg->src;
   9223 	p = arg->dst;
   9224 	sample_count = arg->count * fmt->channels;
   9225 	swap = arg->dstfmt->encoding == AUDIO_ENCODING_SLINEAR_OE;
   9226 
   9227 #if BYTE_ORDER == LITTLE_ENDIAN
   9228 	little = !swap;
   9229 #endif
   9230 #if BYTE_ORDER == BIG_ENDIAN
   9231 	little = swap;
   9232 #endif
   9233 
   9234 	bps = fmt->stride / NBBY;
   9235 
   9236 	xor = audio_format2_is_signed(fmt)
   9237 	   ? 0 : 1 << (fmt->stride - 1);
   9238 
   9239 	for (i=0; i<sample_count; ++i) {
   9240 		v = *m++;
   9241 
   9242 		/* scale up to 32bit and then down to target size */
   9243 		v <<= 32 - AUDIO_INTERNAL_BITS;
   9244 		v >>= (4 - bps) * NBBY;
   9245 
   9246 		/* signed -> unsigned */
   9247 		v ^= xor;
   9248 
   9249 		if (little) {
   9250 			switch (bps) {
   9251 			case 4: *p++ = v; v >>= 8; /* FALLTHROUGH */
   9252 			case 3: *p++ = v; v >>= 8; /* FALLTHROUGH */
   9253 			case 2: *p++ = v; v >>= 8; /* FALLTHROUGH */
   9254 			case 1: *p++ = v; /* FALLTHROUGH */
   9255 			}
   9256 		} else {
   9257 			switch (bps) {
   9258 			case 4: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
   9259 			case 3: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
   9260 			case 2: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
   9261 			case 1: *p++ = v >> 24; /* FALLTHROUGH */
   9262 			}
   9263 		}
   9264 	}
   9265 }
   9266 
   9267 
   9268 #endif /* NAUDIO > 0 */
   9269 
   9270 #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
   9271 #include <sys/param.h>
   9272 #include <sys/systm.h>
   9273 #include <sys/device.h>
   9274 #include <sys/audioio.h>
   9275 #include <dev/audio/audio_if.h>
   9276 #endif
   9277 
   9278 #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
   9279 int
   9280 audioprint(void *aux, const char *pnp)
   9281 {
   9282 	struct audio_attach_args *arg;
   9283 	const char *type;
   9284 
   9285 	if (pnp != NULL) {
   9286 		arg = aux;
   9287 		switch (arg->type) {
   9288 		case AUDIODEV_TYPE_AUDIO:
   9289 			type = "audio";
   9290 			break;
   9291 		case AUDIODEV_TYPE_MIDI:
   9292 			type = "midi";
   9293 			break;
   9294 		case AUDIODEV_TYPE_OPL:
   9295 			type = "opl";
   9296 			break;
   9297 		case AUDIODEV_TYPE_MPU:
   9298 			type = "mpu";
   9299 			break;
   9300 		case AUDIODEV_TYPE_AUX:
   9301 			type = "aux";
   9302 			break;
   9303 		default:
   9304 			panic("audioprint: unknown type %d", arg->type);
   9305 		}
   9306 		aprint_normal("%s at %s", type, pnp);
   9307 	}
   9308 	return UNCONF;
   9309 }
   9310 
   9311 #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */
   9312 
   9313 #ifdef _MODULE
   9314 
   9315 devmajor_t audio_bmajor = -1, audio_cmajor = -1;
   9316 
   9317 #include "ioconf.c"
   9318 
   9319 #endif
   9320 
   9321 MODULE(MODULE_CLASS_DRIVER, audio, NULL);
   9322 
   9323 static int
   9324 audio_modcmd(modcmd_t cmd, void *arg)
   9325 {
   9326 	int error = 0;
   9327 
   9328 	switch (cmd) {
   9329 	case MODULE_CMD_INIT:
   9330 		/* XXX interrupt level? */
   9331 		audio_psref_class = psref_class_create("audio", IPL_SOFTSERIAL);
   9332 #ifdef _MODULE
   9333 		error = devsw_attach(audio_cd.cd_name, NULL, &audio_bmajor,
   9334 		    &audio_cdevsw, &audio_cmajor);
   9335 		if (error)
   9336 			break;
   9337 
   9338 		error = config_init_component(cfdriver_ioconf_audio,
   9339 		    cfattach_ioconf_audio, cfdata_ioconf_audio);
   9340 		if (error) {
   9341 			devsw_detach(NULL, &audio_cdevsw);
   9342 		}
   9343 #endif
   9344 		break;
   9345 	case MODULE_CMD_FINI:
   9346 #ifdef _MODULE
   9347 		error = config_fini_component(cfdriver_ioconf_audio,
   9348 		   cfattach_ioconf_audio, cfdata_ioconf_audio);
   9349 		if (error == 0)
   9350 			devsw_detach(NULL, &audio_cdevsw);
   9351 #endif
   9352 		if (error == 0)
   9353 			psref_class_destroy(audio_psref_class);
   9354 		break;
   9355 	default:
   9356 		error = ENOTTY;
   9357 		break;
   9358 	}
   9359 
   9360 	return error;
   9361 }
   9362