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