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