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