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