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