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