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