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