Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: audio.c,v 1.267 2016/04/23 10:15:31 skrll 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  * This is a (partially) SunOS-compatible /dev/audio driver for NetBSD.
     67  *
     68  * This code tries to do something half-way sensible with
     69  * half-duplex hardware, such as with the SoundBlaster hardware.  With
     70  * half-duplex hardware allowing O_RDWR access doesn't really make
     71  * sense.  However, closing and opening the device to "turn around the
     72  * line" is relatively expensive and costs a card reset (which can
     73  * take some time, at least for the SoundBlaster hardware).  Instead
     74  * we allow O_RDWR access, and provide an ioctl to set the "mode",
     75  * i.e. playing or recording.
     76  *
     77  * If you write to a half-duplex device in record mode, the data is
     78  * tossed.  If you read from the device in play mode, you get silence
     79  * filled buffers at the rate at which samples are naturally
     80  * generated.
     81  *
     82  * If you try to set both play and record mode on a half-duplex
     83  * device, playing takes precedence.
     84  */
     85 
     86 /*
     87  * Locking: there are three locks.
     88  *
     89  * - sc_lock, provided by the underlying driver.  This is an adaptive lock,
     90  *   returned in the second parameter to hw_if->get_locks().  It is known
     91  *   as the "thread lock".
     92  *
     93  *   It serializes access to state in all places except the
     94  *   driver's interrupt service routine.  This lock is taken from process
     95  *   context (example: access to /dev/audio).  It is also taken from soft
     96  *   interrupt handlers in this module, primarily to serialize delivery of
     97  *   wakeups.  This lock may be used/provided by modules external to the
     98  *   audio subsystem, so take care not to introduce a lock order problem.
     99  *   LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
    100  *
    101  * - sc_intr_lock, provided by the underlying driver.  This may be either a
    102  *   spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
    103  *   IPL_SOFT*), returned in the first parameter to hw_if->get_locks().  It
    104  *   is known as the "interrupt lock".
    105  *
    106  *   It provides atomic access to the device's hardware state, and to audio
    107  *   channel data that may be accessed by the hardware driver's ISR.
    108  *   In all places outside the ISR, sc_lock must be held before taking
    109  *   sc_intr_lock.  This is to ensure that groups of hardware operations are
    110  *   made atomically.  SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
    111  *
    112  * - sc_dvlock, private to this module.  This is a custom reader/writer lock
    113  *   built on sc_lock and a condition variable.  Some operations release
    114  *   sc_lock in order to allocate memory, to wait for in-flight I/O to
    115  *   complete, to copy to/from user context, etc.  sc_dvlock serializes
    116  *   changes to filters and audio device settings while a read/write to the
    117  *   hardware is in progress.  A write lock is taken only under exceptional
    118  *   circumstances, for example when opening /dev/audio or changing audio
    119  *   parameters.  Long term sleeps and copy to/from user space may be done
    120  *   with this lock held.
    121  *
    122  * List of hardware interface methods, and which locks are held when each
    123  * is called by this module:
    124  *
    125  *	METHOD			INTR	THREAD  NOTES
    126  *	----------------------- ------- -------	-------------------------
    127  *	open 			x	x
    128  *	close 			x	x
    129  *	drain 			x	x
    130  *	query_encoding		-	x
    131  *	set_params 		-	x
    132  *	round_blocksize		-	x
    133  *	commit_settings		-	x
    134  *	init_output 		x	x
    135  *	init_input 		x	x
    136  *	start_output 		x	x
    137  *	start_input 		x	x
    138  *	halt_output 		x	x
    139  *	halt_input 		x	x
    140  *	speaker_ctl 		x	x
    141  *	getdev 			-	x
    142  *	setfd 			-	x
    143  *	set_port 		-	x
    144  *	get_port 		-	x
    145  *	query_devinfo 		-	x
    146  *	allocm 			-	-	Called at attach time
    147  *	freem 			-	-	Called at attach time
    148  *	round_buffersize 	-	x
    149  *	mappage 		-	-	Mem. unchanged after attach
    150  *	get_props 		-	x
    151  *	trigger_output 		x	x
    152  *	trigger_input 		x	x
    153  *	dev_ioctl 		-	x
    154  *	get_locks 		-	-	Called at attach time
    155  */
    156 
    157 #include <sys/cdefs.h>
    158 __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.267 2016/04/23 10:15:31 skrll Exp $");
    159 
    160 #include "audio.h"
    161 #if NAUDIO > 0
    162 
    163 #include <sys/param.h>
    164 #include <sys/ioctl.h>
    165 #include <sys/fcntl.h>
    166 #include <sys/vnode.h>
    167 #include <sys/select.h>
    168 #include <sys/poll.h>
    169 #include <sys/kmem.h>
    170 #include <sys/malloc.h>
    171 #include <sys/proc.h>
    172 #include <sys/systm.h>
    173 #include <sys/syslog.h>
    174 #include <sys/kernel.h>
    175 #include <sys/signalvar.h>
    176 #include <sys/conf.h>
    177 #include <sys/audioio.h>
    178 #include <sys/device.h>
    179 #include <sys/intr.h>
    180 #include <sys/cpu.h>
    181 
    182 #include <dev/audio_if.h>
    183 #include <dev/audiovar.h>
    184 
    185 #include <machine/endian.h>
    186 
    187 /* #define AUDIO_DEBUG	1 */
    188 #ifdef AUDIO_DEBUG
    189 #define DPRINTF(x)	if (audiodebug) printf x
    190 #define DPRINTFN(n,x)	if (audiodebug>(n)) printf x
    191 int	audiodebug = AUDIO_DEBUG;
    192 #else
    193 #define DPRINTF(x)
    194 #define DPRINTFN(n,x)
    195 #endif
    196 
    197 #define ROUNDSIZE(x)	x &= -16	/* round to nice boundary */
    198 #define SPECIFIED(x)	(x != ~0)
    199 #define SPECIFIED_CH(x)	(x != (u_char)~0)
    200 
    201 /* #define AUDIO_PM_IDLE */
    202 #ifdef AUDIO_PM_IDLE
    203 int	audio_idle_timeout = 30;
    204 #endif
    205 
    206 int	audio_blk_ms = AUDIO_BLK_MS;
    207 
    208 int	audiosetinfo(struct audio_softc *, struct audio_info *);
    209 int	audiogetinfo(struct audio_softc *, struct audio_info *, int);
    210 
    211 int	audio_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    212 int	audio_close(struct audio_softc *, int, int, struct lwp *);
    213 int	audio_read(struct audio_softc *, struct uio *, int);
    214 int	audio_write(struct audio_softc *, struct uio *, int);
    215 int	audio_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
    216 int	audio_poll(struct audio_softc *, int, struct lwp *);
    217 int	audio_kqfilter(struct audio_softc *, struct knote *);
    218 paddr_t	audio_mmap(struct audio_softc *, off_t, int);
    219 
    220 int	mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    221 int	mixer_close(struct audio_softc *, int, int, struct lwp *);
    222 int	mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
    223 static	void mixer_remove(struct audio_softc *);
    224 static	void mixer_signal(struct audio_softc *);
    225 
    226 void	audio_init_record(struct audio_softc *);
    227 void	audio_init_play(struct audio_softc *);
    228 int	audiostartr(struct audio_softc *);
    229 int	audiostartp(struct audio_softc *);
    230 void	audio_rint(void *);
    231 void	audio_pint(void *);
    232 int	audio_check_params(struct audio_params *);
    233 
    234 void	audio_calc_blksize(struct audio_softc *, int);
    235 void	audio_fill_silence(struct audio_params *, uint8_t *, int);
    236 int	audio_silence_copyout(struct audio_softc *, int, struct uio *);
    237 
    238 void	audio_init_ringbuffer(struct audio_softc *,
    239 			      struct audio_ringbuffer *, int);
    240 int	audio_initbufs(struct audio_softc *);
    241 void	audio_calcwater(struct audio_softc *);
    242 int	audio_drain(struct audio_softc *);
    243 void	audio_clear(struct audio_softc *);
    244 void	audio_clear_intr_unlocked(struct audio_softc *sc);
    245 static inline void audio_pint_silence
    246 	(struct audio_softc *, struct audio_ringbuffer *, uint8_t *, int);
    247 
    248 int	audio_alloc_ring
    249 	(struct audio_softc *, struct audio_ringbuffer *, int, size_t);
    250 void	audio_free_ring(struct audio_softc *, struct audio_ringbuffer *);
    251 static int audio_setup_pfilters(struct audio_softc *, const audio_params_t *,
    252 				stream_filter_list_t *);
    253 static int audio_setup_rfilters(struct audio_softc *, const audio_params_t *,
    254 				stream_filter_list_t *);
    255 static void audio_stream_dtor(audio_stream_t *);
    256 static int audio_stream_ctor(audio_stream_t *, const audio_params_t *, int);
    257 static void stream_filter_list_append
    258 	(stream_filter_list_t *, stream_filter_factory_t,
    259 	 const audio_params_t *);
    260 static void stream_filter_list_prepend
    261 	(stream_filter_list_t *, stream_filter_factory_t,
    262 	 const audio_params_t *);
    263 static void stream_filter_list_set
    264 	(stream_filter_list_t *, int, stream_filter_factory_t,
    265 	 const audio_params_t *);
    266 int	audio_set_defaults(struct audio_softc *, u_int);
    267 
    268 int	audioprobe(device_t, cfdata_t, void *);
    269 void	audioattach(device_t, device_t, void *);
    270 int	audiodetach(device_t, int);
    271 int	audioactivate(device_t, enum devact);
    272 
    273 #ifdef AUDIO_PM_IDLE
    274 static void	audio_idle(void *);
    275 static void	audio_activity(device_t, devactive_t);
    276 #endif
    277 
    278 static bool	audio_suspend(device_t dv, const pmf_qual_t *);
    279 static bool	audio_resume(device_t dv, const pmf_qual_t *);
    280 static void	audio_volume_down(device_t);
    281 static void	audio_volume_up(device_t);
    282 static void	audio_volume_toggle(device_t);
    283 
    284 static void	audio_mixer_capture(struct audio_softc *);
    285 static void	audio_mixer_restore(struct audio_softc *);
    286 
    287 static int	audio_get_props(struct audio_softc *);
    288 static bool	audio_can_playback(struct audio_softc *);
    289 static bool	audio_can_capture(struct audio_softc *);
    290 
    291 static void	audio_softintr_rd(void *);
    292 static void	audio_softintr_wr(void *);
    293 
    294 static int	audio_enter(dev_t, krw_t, struct audio_softc **);
    295 static void	audio_exit(struct audio_softc *);
    296 static int	audio_waitio(struct audio_softc *, kcondvar_t *);
    297 
    298 struct portname {
    299 	const char *name;
    300 	int mask;
    301 };
    302 static const struct portname itable[] = {
    303 	{ AudioNmicrophone,	AUDIO_MICROPHONE },
    304 	{ AudioNline,		AUDIO_LINE_IN },
    305 	{ AudioNcd,		AUDIO_CD },
    306 	{ 0, 0 }
    307 };
    308 static const struct portname otable[] = {
    309 	{ AudioNspeaker,	AUDIO_SPEAKER },
    310 	{ AudioNheadphone,	AUDIO_HEADPHONE },
    311 	{ AudioNline,		AUDIO_LINE_OUT },
    312 	{ 0, 0 }
    313 };
    314 void	au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
    315 			mixer_devinfo_t *, const struct portname *);
    316 int	au_set_gain(struct audio_softc *, struct au_mixer_ports *,
    317 			int, int);
    318 void	au_get_gain(struct audio_softc *, struct au_mixer_ports *,
    319 			u_int *, u_char *);
    320 int	au_set_port(struct audio_softc *, struct au_mixer_ports *,
    321 			u_int);
    322 int	au_get_port(struct audio_softc *, struct au_mixer_ports *);
    323 int	au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
    324 int	au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
    325 int	au_portof(struct audio_softc *, char *, int);
    326 
    327 typedef struct uio_fetcher {
    328 	stream_fetcher_t base;
    329 	struct uio *uio;
    330 	int usedhigh;
    331 	int last_used;
    332 } uio_fetcher_t;
    333 
    334 static void	uio_fetcher_ctor(uio_fetcher_t *, struct uio *, int);
    335 static int	uio_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *,
    336 				     audio_stream_t *, int);
    337 static int	null_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *,
    338 				      audio_stream_t *, int);
    339 
    340 dev_type_open(audioopen);
    341 dev_type_close(audioclose);
    342 dev_type_read(audioread);
    343 dev_type_write(audiowrite);
    344 dev_type_ioctl(audioioctl);
    345 dev_type_poll(audiopoll);
    346 dev_type_mmap(audiommap);
    347 dev_type_kqfilter(audiokqfilter);
    348 
    349 const struct cdevsw audio_cdevsw = {
    350 	.d_open = audioopen,
    351 	.d_close = audioclose,
    352 	.d_read = audioread,
    353 	.d_write = audiowrite,
    354 	.d_ioctl = audioioctl,
    355 	.d_stop = nostop,
    356 	.d_tty = notty,
    357 	.d_poll = audiopoll,
    358 	.d_mmap = audiommap,
    359 	.d_kqfilter = audiokqfilter,
    360 	.d_discard = nodiscard,
    361 	.d_flag = D_OTHER | D_MPSAFE
    362 };
    363 
    364 /* The default audio mode: 8 kHz mono mu-law */
    365 const struct audio_params audio_default = {
    366 	.sample_rate = 8000,
    367 	.encoding = AUDIO_ENCODING_ULAW,
    368 	.precision = 8,
    369 	.validbits = 8,
    370 	.channels = 1,
    371 };
    372 
    373 CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
    374     audioprobe, audioattach, audiodetach, audioactivate, NULL, NULL,
    375     DVF_DETACH_SHUTDOWN);
    376 
    377 extern struct cfdriver audio_cd;
    378 
    379 int
    380 audioprobe(device_t parent, cfdata_t match, void *aux)
    381 {
    382 	struct audio_attach_args *sa;
    383 
    384 	sa = aux;
    385 	DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n",
    386 		 sa->type, sa, sa->hwif));
    387 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
    388 }
    389 
    390 void
    391 audioattach(device_t parent, device_t self, void *aux)
    392 {
    393 	struct audio_softc *sc;
    394 	struct audio_attach_args *sa;
    395 	const struct audio_hw_if *hwp;
    396 	void *hdlp;
    397 	int error;
    398 	mixer_devinfo_t mi;
    399 	int iclass, mclass, oclass, rclass, props;
    400 	int record_master_found, record_source_found;
    401 	bool can_capture, can_playback;
    402 
    403 	sc = device_private(self);
    404 	sc->dev = self;
    405 	sa = aux;
    406 	hwp = sa->hwif;
    407 	hdlp = sa->hdl;
    408 
    409 	cv_init(&sc->sc_rchan, "audiord");
    410 	cv_init(&sc->sc_wchan, "audiowr");
    411 	cv_init(&sc->sc_lchan, "audiolk");
    412 
    413 	if (hwp == 0 || hwp->get_locks == 0) {
    414 		aprint_error(": missing method\n");
    415 		panic("audioattach");
    416 	}
    417 
    418 	hwp->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
    419 
    420 #ifdef DIAGNOSTIC
    421 	if (hwp->query_encoding == 0 ||
    422 	    hwp->set_params == 0 ||
    423 	    (hwp->start_output == 0 && hwp->trigger_output == 0) ||
    424 	    (hwp->start_input == 0 && hwp->trigger_input == 0) ||
    425 	    hwp->halt_output == 0 ||
    426 	    hwp->halt_input == 0 ||
    427 	    hwp->getdev == 0 ||
    428 	    hwp->set_port == 0 ||
    429 	    hwp->get_port == 0 ||
    430 	    hwp->query_devinfo == 0 ||
    431 	    hwp->get_props == 0) {
    432 		aprint_error(": missing method\n");
    433 		sc->hw_if = 0;
    434 		return;
    435 	}
    436 #endif
    437 
    438 	sc->hw_if = hwp;
    439 	sc->hw_hdl = hdlp;
    440 	sc->sc_dev = parent;
    441 	sc->sc_lastinfovalid = false;
    442 
    443 	mutex_enter(sc->sc_lock);
    444 	props = audio_get_props(sc);
    445 	mutex_exit(sc->sc_lock);
    446 
    447 	if (props & AUDIO_PROP_FULLDUPLEX)
    448 		aprint_normal(": full duplex");
    449 	else
    450 		aprint_normal(": half duplex");
    451 
    452 	if (props & AUDIO_PROP_PLAYBACK)
    453 		aprint_normal(", playback");
    454 	if (props & AUDIO_PROP_CAPTURE)
    455 		aprint_normal(", capture");
    456 	if (props & AUDIO_PROP_MMAP)
    457 		aprint_normal(", mmap");
    458 	if (props & AUDIO_PROP_INDEPENDENT)
    459 		aprint_normal(", independent");
    460 
    461 	aprint_naive("\n");
    462 	aprint_normal("\n");
    463 
    464 	mutex_enter(sc->sc_lock);
    465 	can_playback = audio_can_playback(sc);
    466 	can_capture = audio_can_capture(sc);
    467  	mutex_exit(sc->sc_lock);
    468 
    469 	if (can_playback) {
    470 		error = audio_alloc_ring(sc, &sc->sc_pr,
    471 		    AUMODE_PLAY, AU_RING_SIZE);
    472 		if (error) {
    473 			sc->hw_if = NULL;
    474 			aprint_error("audio: could not allocate play buffer\n");
    475 			return;
    476 		}
    477 	}
    478 	if (can_capture) {
    479 		error = audio_alloc_ring(sc, &sc->sc_rr,
    480 		    AUMODE_RECORD, AU_RING_SIZE);
    481 		if (error) {
    482 			if (sc->sc_pr.s.start != 0)
    483 				audio_free_ring(sc, &sc->sc_pr);
    484 			sc->hw_if = NULL;
    485 			aprint_error("audio: could not allocate record buffer\n");
    486 			return;
    487 		}
    488 	}
    489 
    490 	sc->sc_lastgain = 128;
    491 
    492 	mutex_enter(sc->sc_lock);
    493 	error = audio_set_defaults(sc, 0);
    494 	mutex_exit(sc->sc_lock);
    495 	if (error != 0) {
    496 		aprint_error("audioattach: audio_set_defaults() failed\n");
    497 		sc->hw_if = NULL;
    498 		return;
    499 	}
    500 
    501 	sc->sc_sih_rd = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
    502 	    audio_softintr_rd, sc);
    503 	sc->sc_sih_wr = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
    504 	    audio_softintr_wr, sc);
    505 
    506 	iclass = mclass = oclass = rclass = -1;
    507 	sc->sc_inports.index = -1;
    508 	sc->sc_inports.master = -1;
    509 	sc->sc_inports.nports = 0;
    510 	sc->sc_inports.isenum = false;
    511 	sc->sc_inports.allports = 0;
    512 	sc->sc_inports.isdual = false;
    513 	sc->sc_inports.mixerout = -1;
    514 	sc->sc_inports.cur_port = -1;
    515 	sc->sc_outports.index = -1;
    516 	sc->sc_outports.master = -1;
    517 	sc->sc_outports.nports = 0;
    518 	sc->sc_outports.isenum = false;
    519 	sc->sc_outports.allports = 0;
    520 	sc->sc_outports.isdual = false;
    521 	sc->sc_outports.mixerout = -1;
    522 	sc->sc_outports.cur_port = -1;
    523 	sc->sc_monitor_port = -1;
    524 	/*
    525 	 * Read through the underlying driver's list, picking out the class
    526 	 * names from the mixer descriptions. We'll need them to decode the
    527 	 * mixer descriptions on the next pass through the loop.
    528 	 */
    529 	mutex_enter(sc->sc_lock);
    530 	for(mi.index = 0; ; mi.index++) {
    531 		if (hwp->query_devinfo(hdlp, &mi) != 0)
    532 			break;
    533 		 /*
    534 		  * The type of AUDIO_MIXER_CLASS merely introduces a class.
    535 		  * All the other types describe an actual mixer.
    536 		  */
    537 		if (mi.type == AUDIO_MIXER_CLASS) {
    538 			if (strcmp(mi.label.name, AudioCinputs) == 0)
    539 				iclass = mi.mixer_class;
    540 			if (strcmp(mi.label.name, AudioCmonitor) == 0)
    541 				mclass = mi.mixer_class;
    542 			if (strcmp(mi.label.name, AudioCoutputs) == 0)
    543 				oclass = mi.mixer_class;
    544 			if (strcmp(mi.label.name, AudioCrecord) == 0)
    545 				rclass = mi.mixer_class;
    546 		}
    547 	}
    548 	mutex_exit(sc->sc_lock);
    549 
    550 	/* Allocate save area.  Ensure non-zero allocation. */
    551 	sc->sc_nmixer_states = mi.index;
    552 	sc->sc_mixer_state = kmem_alloc(sizeof(mixer_ctrl_t) *
    553 	    sc->sc_nmixer_states + 1, KM_SLEEP);
    554 
    555 	/*
    556 	 * This is where we assign each control in the "audio" model, to the
    557 	 * underlying "mixer" control.  We walk through the whole list once,
    558 	 * assigning likely candidates as we come across them.
    559 	 */
    560 	record_master_found = 0;
    561 	record_source_found = 0;
    562 	mutex_enter(sc->sc_lock);
    563 	for(mi.index = 0; ; mi.index++) {
    564 		if (hwp->query_devinfo(hdlp, &mi) != 0)
    565 			break;
    566 		KASSERT(mi.index < sc->sc_nmixer_states);
    567 		if (mi.type == AUDIO_MIXER_CLASS)
    568 			continue;
    569 		if (mi.mixer_class == iclass) {
    570 			/*
    571 			 * AudioCinputs is only a fallback, when we don't
    572 			 * find what we're looking for in AudioCrecord, so
    573 			 * check the flags before accepting one of these.
    574 			 */
    575 			if (strcmp(mi.label.name, AudioNmaster) == 0
    576 			    && record_master_found == 0)
    577 				sc->sc_inports.master = mi.index;
    578 			if (strcmp(mi.label.name, AudioNsource) == 0
    579 			    && record_source_found == 0) {
    580 				if (mi.type == AUDIO_MIXER_ENUM) {
    581 				    int i;
    582 				    for(i = 0; i < mi.un.e.num_mem; i++)
    583 					if (strcmp(mi.un.e.member[i].label.name,
    584 						    AudioNmixerout) == 0)
    585 						sc->sc_inports.mixerout =
    586 						    mi.un.e.member[i].ord;
    587 				}
    588 				au_setup_ports(sc, &sc->sc_inports, &mi,
    589 				    itable);
    590 			}
    591 			if (strcmp(mi.label.name, AudioNdac) == 0 &&
    592 			    sc->sc_outports.master == -1)
    593 				sc->sc_outports.master = mi.index;
    594 		} else if (mi.mixer_class == mclass) {
    595 			if (strcmp(mi.label.name, AudioNmonitor) == 0)
    596 				sc->sc_monitor_port = mi.index;
    597 		} else if (mi.mixer_class == oclass) {
    598 			if (strcmp(mi.label.name, AudioNmaster) == 0)
    599 				sc->sc_outports.master = mi.index;
    600 			if (strcmp(mi.label.name, AudioNselect) == 0)
    601 				au_setup_ports(sc, &sc->sc_outports, &mi,
    602 				    otable);
    603 		} else if (mi.mixer_class == rclass) {
    604 			/*
    605 			 * These are the preferred mixers for the audio record
    606 			 * controls, so set the flags here, but don't check.
    607 			 */
    608 			if (strcmp(mi.label.name, AudioNmaster) == 0) {
    609 				sc->sc_inports.master = mi.index;
    610 				record_master_found = 1;
    611 			}
    612 #if 1	/* Deprecated. Use AudioNmaster. */
    613 			if (strcmp(mi.label.name, AudioNrecord) == 0) {
    614 				sc->sc_inports.master = mi.index;
    615 				record_master_found = 1;
    616 			}
    617 			if (strcmp(mi.label.name, AudioNvolume) == 0) {
    618 				sc->sc_inports.master = mi.index;
    619 				record_master_found = 1;
    620 			}
    621 #endif
    622 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    623 				if (mi.type == AUDIO_MIXER_ENUM) {
    624 				    int i;
    625 				    for(i = 0; i < mi.un.e.num_mem; i++)
    626 					if (strcmp(mi.un.e.member[i].label.name,
    627 						    AudioNmixerout) == 0)
    628 						sc->sc_inports.mixerout =
    629 						    mi.un.e.member[i].ord;
    630 				}
    631 				au_setup_ports(sc, &sc->sc_inports, &mi,
    632 				    itable);
    633 				record_source_found = 1;
    634 			}
    635 		}
    636 	}
    637 	mutex_exit(sc->sc_lock);
    638 	DPRINTF(("audio_attach: inputs ports=0x%x, input master=%d, "
    639 		 "output ports=0x%x, output master=%d\n",
    640 		 sc->sc_inports.allports, sc->sc_inports.master,
    641 		 sc->sc_outports.allports, sc->sc_outports.master));
    642 
    643 	selinit(&sc->sc_rsel);
    644 	selinit(&sc->sc_wsel);
    645 
    646 #ifdef AUDIO_PM_IDLE
    647 	callout_init(&sc->sc_idle_counter, 0);
    648 	callout_setfunc(&sc->sc_idle_counter, audio_idle, self);
    649 #endif
    650 
    651 	if (!pmf_device_register(self, audio_suspend, audio_resume))
    652 		aprint_error_dev(self, "couldn't establish power handler\n");
    653 #ifdef AUDIO_PM_IDLE
    654 	if (!device_active_register(self, audio_activity))
    655 		aprint_error_dev(self, "couldn't register activity handler\n");
    656 #endif
    657 
    658 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
    659 	    audio_volume_down, true))
    660 		aprint_error_dev(self, "couldn't add volume down handler\n");
    661 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
    662 	    audio_volume_up, true))
    663 		aprint_error_dev(self, "couldn't add volume up handler\n");
    664 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE,
    665 	    audio_volume_toggle, true))
    666 		aprint_error_dev(self, "couldn't add volume toggle handler\n");
    667 
    668 #ifdef AUDIO_PM_IDLE
    669 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
    670 #endif
    671 }
    672 
    673 int
    674 audioactivate(device_t self, enum devact act)
    675 {
    676 	struct audio_softc *sc = device_private(self);
    677 
    678 	switch (act) {
    679 	case DVACT_DEACTIVATE:
    680 		mutex_enter(sc->sc_lock);
    681 		sc->sc_dying = true;
    682 		mutex_exit(sc->sc_lock);
    683 		return 0;
    684 	default:
    685 		return EOPNOTSUPP;
    686 	}
    687 }
    688 
    689 int
    690 audiodetach(device_t self, int flags)
    691 {
    692 	struct audio_softc *sc;
    693 	int maj, mn, i;
    694 
    695 	sc = device_private(self);
    696 	DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags));
    697 
    698 	/* Start draining existing accessors of the device. */
    699 	mutex_enter(sc->sc_lock);
    700 	sc->sc_dying = true;
    701 	cv_broadcast(&sc->sc_wchan);
    702 	cv_broadcast(&sc->sc_rchan);
    703 	mutex_exit(sc->sc_lock);
    704 
    705 	/* locate the major number */
    706 	maj = cdevsw_lookup_major(&audio_cdevsw);
    707 
    708 	/*
    709 	 * Nuke the vnodes for any open instances (calls close).
    710 	 * Will wait until any activity on the device nodes has ceased.
    711 	 *
    712 	 * XXXAD NOT YET.
    713 	 *
    714 	 * XXXAD NEED TO PREVENT NEW REFERENCES THROUGH AUDIO_ENTER().
    715 	 */
    716 	mn = device_unit(self);
    717 	vdevgone(maj, mn | SOUND_DEVICE,    mn | SOUND_DEVICE, VCHR);
    718 	vdevgone(maj, mn | AUDIO_DEVICE,    mn | AUDIO_DEVICE, VCHR);
    719 	vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR);
    720 	vdevgone(maj, mn | MIXER_DEVICE,    mn | MIXER_DEVICE, VCHR);
    721 
    722 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN,
    723 	    audio_volume_down, true);
    724 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP,
    725 	    audio_volume_up, true);
    726 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE,
    727 	    audio_volume_toggle, true);
    728 
    729 #ifdef AUDIO_PM_IDLE
    730 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
    731 
    732 	device_active_deregister(self, audio_activity);
    733 #endif
    734 
    735 	pmf_device_deregister(self);
    736 
    737 	/* free resources */
    738 	audio_free_ring(sc, &sc->sc_pr);
    739 	audio_free_ring(sc, &sc->sc_rr);
    740 	for (i = 0; i < sc->sc_nrfilters; i++) {
    741 		sc->sc_rfilters[i]->dtor(sc->sc_rfilters[i]);
    742 		sc->sc_rfilters[i] = NULL;
    743 		audio_stream_dtor(&sc->sc_rstreams[i]);
    744 	}
    745 	sc->sc_nrfilters = 0;
    746 	for (i = 0; i < sc->sc_npfilters; i++) {
    747 		sc->sc_pfilters[i]->dtor(sc->sc_pfilters[i]);
    748 		sc->sc_pfilters[i] = NULL;
    749 		audio_stream_dtor(&sc->sc_pstreams[i]);
    750 	}
    751 	sc->sc_npfilters = 0;
    752 
    753 	if (sc->sc_sih_rd) {
    754 		softint_disestablish(sc->sc_sih_rd);
    755 		sc->sc_sih_rd = NULL;
    756 	}
    757 	if (sc->sc_sih_wr) {
    758 		softint_disestablish(sc->sc_sih_wr);
    759 		sc->sc_sih_wr = NULL;
    760 	}
    761 
    762 #ifdef AUDIO_PM_IDLE
    763 	callout_destroy(&sc->sc_idle_counter);
    764 #endif
    765 	seldestroy(&sc->sc_rsel);
    766 	seldestroy(&sc->sc_wsel);
    767 
    768 	cv_destroy(&sc->sc_rchan);
    769 	cv_destroy(&sc->sc_wchan);
    770 	cv_destroy(&sc->sc_lchan);
    771 
    772 	return 0;
    773 }
    774 
    775 int
    776 au_portof(struct audio_softc *sc, char *name, int class)
    777 {
    778 	mixer_devinfo_t mi;
    779 
    780 	for(mi.index = 0;
    781 	    sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0;
    782 	    mi.index++)
    783 		if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
    784 			return mi.index;
    785 	return -1;
    786 }
    787 
    788 void
    789 au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
    790 	       mixer_devinfo_t *mi, const struct portname *tbl)
    791 {
    792 	int i, j;
    793 
    794 	ports->index = mi->index;
    795 	if (mi->type == AUDIO_MIXER_ENUM) {
    796 		ports->isenum = true;
    797 		for(i = 0; tbl[i].name; i++)
    798 		    for(j = 0; j < mi->un.e.num_mem; j++)
    799 			if (strcmp(mi->un.e.member[j].label.name,
    800 							    tbl[i].name) == 0) {
    801 				ports->allports |= tbl[i].mask;
    802 				ports->aumask[ports->nports] = tbl[i].mask;
    803 				ports->misel[ports->nports] =
    804 				    mi->un.e.member[j].ord;
    805 				ports->miport[ports->nports] =
    806 				    au_portof(sc, mi->un.e.member[j].label.name,
    807 				    mi->mixer_class);
    808 				if (ports->mixerout != -1 &&
    809 				    ports->miport[ports->nports] != -1)
    810 					ports->isdual = true;
    811 				++ports->nports;
    812 			}
    813 	} else if (mi->type == AUDIO_MIXER_SET) {
    814 		for(i = 0; tbl[i].name; i++)
    815 		    for(j = 0; j < mi->un.s.num_mem; j++)
    816 			if (strcmp(mi->un.s.member[j].label.name,
    817 							    tbl[i].name) == 0) {
    818 				ports->allports |= tbl[i].mask;
    819 				ports->aumask[ports->nports] = tbl[i].mask;
    820 				ports->misel[ports->nports] =
    821 				    mi->un.s.member[j].mask;
    822 				ports->miport[ports->nports] =
    823 				    au_portof(sc, mi->un.s.member[j].label.name,
    824 				    mi->mixer_class);
    825 				++ports->nports;
    826 			}
    827 	}
    828 }
    829 
    830 /*
    831  * Called from hardware driver.  This is where the MI audio driver gets
    832  * probed/attached to the hardware driver.
    833  */
    834 device_t
    835 audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev)
    836 {
    837 	struct audio_attach_args arg;
    838 
    839 #ifdef DIAGNOSTIC
    840 	if (ahwp == NULL) {
    841 		aprint_error("audio_attach_mi: NULL\n");
    842 		return 0;
    843 	}
    844 #endif
    845 	arg.type = AUDIODEV_TYPE_AUDIO;
    846 	arg.hwif = ahwp;
    847 	arg.hdl = hdlp;
    848 	return config_found(dev, &arg, audioprint);
    849 }
    850 
    851 #ifdef AUDIO_DEBUG
    852 void	audio_printsc(struct audio_softc *);
    853 void	audio_print_params(const char *, struct audio_params *);
    854 
    855 void
    856 audio_printsc(struct audio_softc *sc)
    857 {
    858 	printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if);
    859 	printf("open 0x%x mode 0x%x\n", sc->sc_open, sc->sc_mode);
    860 	printf("rchan 0x%x wchan 0x%x ", cv_has_waiters(&sc->sc_rchan),
    861 	    cv_has_waiters(&sc->sc_wchan));
    862 	printf("rring used 0x%x pring used=%d\n",
    863 	       audio_stream_get_used(&sc->sc_rr.s),
    864 	       audio_stream_get_used(&sc->sc_pr.s));
    865 	printf("rbus 0x%x pbus 0x%x ", sc->sc_rbus, sc->sc_pbus);
    866 	printf("blksize %d", sc->sc_pr.blksize);
    867 	printf("hiwat %d lowat %d\n", sc->sc_pr.usedhigh, sc->sc_pr.usedlow);
    868 }
    869 
    870 void
    871 audio_print_params(const char *s, struct audio_params *p)
    872 {
    873 	printf("%s enc=%u %uch %u/%ubit %uHz\n", s, p->encoding, p->channels,
    874 	       p->validbits, p->precision, p->sample_rate);
    875 }
    876 #endif
    877 
    878 int
    879 audio_alloc_ring(struct audio_softc *sc, struct audio_ringbuffer *r,
    880 		 int direction, size_t bufsize)
    881 {
    882 	const struct audio_hw_if *hw;
    883 	void *hdl;
    884 
    885 	hw = sc->hw_if;
    886 	hdl = sc->hw_hdl;
    887 	/*
    888 	 * Alloc DMA play and record buffers
    889 	 */
    890 	if (bufsize < AUMINBUF)
    891 		bufsize = AUMINBUF;
    892 	ROUNDSIZE(bufsize);
    893 	if (hw->round_buffersize) {
    894 		mutex_enter(sc->sc_lock);
    895 		bufsize = hw->round_buffersize(hdl, direction, bufsize);
    896  		mutex_exit(sc->sc_lock);
    897 	}
    898 	if (hw->allocm)
    899 		r->s.start = hw->allocm(hdl, direction, bufsize);
    900 	else
    901 		r->s.start = kmem_alloc(bufsize, KM_SLEEP);
    902 	if (r->s.start == 0)
    903 		return ENOMEM;
    904 	r->s.bufsize = bufsize;
    905 	return 0;
    906 }
    907 
    908 void
    909 audio_free_ring(struct audio_softc *sc, struct audio_ringbuffer *r)
    910 {
    911 	if (r->s.start == 0)
    912 		return;
    913 
    914 	if (sc->hw_if->freem)
    915 		sc->hw_if->freem(sc->hw_hdl, r->s.start, r->s.bufsize);
    916 	else
    917 		kmem_free(r->s.start, r->s.bufsize);
    918 	r->s.start = 0;
    919 }
    920 
    921 static int
    922 audio_setup_pfilters(struct audio_softc *sc, const audio_params_t *pp,
    923 		     stream_filter_list_t *pfilters)
    924 {
    925 	stream_filter_t *pf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS];
    926 	audio_stream_t ps[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS];
    927 	const audio_params_t *from_param;
    928 	audio_params_t *to_param;
    929 	int i, n, onfilters;
    930 
    931 	KASSERT(mutex_owned(sc->sc_lock));
    932 
    933 	/* Construct new filters. */
    934 	mutex_exit(sc->sc_lock);
    935 	memset(pf, 0, sizeof(pf));
    936 	memset(ps, 0, sizeof(ps));
    937 	from_param = pp;
    938 	for (i = 0; i < pfilters->req_size; i++) {
    939 		n = pfilters->req_size - i - 1;
    940 		to_param = &pfilters->filters[n].param;
    941 		audio_check_params(to_param);
    942 		pf[i] = pfilters->filters[n].factory(sc, from_param, to_param);
    943 		if (pf[i] == NULL)
    944 			break;
    945 		if (audio_stream_ctor(&ps[i], from_param, AU_RING_SIZE))
    946 			break;
    947 		if (i > 0)
    948 			pf[i]->set_fetcher(pf[i], &pf[i - 1]->base);
    949 		from_param = to_param;
    950 	}
    951 	if (i < pfilters->req_size) { /* failure */
    952 		DPRINTF(("%s: pfilters failure\n", __func__));
    953 		for (; i >= 0; i--) {
    954 			if (pf[i] != NULL)
    955 				pf[i]->dtor(pf[i]);
    956 			audio_stream_dtor(&ps[i]);
    957 		}
    958 		mutex_enter(sc->sc_lock);
    959 		return EINVAL;
    960 	}
    961 	mutex_enter(sc->sc_lock);
    962 
    963 	/* Swap in new filters. */
    964 	mutex_enter(sc->sc_intr_lock);
    965 	memcpy(of, sc->sc_pfilters, sizeof(of));
    966 	memcpy(os, sc->sc_pstreams, sizeof(os));
    967 	onfilters = sc->sc_npfilters;
    968 	memcpy(sc->sc_pfilters, pf, sizeof(pf));
    969 	memcpy(sc->sc_pstreams, ps, sizeof(ps));
    970 	sc->sc_npfilters = pfilters->req_size;
    971 	for (i = 0; i < pfilters->req_size; i++) {
    972 		pf[i]->set_inputbuffer(pf[i], &sc->sc_pstreams[i]);
    973 	}
    974 	/* hardware format and the buffer near to userland */
    975 	if (pfilters->req_size <= 0) {
    976 		sc->sc_pr.s.param = *pp;
    977 		sc->sc_pustream = &sc->sc_pr.s;
    978 	} else {
    979 		sc->sc_pr.s.param = pfilters->filters[0].param;
    980 		sc->sc_pustream = &sc->sc_pstreams[0];
    981 	}
    982 	mutex_exit(sc->sc_intr_lock);
    983 
    984 	/* Destroy old filters. */
    985 	mutex_exit(sc->sc_lock);
    986 	for (i = 0; i < onfilters; i++) {
    987 		of[i]->dtor(of[i]);
    988 		audio_stream_dtor(&os[i]);
    989 	}
    990 	mutex_enter(sc->sc_lock);
    991 
    992 #ifdef AUDIO_DEBUG
    993 	printf("%s: HW-buffer=%p pustream=%p\n",
    994 	       __func__, &sc->sc_pr.s, sc->sc_pustream);
    995 	for (i = 0; i < pfilters->req_size; i++) {
    996 		char num[100];
    997 		snprintf(num, 100, "[%d]", i);
    998 		audio_print_params(num, &sc->sc_pstreams[i].param);
    999 	}
   1000 	audio_print_params("[HW]", &sc->sc_pr.s.param);
   1001 #endif /* AUDIO_DEBUG */
   1002 
   1003 	return 0;
   1004 }
   1005 
   1006 static int
   1007 audio_setup_rfilters(struct audio_softc *sc, const audio_params_t *rp,
   1008 		     stream_filter_list_t *rfilters)
   1009 {
   1010 	stream_filter_t *rf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS];
   1011 	audio_stream_t rs[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS];
   1012 	const audio_params_t *to_param;
   1013 	audio_params_t *from_param;
   1014 	int i, onfilters;
   1015 
   1016 	KASSERT(mutex_owned(sc->sc_lock));
   1017 
   1018 	/* Construct new filters. */
   1019 	mutex_exit(sc->sc_lock);
   1020 	memset(rf, 0, sizeof(rf));
   1021 	memset(rs, 0, sizeof(rs));
   1022 	for (i = 0; i < rfilters->req_size; i++) {
   1023 		from_param = &rfilters->filters[i].param;
   1024 		audio_check_params(from_param);
   1025 		to_param = i + 1 < rfilters->req_size
   1026 			? &rfilters->filters[i + 1].param : rp;
   1027 		rf[i] = rfilters->filters[i].factory(sc, from_param, to_param);
   1028 		if (rf[i] == NULL)
   1029 			break;
   1030 		if (audio_stream_ctor(&rs[i], to_param, AU_RING_SIZE))
   1031 			break;
   1032 		if (i > 0) {
   1033 			rf[i]->set_fetcher(rf[i], &rf[i - 1]->base);
   1034 		} else {
   1035 			/* rf[0] has no previous fetcher because
   1036 			 * the audio hardware fills data to the
   1037 			 * input buffer. */
   1038 			rf[0]->set_inputbuffer(rf[0], &sc->sc_rr.s);
   1039 		}
   1040 	}
   1041 	if (i < rfilters->req_size) { /* failure */
   1042 		DPRINTF(("%s: rfilters failure\n", __func__));
   1043 		for (; i >= 0; i--) {
   1044 			if (rf[i] != NULL)
   1045 				rf[i]->dtor(rf[i]);
   1046 			audio_stream_dtor(&rs[i]);
   1047 		}
   1048 		mutex_enter(sc->sc_lock);
   1049 		return EINVAL;
   1050 	}
   1051 	mutex_enter(sc->sc_lock);
   1052 
   1053 	/* Swap in new filters. */
   1054 	mutex_enter(sc->sc_intr_lock);
   1055 	memcpy(of, sc->sc_rfilters, sizeof(of));
   1056 	memcpy(os, sc->sc_rstreams, sizeof(os));
   1057 	onfilters = sc->sc_nrfilters;
   1058 	memcpy(sc->sc_rfilters, rf, sizeof(rf));
   1059 	memcpy(sc->sc_rstreams, rs, sizeof(rs));
   1060 	sc->sc_nrfilters = rfilters->req_size;
   1061 	for (i = 1; i < rfilters->req_size; i++) {
   1062 		rf[i]->set_inputbuffer(rf[i], &sc->sc_rstreams[i - 1]);
   1063 	}
   1064 	/* hardware format and the buffer near to userland */
   1065 	if (rfilters->req_size <= 0) {
   1066 		sc->sc_rr.s.param = *rp;
   1067 		sc->sc_rustream = &sc->sc_rr.s;
   1068 	} else {
   1069 		sc->sc_rr.s.param = rfilters->filters[0].param;
   1070 		sc->sc_rustream = &sc->sc_rstreams[rfilters->req_size - 1];
   1071 	}
   1072 	mutex_exit(sc->sc_intr_lock);
   1073 
   1074 #ifdef AUDIO_DEBUG
   1075 	printf("%s: HW-buffer=%p pustream=%p\n",
   1076 	       __func__, &sc->sc_rr.s, sc->sc_rustream);
   1077 	audio_print_params("[HW]", &sc->sc_rr.s.param);
   1078 	for (i = 0; i < rfilters->req_size; i++) {
   1079 		char num[100];
   1080 		snprintf(num, 100, "[%d]", i);
   1081 		audio_print_params(num, &sc->sc_rstreams[i].param);
   1082 	}
   1083 #endif /* AUDIO_DEBUG */
   1084 
   1085 	/* Destroy old filters. */
   1086 	mutex_exit(sc->sc_lock);
   1087 	for (i = 0; i < onfilters; i++) {
   1088 		of[i]->dtor(of[i]);
   1089 		audio_stream_dtor(&os[i]);
   1090 	}
   1091 	mutex_enter(sc->sc_lock);
   1092 
   1093 	return 0;
   1094 }
   1095 
   1096 static void
   1097 audio_stream_dtor(audio_stream_t *stream)
   1098 {
   1099 
   1100 	if (stream->start != NULL)
   1101 		kmem_free(stream->start, stream->bufsize);
   1102 	memset(stream, 0, sizeof(audio_stream_t));
   1103 }
   1104 
   1105 static int
   1106 audio_stream_ctor(audio_stream_t *stream, const audio_params_t *param, int size)
   1107 {
   1108 	int frame_size;
   1109 
   1110 	size = min(size, AU_RING_SIZE);
   1111 	stream->bufsize = size;
   1112 	stream->start = kmem_alloc(size, KM_SLEEP);
   1113 	if (stream->start == NULL)
   1114 		return ENOMEM;
   1115 	frame_size = (param->precision + 7) / 8 * param->channels;
   1116 	size = (size / frame_size) * frame_size;
   1117 	stream->end = stream->start + size;
   1118 	stream->inp = stream->start;
   1119 	stream->outp = stream->start;
   1120 	stream->used = 0;
   1121 	stream->param = *param;
   1122 	stream->loop = false;
   1123 	return 0;
   1124 }
   1125 
   1126 static void
   1127 stream_filter_list_append(stream_filter_list_t *list,
   1128 			  stream_filter_factory_t factory,
   1129 			  const audio_params_t *param)
   1130 {
   1131 
   1132 	if (list->req_size >= AUDIO_MAX_FILTERS) {
   1133 		printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n",
   1134 		       __func__);
   1135 		return;
   1136 	}
   1137 	list->filters[list->req_size].factory = factory;
   1138 	list->filters[list->req_size].param = *param;
   1139 	list->req_size++;
   1140 }
   1141 
   1142 static void
   1143 stream_filter_list_set(stream_filter_list_t *list, int i,
   1144 		       stream_filter_factory_t factory,
   1145 		       const audio_params_t *param)
   1146 {
   1147 
   1148 	if (i < 0 || i >= AUDIO_MAX_FILTERS) {
   1149 		printf("%s: invalid index: %d\n", __func__, i);
   1150 		return;
   1151 	}
   1152 
   1153 	list->filters[i].factory = factory;
   1154 	list->filters[i].param = *param;
   1155 	if (list->req_size <= i)
   1156 		list->req_size = i + 1;
   1157 }
   1158 
   1159 static void
   1160 stream_filter_list_prepend(stream_filter_list_t *list,
   1161 			   stream_filter_factory_t factory,
   1162 			   const audio_params_t *param)
   1163 {
   1164 
   1165 	if (list->req_size >= AUDIO_MAX_FILTERS) {
   1166 		printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n",
   1167 		       __func__);
   1168 		return;
   1169 	}
   1170 	memmove(&list->filters[1], &list->filters[0],
   1171 		sizeof(struct stream_filter_req) * list->req_size);
   1172 	list->filters[0].factory = factory;
   1173 	list->filters[0].param = *param;
   1174 	list->req_size++;
   1175 }
   1176 
   1177 /*
   1178  * Look up audio device and acquire locks for device access.
   1179  */
   1180 static int
   1181 audio_enter(dev_t dev, krw_t rw, struct audio_softc **scp)
   1182 {
   1183 	struct audio_softc *sc;
   1184 
   1185 	/* First, find the device and take sc_lock. */
   1186 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1187 	if (sc == NULL)
   1188 		return ENXIO;
   1189 	mutex_enter(sc->sc_lock);
   1190 	if (sc->sc_dying) {
   1191 		mutex_exit(sc->sc_lock);
   1192 		return EIO;
   1193 	}
   1194 
   1195 	/* Acquire device access lock. */
   1196 	switch (rw) {
   1197 	case RW_WRITER:
   1198 		while (__predict_false(sc->sc_dvlock != 0)) {
   1199 			cv_wait(&sc->sc_lchan, sc->sc_lock);
   1200 		}
   1201 		sc->sc_dvlock = -1;
   1202 		break;
   1203 	case RW_READER:
   1204 		while (__predict_false(sc->sc_dvlock < 0)) {
   1205 			cv_wait(&sc->sc_lchan, sc->sc_lock);
   1206 		}
   1207 		sc->sc_dvlock++;
   1208 		break;
   1209 	default:
   1210 		panic("audio_enter");
   1211 	}
   1212 
   1213 	*scp = sc;
   1214 	return 0;
   1215 }
   1216 
   1217 /*
   1218  * Release reference to device acquired with audio_enter().
   1219  */
   1220 static void
   1221 audio_exit(struct audio_softc *sc)
   1222 {
   1223 
   1224 	KASSERT(mutex_owned(sc->sc_lock));
   1225 	KASSERT(sc->sc_dvlock != 0);
   1226 
   1227 	/* Release device level lock. */
   1228 	if (__predict_false(sc->sc_dvlock < 0)) {
   1229 		sc->sc_dvlock = 0;
   1230 	} else {
   1231 		sc->sc_dvlock--;
   1232 	}
   1233 	cv_broadcast(&sc->sc_lchan);
   1234 	mutex_exit(sc->sc_lock);
   1235 }
   1236 
   1237 /*
   1238  * Wait for I/O to complete, releasing device lock.
   1239  */
   1240 static int
   1241 audio_waitio(struct audio_softc *sc, kcondvar_t *chan)
   1242 {
   1243 	int error;
   1244 	krw_t rw;
   1245 
   1246 	KASSERT(mutex_owned(sc->sc_lock));
   1247 
   1248 	/* Release device level lock while sleeping. */
   1249 	if (__predict_false(sc->sc_dvlock < 0)) {
   1250 		sc->sc_dvlock = 0;
   1251 		rw = RW_WRITER;
   1252 	} else {
   1253 		KASSERT(sc->sc_dvlock > 0);
   1254 		sc->sc_dvlock--;
   1255 		rw = RW_READER;
   1256 	}
   1257 	cv_broadcast(&sc->sc_lchan);
   1258 
   1259 	/* Wait for pending I/O to complete. */
   1260 	error = cv_wait_sig(chan, sc->sc_lock);
   1261 
   1262 	/* Re-acquire device level lock. */
   1263 	if (__predict_false(rw == RW_WRITER)) {
   1264 		while (__predict_false(sc->sc_dvlock != 0)) {
   1265 			cv_wait(&sc->sc_lchan, sc->sc_lock);
   1266 		}
   1267 		sc->sc_dvlock = -1;
   1268 	} else {
   1269 		while (__predict_false(sc->sc_dvlock < 0)) {
   1270 			cv_wait(&sc->sc_lchan, sc->sc_lock);
   1271 		}
   1272 		sc->sc_dvlock++;
   1273 	}
   1274 
   1275 	return error;
   1276 }
   1277 
   1278 int
   1279 audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1280 {
   1281 	struct audio_softc *sc;
   1282 	int error;
   1283 
   1284 	if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0)
   1285 		return error;
   1286 	device_active(sc->dev, DVA_SYSTEM);
   1287 	switch (AUDIODEV(dev)) {
   1288 	case SOUND_DEVICE:
   1289 	case AUDIO_DEVICE:
   1290 		error = audio_open(dev, sc, flags, ifmt, l);
   1291 		break;
   1292 	case AUDIOCTL_DEVICE:
   1293 		error = 0;
   1294 		break;
   1295 	case MIXER_DEVICE:
   1296 		error = mixer_open(dev, sc, flags, ifmt, l);
   1297 		break;
   1298 	default:
   1299 		error = ENXIO;
   1300 		break;
   1301 	}
   1302 	audio_exit(sc);
   1303 
   1304 	return error;
   1305 }
   1306 
   1307 int
   1308 audioclose(dev_t dev, int flags, int ifmt, struct lwp *l)
   1309 {
   1310 	struct audio_softc *sc;
   1311 	int error;
   1312 
   1313 	if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0)
   1314 		return error;
   1315 	device_active(sc->dev, DVA_SYSTEM);
   1316 	switch (AUDIODEV(dev)) {
   1317 	case SOUND_DEVICE:
   1318 	case AUDIO_DEVICE:
   1319 		error = audio_close(sc, flags, ifmt, l);
   1320 		break;
   1321 	case MIXER_DEVICE:
   1322 		error = mixer_close(sc, flags, ifmt, l);
   1323 		break;
   1324 	case AUDIOCTL_DEVICE:
   1325 		error = 0;
   1326 		break;
   1327 	default:
   1328 		error = ENXIO;
   1329 		break;
   1330 	}
   1331 	audio_exit(sc);
   1332 
   1333 	return error;
   1334 }
   1335 
   1336 int
   1337 audioread(dev_t dev, struct uio *uio, int ioflag)
   1338 {
   1339 	struct audio_softc *sc;
   1340 	int error;
   1341 
   1342 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1343 		return error;
   1344 	switch (AUDIODEV(dev)) {
   1345 	case SOUND_DEVICE:
   1346 	case AUDIO_DEVICE:
   1347 		error = audio_read(sc, uio, ioflag);
   1348 		break;
   1349 	case AUDIOCTL_DEVICE:
   1350 	case MIXER_DEVICE:
   1351 		error = ENODEV;
   1352 		break;
   1353 	default:
   1354 		error = ENXIO;
   1355 		break;
   1356 	}
   1357 	audio_exit(sc);
   1358 
   1359 	return error;
   1360 }
   1361 
   1362 int
   1363 audiowrite(dev_t dev, struct uio *uio, int ioflag)
   1364 {
   1365 	struct audio_softc *sc;
   1366 	int error;
   1367 
   1368 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1369 		return error;
   1370 	switch (AUDIODEV(dev)) {
   1371 	case SOUND_DEVICE:
   1372 	case AUDIO_DEVICE:
   1373 		error = audio_write(sc, uio, ioflag);
   1374 		break;
   1375 	case AUDIOCTL_DEVICE:
   1376 	case MIXER_DEVICE:
   1377 		error = ENODEV;
   1378 		break;
   1379 	default:
   1380 		error = ENXIO;
   1381 		break;
   1382 	}
   1383 	audio_exit(sc);
   1384 
   1385 	return error;
   1386 }
   1387 
   1388 int
   1389 audioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1390 {
   1391 	struct audio_softc *sc;
   1392 	int error;
   1393 	krw_t rw;
   1394 
   1395 	/* Figure out which lock type we need. */
   1396 	switch (cmd) {
   1397 	case AUDIO_FLUSH:
   1398 	case AUDIO_SETINFO:
   1399 	case AUDIO_DRAIN:
   1400 	case AUDIO_SETFD:
   1401 		rw = RW_WRITER;
   1402 		break;
   1403 	default:
   1404 		rw = RW_READER;
   1405 		break;
   1406 	}
   1407 
   1408 	if ((error = audio_enter(dev, rw, &sc)) != 0)
   1409 		return error;
   1410 	switch (AUDIODEV(dev)) {
   1411 	case SOUND_DEVICE:
   1412 	case AUDIO_DEVICE:
   1413 	case AUDIOCTL_DEVICE:
   1414 		device_active(sc->dev, DVA_SYSTEM);
   1415 		if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ))
   1416 			error = mixer_ioctl(sc, cmd, addr, flag, l);
   1417 		else
   1418 			error = audio_ioctl(sc, cmd, addr, flag, l);
   1419 		break;
   1420 	case MIXER_DEVICE:
   1421 		error = mixer_ioctl(sc, cmd, addr, flag, l);
   1422 		break;
   1423 	default:
   1424 		error = ENXIO;
   1425 		break;
   1426 	}
   1427 	audio_exit(sc);
   1428 
   1429 	return error;
   1430 }
   1431 
   1432 int
   1433 audiopoll(dev_t dev, int events, struct lwp *l)
   1434 {
   1435 	struct audio_softc *sc;
   1436 	int revents;
   1437 
   1438 	/* Don't bother with device level lock here. */
   1439 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1440 	if (sc == NULL)
   1441 		return ENXIO;
   1442 	mutex_enter(sc->sc_lock);
   1443 	if (sc->sc_dying) {
   1444 		mutex_exit(sc->sc_lock);
   1445 		return EIO;
   1446 	}
   1447 	switch (AUDIODEV(dev)) {
   1448 	case SOUND_DEVICE:
   1449 	case AUDIO_DEVICE:
   1450 		revents = audio_poll(sc, events, l);
   1451 		break;
   1452 	case AUDIOCTL_DEVICE:
   1453 	case MIXER_DEVICE:
   1454 		revents = 0;
   1455 		break;
   1456 	default:
   1457 		revents = POLLERR;
   1458 		break;
   1459 	}
   1460 	mutex_exit(sc->sc_lock);
   1461 
   1462 	return revents;
   1463 }
   1464 
   1465 int
   1466 audiokqfilter(dev_t dev, struct knote *kn)
   1467 {
   1468 	struct audio_softc *sc;
   1469 	int rv;
   1470 
   1471 	/* Don't bother with device level lock here. */
   1472 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1473 	if (sc == NULL)
   1474 		return ENXIO;
   1475 	mutex_enter(sc->sc_lock);
   1476 	if (sc->sc_dying) {
   1477 		mutex_exit(sc->sc_lock);
   1478 		return EIO;
   1479 	}
   1480 	switch (AUDIODEV(dev)) {
   1481 	case SOUND_DEVICE:
   1482 	case AUDIO_DEVICE:
   1483 		rv = audio_kqfilter(sc, kn);
   1484 		break;
   1485 	case AUDIOCTL_DEVICE:
   1486 	case MIXER_DEVICE:
   1487 		rv = 1;
   1488 		break;
   1489 	default:
   1490 		rv = 1;
   1491 	}
   1492 	mutex_exit(sc->sc_lock);
   1493 
   1494 	return rv;
   1495 }
   1496 
   1497 paddr_t
   1498 audiommap(dev_t dev, off_t off, int prot)
   1499 {
   1500 	struct audio_softc *sc;
   1501 	paddr_t error;
   1502 
   1503 	/*
   1504 	 * Acquire a reader lock.  audio_mmap() will drop sc_lock
   1505 	 * in order to allow the device's mmap routine to sleep.
   1506 	 * Although not yet possible, we want to prevent memory
   1507 	 * from being allocated or freed out from under us.
   1508 	 */
   1509 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1510 		return 1;
   1511 	device_active(sc->dev, DVA_SYSTEM); /* XXXJDM */
   1512 	switch (AUDIODEV(dev)) {
   1513 	case SOUND_DEVICE:
   1514 	case AUDIO_DEVICE:
   1515 		error = audio_mmap(sc, off, prot);
   1516 		break;
   1517 	case AUDIOCTL_DEVICE:
   1518 	case MIXER_DEVICE:
   1519 		error = -1;
   1520 		break;
   1521 	default:
   1522 		error = -1;
   1523 		break;
   1524 	}
   1525 	audio_exit(sc);
   1526 	return error;
   1527 }
   1528 
   1529 /*
   1530  * Audio driver
   1531  */
   1532 void
   1533 audio_init_ringbuffer(struct audio_softc *sc, struct audio_ringbuffer *rp,
   1534 		      int mode)
   1535 {
   1536 	int nblks;
   1537 	int blksize;
   1538 
   1539 	blksize = rp->blksize;
   1540 	if (blksize < AUMINBLK)
   1541 		blksize = AUMINBLK;
   1542 	if (blksize > rp->s.bufsize / AUMINNOBLK)
   1543 		blksize = rp->s.bufsize / AUMINNOBLK;
   1544 	ROUNDSIZE(blksize);
   1545 	DPRINTF(("audio_init_ringbuffer: MI blksize=%d\n", blksize));
   1546 	if (sc->hw_if->round_blocksize)
   1547 		blksize = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
   1548 						     mode, &rp->s.param);
   1549 	if (blksize <= 0)
   1550 		panic("audio_init_ringbuffer: blksize");
   1551 	nblks = rp->s.bufsize / blksize;
   1552 
   1553 	DPRINTF(("audio_init_ringbuffer: final blksize=%d\n", blksize));
   1554 	rp->blksize = blksize;
   1555 	rp->maxblks = nblks;
   1556 	rp->s.end = rp->s.start + nblks * blksize;
   1557 	rp->s.outp = rp->s.inp = rp->s.start;
   1558 	rp->s.used = 0;
   1559 	rp->stamp = 0;
   1560 	rp->stamp_last = 0;
   1561 	rp->fstamp = 0;
   1562 	rp->drops = 0;
   1563 	rp->copying = false;
   1564 	rp->needfill = false;
   1565 	rp->mmapped = false;
   1566 }
   1567 
   1568 int
   1569 audio_initbufs(struct audio_softc *sc)
   1570 {
   1571 	const struct audio_hw_if *hw;
   1572 	int error;
   1573 
   1574 	DPRINTF(("audio_initbufs: mode=0x%x\n", sc->sc_mode));
   1575 	hw = sc->hw_if;
   1576 	if (audio_can_capture(sc)) {
   1577 		audio_init_ringbuffer(sc, &sc->sc_rr, AUMODE_RECORD);
   1578 		if (hw->init_input && (sc->sc_mode & AUMODE_RECORD)) {
   1579 			error = hw->init_input(sc->hw_hdl, sc->sc_rr.s.start,
   1580 				       sc->sc_rr.s.end - sc->sc_rr.s.start);
   1581 			if (error)
   1582 				return error;
   1583 		}
   1584 	}
   1585 
   1586 	if (audio_can_playback(sc)) {
   1587 		audio_init_ringbuffer(sc, &sc->sc_pr, AUMODE_PLAY);
   1588 		sc->sc_sil_count = 0;
   1589 		if (hw->init_output && (sc->sc_mode & AUMODE_PLAY)) {
   1590 			error = hw->init_output(sc->hw_hdl, sc->sc_pr.s.start,
   1591 					sc->sc_pr.s.end - sc->sc_pr.s.start);
   1592 			if (error)
   1593 				return error;
   1594 		}
   1595 	}
   1596 
   1597 #ifdef AUDIO_INTR_TIME
   1598 #define double u_long
   1599 	if (audio_can_playback(sc)) {
   1600 		sc->sc_pnintr = 0;
   1601 		sc->sc_pblktime = (u_long)(
   1602 		    (double)sc->sc_pr.blksize * 100000 /
   1603 		    (double)(sc->sc_pparams.precision / NBBY *
   1604 			     sc->sc_pparams.channels *
   1605 			     sc->sc_pparams.sample_rate)) * 10;
   1606 		DPRINTF(("audio: play blktime = %lu for %d\n",
   1607 			 sc->sc_pblktime, sc->sc_pr.blksize));
   1608 	}
   1609 	if (audio_can_capture(sc)) {
   1610 		sc->sc_rnintr = 0;
   1611 		sc->sc_rblktime = (u_long)(
   1612 		    (double)sc->sc_rr.blksize * 100000 /
   1613 		    (double)(sc->sc_rparams.precision / NBBY *
   1614 			     sc->sc_rparams.channels *
   1615 			     sc->sc_rparams.sample_rate)) * 10;
   1616 		DPRINTF(("audio: record blktime = %lu for %d\n",
   1617 			 sc->sc_rblktime, sc->sc_rr.blksize));
   1618 	}
   1619 #undef double
   1620 #endif
   1621 
   1622 	return 0;
   1623 }
   1624 
   1625 void
   1626 audio_calcwater(struct audio_softc *sc)
   1627 {
   1628 
   1629 	/* set high at 100% */
   1630 	if (audio_can_playback(sc)) {
   1631 		sc->sc_pr.usedhigh =
   1632 		    sc->sc_pustream->end - sc->sc_pustream->start;
   1633 		/* set low at 75% of usedhigh */
   1634 		sc->sc_pr.usedlow = sc->sc_pr.usedhigh * 3 / 4;
   1635 		if (sc->sc_pr.usedlow == sc->sc_pr.usedhigh)
   1636 			sc->sc_pr.usedlow -= sc->sc_pr.blksize;
   1637 	}
   1638 
   1639 	if (audio_can_capture(sc)) {
   1640 		sc->sc_rr.usedhigh =
   1641 		    sc->sc_rustream->end - sc->sc_rustream->start -
   1642 		    sc->sc_rr.blksize;
   1643 		sc->sc_rr.usedlow = 0;
   1644 		DPRINTF(("%s: plow=%d phigh=%d rlow=%d rhigh=%d\n", __func__,
   1645 			 sc->sc_pr.usedlow, sc->sc_pr.usedhigh,
   1646 			 sc->sc_rr.usedlow, sc->sc_rr.usedhigh));
   1647 	}
   1648 }
   1649 
   1650 int
   1651 audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   1652     struct lwp *l)
   1653 {
   1654 	int error;
   1655 	u_int mode;
   1656 	const struct audio_hw_if *hw;
   1657 
   1658 	KASSERT(mutex_owned(sc->sc_lock));
   1659 
   1660 	hw = sc->hw_if;
   1661 	if (hw == NULL)
   1662 		return ENXIO;
   1663 
   1664 	DPRINTF(("audio_open: flags=0x%x sc=%p hdl=%p\n",
   1665 		 flags, sc, sc->hw_hdl));
   1666 
   1667 	if (((flags & FREAD) && (sc->sc_open & AUOPEN_READ)) ||
   1668 	    ((flags & FWRITE) && (sc->sc_open & AUOPEN_WRITE)))
   1669 		return EBUSY;
   1670 
   1671 	if (hw->open != NULL) {
   1672 		mutex_enter(sc->sc_intr_lock);
   1673 		error = hw->open(sc->hw_hdl, flags);
   1674 		mutex_exit(sc->sc_intr_lock);
   1675 		if (error)
   1676 			return error;
   1677 	}
   1678 
   1679 	sc->sc_async_audio = 0;
   1680 	sc->sc_sil_count = 0;
   1681 	sc->sc_rbus = false;
   1682 	sc->sc_pbus = false;
   1683 	sc->sc_eof = 0;
   1684 	sc->sc_playdrop = 0;
   1685 
   1686 	mutex_enter(sc->sc_intr_lock);
   1687 	sc->sc_full_duplex =
   1688 		(flags & (FWRITE|FREAD)) == (FWRITE|FREAD) &&
   1689 		(audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX);
   1690 	mutex_exit(sc->sc_intr_lock);
   1691 
   1692 	mode = 0;
   1693 	if (flags & FREAD) {
   1694 		sc->sc_open |= AUOPEN_READ;
   1695 		mode |= AUMODE_RECORD;
   1696 	}
   1697 	if (flags & FWRITE) {
   1698 		sc->sc_open |= AUOPEN_WRITE;
   1699 		mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
   1700 	}
   1701 
   1702 	/*
   1703 	 * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear)
   1704 	 * The /dev/audio is always (re)set to 8-bit MU-Law mono
   1705 	 * For the other devices, you get what they were last set to.
   1706 	 */
   1707 	if (ISDEVAUDIO(dev)) {
   1708 		error = audio_set_defaults(sc, mode);
   1709 	} else {
   1710 		struct audio_info ai;
   1711 
   1712 		AUDIO_INITINFO(&ai);
   1713 		ai.mode = mode;
   1714 		error = audiosetinfo(sc, &ai);
   1715 	}
   1716 	if (error)
   1717 		goto bad;
   1718 
   1719 #ifdef DIAGNOSTIC
   1720 	/*
   1721 	 * Sample rate and precision are supposed to be set to proper
   1722 	 * default values by the hardware driver, so that it may give
   1723 	 * us these values.
   1724 	 */
   1725 	if (sc->sc_rparams.precision == 0 || sc->sc_pparams.precision == 0) {
   1726 		printf("audio_open: 0 precision\n");
   1727 		return EINVAL;
   1728 	}
   1729 #endif
   1730 
   1731 	/* audio_close() decreases sc_pr.usedlow, recalculate here */
   1732 	audio_calcwater(sc);
   1733 
   1734 	DPRINTF(("audio_open: done sc_mode = 0x%x\n", sc->sc_mode));
   1735 
   1736 	return 0;
   1737 
   1738 bad:
   1739 	mutex_enter(sc->sc_intr_lock);
   1740 	if (hw->close != NULL)
   1741 		hw->close(sc->hw_hdl);
   1742 	sc->sc_open = 0;
   1743 	sc->sc_mode = 0;
   1744 	mutex_exit(sc->sc_intr_lock);
   1745 	sc->sc_full_duplex = 0;
   1746 	return error;
   1747 }
   1748 
   1749 /*
   1750  * Must be called from task context.
   1751  */
   1752 void
   1753 audio_init_record(struct audio_softc *sc)
   1754 {
   1755 
   1756 	KASSERT(mutex_owned(sc->sc_lock));
   1757 
   1758 	mutex_enter(sc->sc_intr_lock);
   1759 	if (sc->hw_if->speaker_ctl &&
   1760 	    (!sc->sc_full_duplex || (sc->sc_mode & AUMODE_PLAY) == 0))
   1761 		sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF);
   1762 	mutex_exit(sc->sc_intr_lock);
   1763 }
   1764 
   1765 /*
   1766  * Must be called from task context.
   1767  */
   1768 void
   1769 audio_init_play(struct audio_softc *sc)
   1770 {
   1771 
   1772 	KASSERT(mutex_owned(sc->sc_lock));
   1773 
   1774 	mutex_enter(sc->sc_intr_lock);
   1775 	sc->sc_wstamp = sc->sc_pr.stamp;
   1776 	if (sc->hw_if->speaker_ctl)
   1777 		sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON);
   1778 	mutex_exit(sc->sc_intr_lock);
   1779 }
   1780 
   1781 int
   1782 audio_drain(struct audio_softc *sc)
   1783 {
   1784 	struct audio_ringbuffer *cb;
   1785 	int error, drops;
   1786 	int i, used;
   1787 
   1788 	KASSERT(mutex_owned(sc->sc_lock));
   1789 	KASSERT(mutex_owned(sc->sc_intr_lock));
   1790 
   1791 	DPRINTF(("audio_drain: enter busy=%d\n", sc->sc_pbus));
   1792 	cb = &sc->sc_pr;
   1793 	if (cb->mmapped)
   1794 		return 0;
   1795 
   1796 	used = audio_stream_get_used(&sc->sc_pr.s);
   1797 	for (i = 0; i < sc->sc_npfilters; i++)
   1798 		used += audio_stream_get_used(&sc->sc_pstreams[i]);
   1799 	if (used <= 0)
   1800 		return 0;
   1801 
   1802 	if (!sc->sc_pbus) {
   1803 		/* We've never started playing, probably because the
   1804 		 * block was too short.  Pad it and start now.
   1805 		 */
   1806 		int cc;
   1807 		uint8_t *inp = cb->s.inp;
   1808 
   1809 		cc = cb->blksize - (inp - cb->s.start) % cb->blksize;
   1810 		audio_fill_silence(&cb->s.param, inp, cc);
   1811 		cb->s.inp = audio_stream_add_inp(&cb->s, inp, cc);
   1812 		error = audiostartp(sc);
   1813 		if (error)
   1814 			return error;
   1815 	}
   1816 	/*
   1817 	 * Play until a silence block has been played, then we
   1818 	 * know all has been drained.
   1819 	 * XXX This should be done some other way to avoid
   1820 	 * playing silence.
   1821 	 */
   1822 #ifdef DIAGNOSTIC
   1823 	if (cb->copying) {
   1824 		printf("audio_drain: copying in progress!?!\n");
   1825 		cb->copying = false;
   1826 	}
   1827 #endif
   1828 	drops = cb->drops;
   1829 	error = 0;
   1830 	while (cb->drops == drops && !error) {
   1831 		DPRINTF(("audio_drain: used=%d, drops=%ld\n",
   1832 			 audio_stream_get_used(&sc->sc_pr.s), cb->drops));
   1833 		mutex_exit(sc->sc_intr_lock);
   1834 		error = audio_waitio(sc, &sc->sc_wchan);
   1835 		mutex_enter(sc->sc_intr_lock);
   1836 		if (sc->sc_dying)
   1837 			error = EIO;
   1838 	}
   1839 	return error;
   1840 }
   1841 
   1842 /*
   1843  * Close an audio chip.
   1844  */
   1845 /* ARGSUSED */
   1846 int
   1847 audio_close(struct audio_softc *sc, int flags, int ifmt, struct lwp *l)
   1848 {
   1849 	const struct audio_hw_if *hw;
   1850 
   1851 	KASSERT(mutex_owned(sc->sc_lock));
   1852 
   1853 	DPRINTF(("audio_close: sc=%p\n", sc));
   1854 	hw = sc->hw_if;
   1855 	mutex_enter(sc->sc_intr_lock);
   1856 	/* Stop recording. */
   1857 	if ((flags & FREAD) && sc->sc_rbus) {
   1858 		/*
   1859 		 * XXX Some drivers (e.g. SB) use the same routine
   1860 		 * to halt input and output so don't halt input if
   1861 		 * in full duplex mode.  These drivers should be fixed.
   1862 		 */
   1863 		if (!sc->sc_full_duplex || hw->halt_input != hw->halt_output)
   1864 			hw->halt_input(sc->hw_hdl);
   1865 		sc->sc_rbus = false;
   1866 	}
   1867 	/*
   1868 	 * Block until output drains, but allow ^C interrupt.
   1869 	 */
   1870 	sc->sc_pr.usedlow = sc->sc_pr.blksize;	/* avoid excessive wakeups */
   1871 	/*
   1872 	 * If there is pending output, let it drain (unless
   1873 	 * the output is paused).
   1874 	 */
   1875 	if ((flags & FWRITE) && sc->sc_pbus) {
   1876 		if (!sc->sc_pr.pause && !audio_drain(sc) && hw->drain)
   1877 			(void)hw->drain(sc->hw_hdl);
   1878 		hw->halt_output(sc->hw_hdl);
   1879 		sc->sc_pbus = false;
   1880 	}
   1881 	if (hw->close != NULL)
   1882 		hw->close(sc->hw_hdl);
   1883 	sc->sc_open = 0;
   1884 	sc->sc_mode = 0;
   1885 	sc->sc_full_duplex = 0;
   1886 	mutex_exit(sc->sc_intr_lock);
   1887 	sc->sc_async_audio = 0;
   1888 
   1889 	return 0;
   1890 }
   1891 
   1892 int
   1893 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag)
   1894 {
   1895 	struct audio_ringbuffer *cb;
   1896 	const uint8_t *outp;
   1897 	uint8_t *inp;
   1898 	int error, used, cc, n;
   1899 
   1900 	KASSERT(mutex_owned(sc->sc_lock));
   1901 
   1902 	cb = &sc->sc_rr;
   1903 	if (cb->mmapped)
   1904 		return EINVAL;
   1905 
   1906 	DPRINTFN(1,("audio_read: cc=%zu mode=%d\n",
   1907 		    uio->uio_resid, sc->sc_mode));
   1908 
   1909 #ifdef AUDIO_PM_IDLE
   1910 	if (device_is_active(&sc->dev) || sc->sc_idle)
   1911 		device_active(&sc->dev, DVA_SYSTEM);
   1912 #endif
   1913 
   1914 	error = 0;
   1915 	/*
   1916 	 * If hardware is half-duplex and currently playing, return
   1917 	 * silence blocks based on the number of blocks we have output.
   1918 	 */
   1919 	if (!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) {
   1920 		while (uio->uio_resid > 0 && !error) {
   1921 			for(;;) {
   1922 				/*
   1923 				 * No need to lock, as any wakeup will be
   1924 				 * held for us while holding sc_lock.
   1925 				 */
   1926 				cc = sc->sc_pr.stamp - sc->sc_wstamp;
   1927 				if (cc > 0)
   1928 					break;
   1929 				DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n",
   1930 					 sc->sc_pr.stamp, sc->sc_wstamp));
   1931 				if (ioflag & IO_NDELAY)
   1932 					return EWOULDBLOCK;
   1933 				error = audio_waitio(sc, &sc->sc_rchan);
   1934 				if (sc->sc_dying)
   1935 					error = EIO;
   1936 				if (error)
   1937 					return error;
   1938 			}
   1939 
   1940 			if (uio->uio_resid < cc)
   1941 				cc = uio->uio_resid;
   1942 			DPRINTFN(1,("audio_read: reading in write mode, "
   1943 				    "cc=%d\n", cc));
   1944 			error = audio_silence_copyout(sc, cc, uio);
   1945 			sc->sc_wstamp += cc;
   1946 		}
   1947 		return error;
   1948 	}
   1949 
   1950 	mutex_enter(sc->sc_intr_lock);
   1951 	while (uio->uio_resid > 0 && !error) {
   1952 		while ((used = audio_stream_get_used(sc->sc_rustream)) <= 0) {
   1953 			if (!sc->sc_rbus && !sc->sc_rr.pause)
   1954 				error = audiostartr(sc);
   1955 			mutex_exit(sc->sc_intr_lock);
   1956 			if (error)
   1957 				return error;
   1958 			if (ioflag & IO_NDELAY)
   1959 				return EWOULDBLOCK;
   1960 			DPRINTFN(2, ("audio_read: sleep used=%d\n", used));
   1961 			error = audio_waitio(sc, &sc->sc_rchan);
   1962 			if (sc->sc_dying)
   1963 				error = EIO;
   1964 			if (error)
   1965 				return error;
   1966 			mutex_enter(sc->sc_intr_lock);
   1967 		}
   1968 
   1969 		outp = sc->sc_rustream->outp;
   1970 		inp = sc->sc_rustream->inp;
   1971 		cb->copying = true;
   1972 
   1973 		/*
   1974 		 * cc is the amount of data in the sc_rustream excluding
   1975 		 * wrapped data.  Note the tricky case of inp == outp, which
   1976 		 * must mean the buffer is full, not empty, because used > 0.
   1977 		 */
   1978 		cc = outp < inp ? inp - outp :sc->sc_rustream->end - outp;
   1979 		DPRINTFN(1,("audio_read: outp=%p, cc=%d\n", outp, cc));
   1980 
   1981 		n = uio->uio_resid;
   1982 		mutex_exit(sc->sc_intr_lock);
   1983 		mutex_exit(sc->sc_lock);
   1984 		error = uiomove(__UNCONST(outp), cc, uio);
   1985 		mutex_enter(sc->sc_lock);
   1986 		mutex_enter(sc->sc_intr_lock);
   1987 		n -= uio->uio_resid; /* number of bytes actually moved */
   1988 
   1989 		sc->sc_rustream->outp = audio_stream_add_outp
   1990 			(sc->sc_rustream, outp, n);
   1991 		cb->copying = false;
   1992 	}
   1993 	mutex_exit(sc->sc_intr_lock);
   1994 	return error;
   1995 }
   1996 
   1997 void
   1998 audio_clear(struct audio_softc *sc)
   1999 {
   2000 
   2001 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2002 
   2003 	if (sc->sc_rbus) {
   2004 		cv_broadcast(&sc->sc_rchan);
   2005 		sc->hw_if->halt_input(sc->hw_hdl);
   2006 		sc->sc_rbus = false;
   2007 		sc->sc_rr.pause = false;
   2008 	}
   2009 	if (sc->sc_pbus) {
   2010 		cv_broadcast(&sc->sc_wchan);
   2011 		sc->hw_if->halt_output(sc->hw_hdl);
   2012 		sc->sc_pbus = false;
   2013 		sc->sc_pr.pause = false;
   2014 	}
   2015 }
   2016 
   2017 void
   2018 audio_clear_intr_unlocked(struct audio_softc *sc)
   2019 {
   2020 
   2021 	mutex_enter(sc->sc_intr_lock);
   2022 	audio_clear(sc);
   2023 	mutex_exit(sc->sc_intr_lock);
   2024 }
   2025 
   2026 void
   2027 audio_calc_blksize(struct audio_softc *sc, int mode)
   2028 {
   2029 	const audio_params_t *parm;
   2030 	struct audio_ringbuffer *rb;
   2031 
   2032 	if (sc->sc_blkset)
   2033 		return;
   2034 
   2035 	if (mode == AUMODE_PLAY) {
   2036 		rb = &sc->sc_pr;
   2037 		parm = &rb->s.param;
   2038 	} else {
   2039 		rb = &sc->sc_rr;
   2040 		parm = &rb->s.param;
   2041 	}
   2042 
   2043 	rb->blksize = parm->sample_rate * audio_blk_ms / 1000 *
   2044 	     parm->channels * parm->precision / NBBY;
   2045 
   2046 	DPRINTF(("audio_calc_blksize: %s blksize=%d\n",
   2047 		 mode == AUMODE_PLAY ? "play" : "record", rb->blksize));
   2048 }
   2049 
   2050 void
   2051 audio_fill_silence(struct audio_params *params, uint8_t *p, int n)
   2052 {
   2053 	uint8_t auzero0, auzero1;
   2054 	int nfill;
   2055 
   2056 	auzero1 = 0;		/* initialize to please gcc */
   2057 	nfill = 1;
   2058 	switch (params->encoding) {
   2059 	case AUDIO_ENCODING_ULAW:
   2060 		auzero0 = 0x7f;
   2061 		break;
   2062 	case AUDIO_ENCODING_ALAW:
   2063 		auzero0 = 0x55;
   2064 		break;
   2065 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   2066 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   2067 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   2068 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   2069 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   2070 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   2071 	case AUDIO_ENCODING_AC3:
   2072 	case AUDIO_ENCODING_ADPCM: /* is this right XXX */
   2073 	case AUDIO_ENCODING_SLINEAR_LE:
   2074 	case AUDIO_ENCODING_SLINEAR_BE:
   2075 		auzero0 = 0;/* fortunately this works for any number of bits */
   2076 		break;
   2077 	case AUDIO_ENCODING_ULINEAR_LE:
   2078 	case AUDIO_ENCODING_ULINEAR_BE:
   2079 		if (params->precision > 8) {
   2080 			nfill = (params->precision + NBBY - 1)/ NBBY;
   2081 			auzero0 = 0x80;
   2082 			auzero1 = 0;
   2083 		} else
   2084 			auzero0 = 0x80;
   2085 		break;
   2086 	default:
   2087 		DPRINTF(("audio: bad encoding %d\n", params->encoding));
   2088 		auzero0 = 0;
   2089 		break;
   2090 	}
   2091 	if (nfill == 1) {
   2092 		while (--n >= 0)
   2093 			*p++ = auzero0; /* XXX memset */
   2094 	} else /* nfill must no longer be 2 */ {
   2095 		if (params->encoding == AUDIO_ENCODING_ULINEAR_LE) {
   2096 			int k = nfill;
   2097 			while (--k > 0)
   2098 				*p++ = auzero1;
   2099 			n -= nfill - 1;
   2100 		}
   2101 		while (n >= nfill) {
   2102 			int k = nfill;
   2103 			*p++ = auzero0;
   2104 			while (--k > 0)
   2105 				*p++ = auzero1;
   2106 
   2107 			n -= nfill;
   2108 		}
   2109 		if (n-- > 0)	/* XXX must be 1 - DIAGNOSTIC check? */
   2110 			*p++ = auzero0;
   2111 	}
   2112 }
   2113 
   2114 int
   2115 audio_silence_copyout(struct audio_softc *sc, int n, struct uio *uio)
   2116 {
   2117 	uint8_t zerobuf[128];
   2118 	int error;
   2119 	int k;
   2120 
   2121 	audio_fill_silence(&sc->sc_rparams, zerobuf, sizeof zerobuf);
   2122 
   2123 	error = 0;
   2124 	while (n > 0 && uio->uio_resid > 0 && !error) {
   2125 		k = min(n, min(uio->uio_resid, sizeof zerobuf));
   2126 		mutex_exit(sc->sc_lock);
   2127 		error = uiomove(zerobuf, k, uio);
   2128 		mutex_enter(sc->sc_lock);
   2129 		n -= k;
   2130 	}
   2131 
   2132 	return error;
   2133 }
   2134 
   2135 static int
   2136 uio_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
   2137     audio_stream_t *p, int max_used)
   2138 {
   2139 	uio_fetcher_t *this;
   2140 	int size;
   2141 	int stream_space;
   2142 	int error;
   2143 
   2144 	KASSERT(mutex_owned(sc->sc_lock));
   2145 	KASSERT(!cpu_intr_p());
   2146 	KASSERT(!cpu_softintr_p());
   2147 
   2148 	this = (uio_fetcher_t *)self;
   2149 	this->last_used = audio_stream_get_used(p);
   2150 	if (this->last_used >= this->usedhigh)
   2151 		return 0;
   2152 	/*
   2153 	 * uio_fetcher ignores max_used and move the data as
   2154 	 * much as possible in order to return the correct value
   2155 	 * for audio_prinfo::seek and kfilters.
   2156 	 */
   2157 	stream_space = audio_stream_get_space(p);
   2158 	size = min(this->uio->uio_resid, stream_space);
   2159 
   2160 	/* the first fragment of the space */
   2161 	stream_space = p->end - p->inp;
   2162 	if (stream_space >= size) {
   2163 		mutex_exit(sc->sc_lock);
   2164 		error = uiomove(p->inp, size, this->uio);
   2165 		mutex_enter(sc->sc_lock);
   2166 		if (error)
   2167 			return error;
   2168 		p->inp = audio_stream_add_inp(p, p->inp, size);
   2169 	} else {
   2170 		mutex_exit(sc->sc_lock);
   2171 		error = uiomove(p->inp, stream_space, this->uio);
   2172 		mutex_enter(sc->sc_lock);
   2173 		if (error)
   2174 			return error;
   2175 		p->inp = audio_stream_add_inp(p, p->inp, stream_space);
   2176 		mutex_exit(sc->sc_lock);
   2177 		error = uiomove(p->start, size - stream_space, this->uio);
   2178 		mutex_enter(sc->sc_lock);
   2179 		if (error)
   2180 			return error;
   2181 		p->inp = audio_stream_add_inp(p, p->inp, size - stream_space);
   2182 	}
   2183 	this->last_used = audio_stream_get_used(p);
   2184 	return 0;
   2185 }
   2186 
   2187 static int
   2188 null_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
   2189     audio_stream_t *p, int max_used)
   2190 {
   2191 
   2192 	return 0;
   2193 }
   2194 
   2195 static void
   2196 uio_fetcher_ctor(uio_fetcher_t *this, struct uio *u, int h)
   2197 {
   2198 
   2199 	this->base.fetch_to = uio_fetcher_fetch_to;
   2200 	this->uio = u;
   2201 	this->usedhigh = h;
   2202 }
   2203 
   2204 int
   2205 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag)
   2206 {
   2207 	uio_fetcher_t ufetcher;
   2208 	audio_stream_t stream;
   2209 	struct audio_ringbuffer *cb;
   2210 	stream_fetcher_t *fetcher;
   2211 	stream_filter_t *filter;
   2212 	uint8_t *inp, *einp;
   2213 	int saveerror, error, n, cc, used;
   2214 
   2215 	KASSERT(mutex_owned(sc->sc_lock));
   2216 
   2217 	DPRINTFN(2,("audio_write: sc=%p count=%zu used=%d(hi=%d)\n",
   2218 		    sc, uio->uio_resid, audio_stream_get_used(sc->sc_pustream),
   2219 		    sc->sc_pr.usedhigh));
   2220 	cb = &sc->sc_pr;
   2221 	if (cb->mmapped)
   2222 		return EINVAL;
   2223 
   2224 	if (uio->uio_resid == 0) {
   2225 		sc->sc_eof++;
   2226 		return 0;
   2227 	}
   2228 
   2229 #ifdef AUDIO_PM_IDLE
   2230 	if (device_is_active(&sc->dev) || sc->sc_idle)
   2231 		device_active(&sc->dev, DVA_SYSTEM);
   2232 #endif
   2233 
   2234 	/*
   2235 	 * If half-duplex and currently recording, throw away data.
   2236 	 */
   2237 	if (!sc->sc_full_duplex &&
   2238 	    (sc->sc_mode & AUMODE_RECORD)) {
   2239 		uio->uio_offset += uio->uio_resid;
   2240 		uio->uio_resid = 0;
   2241 		DPRINTF(("audio_write: half-dpx read busy\n"));
   2242 		return 0;
   2243 	}
   2244 
   2245 	if (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) {
   2246 		n = min(sc->sc_playdrop, uio->uio_resid);
   2247 		DPRINTF(("audio_write: playdrop %d\n", n));
   2248 		uio->uio_offset += n;
   2249 		uio->uio_resid -= n;
   2250 		sc->sc_playdrop -= n;
   2251 		if (uio->uio_resid == 0)
   2252 			return 0;
   2253 	}
   2254 
   2255 	/**
   2256 	 * setup filter pipeline
   2257 	 */
   2258 	uio_fetcher_ctor(&ufetcher, uio, cb->usedhigh);
   2259 	if (sc->sc_npfilters > 0) {
   2260 		fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base;
   2261 	} else {
   2262 		fetcher = &ufetcher.base;
   2263 	}
   2264 
   2265 	error = 0;
   2266 	mutex_enter(sc->sc_intr_lock);
   2267 	while (uio->uio_resid > 0 && !error) {
   2268 		/* wait if the first buffer is occupied */
   2269 		while ((used = audio_stream_get_used(sc->sc_pustream))
   2270 		    >= cb->usedhigh) {
   2271 			DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d "
   2272 				     "hiwat=%d\n", used,
   2273 				     cb->usedlow, cb->usedhigh));
   2274 			mutex_exit(sc->sc_intr_lock);
   2275 			if (ioflag & IO_NDELAY)
   2276 				return EWOULDBLOCK;
   2277 			error = audio_waitio(sc, &sc->sc_wchan);
   2278 			if (sc->sc_dying)
   2279 				error = EIO;
   2280 			if (error)
   2281 				return error;
   2282 			mutex_enter(sc->sc_intr_lock);
   2283 		}
   2284 		inp = cb->s.inp;
   2285 		cb->copying = true;
   2286 		stream = cb->s;
   2287 		used = stream.used;
   2288 
   2289 		/* Write to the sc_pustream as much as possible. */
   2290 		mutex_exit(sc->sc_intr_lock);
   2291 		if (sc->sc_npfilters > 0) {
   2292 			filter = sc->sc_pfilters[0];
   2293 			filter->set_fetcher(filter, &ufetcher.base);
   2294 			fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base;
   2295 			cc = cb->blksize * 2;
   2296 			error = fetcher->fetch_to(sc, fetcher, &stream, cc);
   2297 			if (error != 0) {
   2298 				fetcher = &ufetcher.base;
   2299 				cc = sc->sc_pustream->end - sc->sc_pustream->start;
   2300 				error = fetcher->fetch_to(sc, fetcher,
   2301 				    sc->sc_pustream, cc);
   2302 			}
   2303 		} else {
   2304 			fetcher = &ufetcher.base;
   2305 			cc = stream.end - stream.start;
   2306 			error = fetcher->fetch_to(sc, fetcher, &stream, cc);
   2307 		}
   2308 		mutex_enter(sc->sc_intr_lock);
   2309 		if (sc->sc_npfilters > 0) {
   2310 			cb->fstamp += ufetcher.last_used
   2311 			    - audio_stream_get_used(sc->sc_pustream);
   2312 		}
   2313 		cb->s.used += stream.used - used;
   2314 		cb->s.inp = stream.inp;
   2315 		einp = cb->s.inp;
   2316 
   2317 		/*
   2318 		 * This is a very suboptimal way of keeping track of
   2319 		 * silence in the buffer, but it is simple.
   2320 		 */
   2321 		sc->sc_sil_count = 0;
   2322 
   2323 		/*
   2324 		 * If the interrupt routine wants the last block filled AND
   2325 		 * the copy did not fill the last block completely it needs to
   2326 		 * be padded.
   2327 		 */
   2328 		if (cb->needfill && inp < einp &&
   2329 		    (inp  - cb->s.start) / cb->blksize ==
   2330 		    (einp - cb->s.start) / cb->blksize) {
   2331 			/* Figure out how many bytes to a block boundary. */
   2332 			cc = cb->blksize - (einp - cb->s.start) % cb->blksize;
   2333 			DPRINTF(("audio_write: partial fill %d\n", cc));
   2334 		} else
   2335 			cc = 0;
   2336 		cb->needfill = false;
   2337 		cb->copying = false;
   2338 		if (!sc->sc_pbus && !cb->pause) {
   2339 			saveerror = error;
   2340 			error = audiostartp(sc);
   2341 			if (saveerror != 0) {
   2342 				/* Report the first error that occurred. */
   2343 				error = saveerror;
   2344 			}
   2345 		}
   2346 		if (cc != 0) {
   2347 			DPRINTFN(1, ("audio_write: fill %d\n", cc));
   2348 			audio_fill_silence(&cb->s.param, einp, cc);
   2349 		}
   2350 	}
   2351 	mutex_exit(sc->sc_intr_lock);
   2352 
   2353 	return error;
   2354 }
   2355 
   2356 int
   2357 audio_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
   2358 	    struct lwp *l)
   2359 {
   2360 	const struct audio_hw_if *hw;
   2361 	struct audio_offset *ao;
   2362 	u_long stamp;
   2363 	int error, offs, fd;
   2364 	bool rbus, pbus;
   2365 
   2366 	KASSERT(mutex_owned(sc->sc_lock));
   2367 
   2368 	DPRINTF(("audio_ioctl(%lu,'%c',%lu)\n",
   2369 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
   2370 	hw = sc->hw_if;
   2371 	error = 0;
   2372 	switch (cmd) {
   2373 	case FIONBIO:
   2374 		/* All handled in the upper FS layer. */
   2375 		break;
   2376 
   2377 	case FIONREAD:
   2378 		*(int *)addr = audio_stream_get_used(sc->sc_rustream);
   2379 		break;
   2380 
   2381 	case FIOASYNC:
   2382 		if (*(int *)addr) {
   2383 			if (sc->sc_async_audio != 0)
   2384 				error = EBUSY;
   2385 			else
   2386 				sc->sc_async_audio = curproc->p_pid;
   2387 			DPRINTF(("audio_ioctl: FIOASYNC pid %d\n",
   2388 			    curproc->p_pid));
   2389 		} else
   2390 			sc->sc_async_audio = 0;
   2391 		break;
   2392 
   2393 	case AUDIO_FLUSH:
   2394 		DPRINTF(("AUDIO_FLUSH\n"));
   2395 		rbus = sc->sc_rbus;
   2396 		pbus = sc->sc_pbus;
   2397 		mutex_enter(sc->sc_intr_lock);
   2398 		audio_clear(sc);
   2399 		error = audio_initbufs(sc);
   2400 		if (error) {
   2401 			mutex_exit(sc->sc_intr_lock);
   2402 			return error;
   2403 		}
   2404 		if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_pbus && pbus)
   2405 			error = audiostartp(sc);
   2406 		if (!error &&
   2407 		    (sc->sc_mode & AUMODE_RECORD) && !sc->sc_rbus && rbus)
   2408 			error = audiostartr(sc);
   2409 		mutex_exit(sc->sc_intr_lock);
   2410 		break;
   2411 
   2412 	/*
   2413 	 * Number of read (write) samples dropped.  We don't know where or
   2414 	 * when they were dropped.
   2415 	 */
   2416 	case AUDIO_RERROR:
   2417 		*(int *)addr = sc->sc_rr.drops;
   2418 		break;
   2419 
   2420 	case AUDIO_PERROR:
   2421 		*(int *)addr = sc->sc_pr.drops;
   2422 		break;
   2423 
   2424 	/*
   2425 	 * Offsets into buffer.
   2426 	 */
   2427 	case AUDIO_GETIOFFS:
   2428 		ao = (struct audio_offset *)addr;
   2429 		mutex_enter(sc->sc_intr_lock);
   2430 		/* figure out where next DMA will start */
   2431 		stamp = sc->sc_rustream == &sc->sc_rr.s
   2432 			? sc->sc_rr.stamp : sc->sc_rr.fstamp;
   2433 		offs = sc->sc_rustream->inp - sc->sc_rustream->start;
   2434 		mutex_exit(sc->sc_intr_lock);
   2435 		ao->samples = stamp;
   2436 		ao->deltablks =
   2437 		  (stamp / sc->sc_rr.blksize) -
   2438 		  (sc->sc_rr.stamp_last / sc->sc_rr.blksize);
   2439 		sc->sc_rr.stamp_last = stamp;
   2440 		ao->offset = offs;
   2441 		break;
   2442 
   2443 	case AUDIO_GETOOFFS:
   2444 		ao = (struct audio_offset *)addr;
   2445 		mutex_enter(sc->sc_intr_lock);
   2446 		/* figure out where next DMA will start */
   2447 		stamp = sc->sc_pustream == &sc->sc_pr.s
   2448 			? sc->sc_pr.stamp : sc->sc_pr.fstamp;
   2449 		offs = sc->sc_pustream->outp - sc->sc_pustream->start
   2450 			+ sc->sc_pr.blksize;
   2451 		mutex_exit(sc->sc_intr_lock);
   2452 		ao->samples = stamp;
   2453 		ao->deltablks =
   2454 		  (stamp / sc->sc_pr.blksize) -
   2455 		  (sc->sc_pr.stamp_last / sc->sc_pr.blksize);
   2456 		sc->sc_pr.stamp_last = stamp;
   2457 		if (sc->sc_pustream->start + offs >= sc->sc_pustream->end)
   2458 			offs = 0;
   2459 		ao->offset = offs;
   2460 		break;
   2461 
   2462 	/*
   2463 	 * How many bytes will elapse until mike hears the first
   2464 	 * sample of what we write next?
   2465 	 */
   2466 	case AUDIO_WSEEK:
   2467 		*(u_long *)addr = audio_stream_get_used(sc->sc_pustream);
   2468 		break;
   2469 
   2470 	case AUDIO_SETINFO:
   2471 		DPRINTF(("AUDIO_SETINFO mode=0x%x\n", sc->sc_mode));
   2472 		error = audiosetinfo(sc, (struct audio_info *)addr);
   2473 		break;
   2474 
   2475 	case AUDIO_GETINFO:
   2476 		DPRINTF(("AUDIO_GETINFO\n"));
   2477 		error = audiogetinfo(sc, (struct audio_info *)addr, 0);
   2478 		break;
   2479 
   2480 	case AUDIO_GETBUFINFO:
   2481 		DPRINTF(("AUDIO_GETBUFINFO\n"));
   2482 		error = audiogetinfo(sc, (struct audio_info *)addr, 1);
   2483 		break;
   2484 
   2485 	case AUDIO_DRAIN:
   2486 		DPRINTF(("AUDIO_DRAIN\n"));
   2487 		mutex_enter(sc->sc_intr_lock);
   2488 		error = audio_drain(sc);
   2489 		if (!error && hw->drain)
   2490 		    error = hw->drain(sc->hw_hdl);
   2491 		mutex_exit(sc->sc_intr_lock);
   2492 		break;
   2493 
   2494 	case AUDIO_GETDEV:
   2495 		DPRINTF(("AUDIO_GETDEV\n"));
   2496 		error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
   2497 		break;
   2498 
   2499 	case AUDIO_GETENC:
   2500 		DPRINTF(("AUDIO_GETENC\n"));
   2501 		error = hw->query_encoding(sc->hw_hdl,
   2502 		    (struct audio_encoding *)addr);
   2503 		break;
   2504 
   2505 	case AUDIO_GETFD:
   2506 		DPRINTF(("AUDIO_GETFD\n"));
   2507 		*(int *)addr = sc->sc_full_duplex;
   2508 		break;
   2509 
   2510 	case AUDIO_SETFD:
   2511 		DPRINTF(("AUDIO_SETFD\n"));
   2512 		fd = *(int *)addr;
   2513 		if (audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX) {
   2514 			if (hw->setfd)
   2515 				error = hw->setfd(sc->hw_hdl, fd);
   2516 			else
   2517 				error = 0;
   2518 			if (!error)
   2519 				sc->sc_full_duplex = fd;
   2520 		} else {
   2521 			if (fd)
   2522 				error = ENOTTY;
   2523 			else
   2524 				error = 0;
   2525 		}
   2526 		break;
   2527 
   2528 	case AUDIO_GETPROPS:
   2529 		DPRINTF(("AUDIO_GETPROPS\n"));
   2530 		*(int *)addr = audio_get_props(sc);
   2531 		break;
   2532 
   2533 	default:
   2534 		if (hw->dev_ioctl) {
   2535 			error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l);
   2536 		} else {
   2537 			DPRINTF(("audio_ioctl: unknown ioctl\n"));
   2538 			error = EINVAL;
   2539 		}
   2540 		break;
   2541 	}
   2542 	DPRINTF(("audio_ioctl(%lu,'%c',%lu) result %d\n",
   2543 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
   2544 	return error;
   2545 }
   2546 
   2547 int
   2548 audio_poll(struct audio_softc *sc, int events, struct lwp *l)
   2549 {
   2550 	int revents;
   2551 	int used;
   2552 
   2553 	KASSERT(mutex_owned(sc->sc_lock));
   2554 
   2555 	DPRINTF(("audio_poll: events=0x%x mode=%d\n", events, sc->sc_mode));
   2556 
   2557 	revents = 0;
   2558 	mutex_enter(sc->sc_intr_lock);
   2559 	if (events & (POLLIN | POLLRDNORM)) {
   2560 		used = audio_stream_get_used(sc->sc_rustream);
   2561 		/*
   2562 		 * If half duplex and playing, audio_read() will generate
   2563 		 * silence at the play rate; poll for silence being
   2564 		 * available.  Otherwise, poll for recorded sound.
   2565 		 */
   2566 		if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY)) ?
   2567 		    sc->sc_pr.stamp > sc->sc_wstamp :
   2568 		    used > sc->sc_rr.usedlow)
   2569 			revents |= events & (POLLIN | POLLRDNORM);
   2570 	}
   2571 
   2572 	if (events & (POLLOUT | POLLWRNORM)) {
   2573 		used = audio_stream_get_used(sc->sc_pustream);
   2574 		/*
   2575 		 * If half duplex and recording, audio_write() will throw
   2576 		 * away play data, which means we are always ready to write.
   2577 		 * Otherwise, poll for play buffer being below its low water
   2578 		 * mark.
   2579 		 */
   2580 		if ((!sc->sc_full_duplex && (sc->sc_mode & AUMODE_RECORD)) ||
   2581 		    (!(sc->sc_mode & AUMODE_PLAY_ALL) && sc->sc_playdrop > 0) ||
   2582 		    (used <= sc->sc_pr.usedlow))
   2583 			revents |= events & (POLLOUT | POLLWRNORM);
   2584 	}
   2585 	mutex_exit(sc->sc_intr_lock);
   2586 
   2587 	if (revents == 0) {
   2588 		if (events & (POLLIN | POLLRDNORM))
   2589 			selrecord(l, &sc->sc_rsel);
   2590 
   2591 		if (events & (POLLOUT | POLLWRNORM))
   2592 			selrecord(l, &sc->sc_wsel);
   2593 	}
   2594 
   2595 	return revents;
   2596 }
   2597 
   2598 static void
   2599 filt_audiordetach(struct knote *kn)
   2600 {
   2601 	struct audio_softc *sc;
   2602 
   2603 	sc = kn->kn_hook;
   2604 	mutex_enter(sc->sc_intr_lock);
   2605 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
   2606 	mutex_exit(sc->sc_intr_lock);
   2607 }
   2608 
   2609 static int
   2610 filt_audioread(struct knote *kn, long hint)
   2611 {
   2612 	struct audio_softc *sc;
   2613 
   2614 	sc = kn->kn_hook;
   2615 	mutex_enter(sc->sc_intr_lock);
   2616 	if (!sc->sc_full_duplex && (sc->sc_mode & AUMODE_PLAY))
   2617 		kn->kn_data = sc->sc_pr.stamp - sc->sc_wstamp;
   2618 	else
   2619 		kn->kn_data = audio_stream_get_used(sc->sc_rustream)
   2620 			- sc->sc_rr.usedlow;
   2621 	mutex_exit(sc->sc_intr_lock);
   2622 
   2623 	return kn->kn_data > 0;
   2624 }
   2625 
   2626 static const struct filterops audioread_filtops =
   2627 	{ 1, NULL, filt_audiordetach, filt_audioread };
   2628 
   2629 static void
   2630 filt_audiowdetach(struct knote *kn)
   2631 {
   2632 	struct audio_softc *sc;
   2633 
   2634 	sc = kn->kn_hook;
   2635 	mutex_enter(sc->sc_intr_lock);
   2636 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
   2637 	mutex_exit(sc->sc_intr_lock);
   2638 }
   2639 
   2640 static int
   2641 filt_audiowrite(struct knote *kn, long hint)
   2642 {
   2643 	struct audio_softc *sc;
   2644 	audio_stream_t *stream;
   2645 
   2646 	sc = kn->kn_hook;
   2647 	mutex_enter(sc->sc_intr_lock);
   2648 	stream = sc->sc_pustream;
   2649 	kn->kn_data = (stream->end - stream->start)
   2650 		- audio_stream_get_used(stream);
   2651 	mutex_exit(sc->sc_intr_lock);
   2652 
   2653 	return kn->kn_data > 0;
   2654 }
   2655 
   2656 static const struct filterops audiowrite_filtops =
   2657 	{ 1, NULL, filt_audiowdetach, filt_audiowrite };
   2658 
   2659 int
   2660 audio_kqfilter(struct audio_softc *sc, struct knote *kn)
   2661 {
   2662 	struct klist *klist;
   2663 
   2664 	switch (kn->kn_filter) {
   2665 	case EVFILT_READ:
   2666 		klist = &sc->sc_rsel.sel_klist;
   2667 		kn->kn_fop = &audioread_filtops;
   2668 		break;
   2669 
   2670 	case EVFILT_WRITE:
   2671 		klist = &sc->sc_wsel.sel_klist;
   2672 		kn->kn_fop = &audiowrite_filtops;
   2673 		break;
   2674 
   2675 	default:
   2676 		return EINVAL;
   2677 	}
   2678 
   2679 	kn->kn_hook = sc;
   2680 
   2681 	mutex_enter(sc->sc_intr_lock);
   2682 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   2683 	mutex_exit(sc->sc_intr_lock);
   2684 
   2685 	return 0;
   2686 }
   2687 
   2688 paddr_t
   2689 audio_mmap(struct audio_softc *sc, off_t off, int prot)
   2690 {
   2691 	const struct audio_hw_if *hw;
   2692 	struct audio_ringbuffer *cb;
   2693 	paddr_t rv;
   2694 
   2695 	KASSERT(mutex_owned(sc->sc_lock));
   2696 	KASSERT(sc->sc_dvlock > 0);
   2697 
   2698 	DPRINTF(("audio_mmap: off=%lld, prot=%d\n", (long long)off, prot));
   2699 	hw = sc->hw_if;
   2700 	if (!(audio_get_props(sc) & AUDIO_PROP_MMAP) || !hw->mappage)
   2701 		return -1;
   2702 #if 0
   2703 /* XXX
   2704  * The idea here was to use the protection to determine if
   2705  * we are mapping the read or write buffer, but it fails.
   2706  * The VM system is broken in (at least) two ways.
   2707  * 1) If you map memory VM_PROT_WRITE you SIGSEGV
   2708  *    when writing to it, so VM_PROT_READ|VM_PROT_WRITE
   2709  *    has to be used for mmapping the play buffer.
   2710  * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
   2711  *    audio_mmap will get called at some point with VM_PROT_READ
   2712  *    only.
   2713  * So, alas, we always map the play buffer for now.
   2714  */
   2715 	if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
   2716 	    prot == VM_PROT_WRITE)
   2717 		cb = &sc->sc_pr;
   2718 	else if (prot == VM_PROT_READ)
   2719 		cb = &sc->sc_rr;
   2720 	else
   2721 		return -1;
   2722 #else
   2723 	cb = &sc->sc_pr;
   2724 #endif
   2725 
   2726 	if ((u_int)off >= cb->s.bufsize)
   2727 		return -1;
   2728 	if (!cb->mmapped) {
   2729 		cb->mmapped = true;
   2730 		if (cb == &sc->sc_pr) {
   2731 			audio_fill_silence(&cb->s.param, cb->s.start,
   2732 					   cb->s.bufsize);
   2733 			mutex_enter(sc->sc_intr_lock);
   2734 			sc->sc_pustream = &cb->s;
   2735 			if (!sc->sc_pbus && !sc->sc_pr.pause)
   2736 				(void)audiostartp(sc);
   2737 			mutex_exit(sc->sc_intr_lock);
   2738 		} else {
   2739 			mutex_enter(sc->sc_intr_lock);
   2740 			sc->sc_rustream = &cb->s;
   2741 			if (!sc->sc_rbus && !sc->sc_rr.pause)
   2742 				(void)audiostartr(sc);
   2743 			mutex_exit(sc->sc_intr_lock);
   2744 		}
   2745 	}
   2746 
   2747 	mutex_exit(sc->sc_lock);
   2748 	rv = hw->mappage(sc->hw_hdl, cb->s.start, off, prot);
   2749 	mutex_enter(sc->sc_lock);
   2750 
   2751 	return rv;
   2752 }
   2753 
   2754 int
   2755 audiostartr(struct audio_softc *sc)
   2756 {
   2757 	int error;
   2758 
   2759 	KASSERT(mutex_owned(sc->sc_lock));
   2760 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2761 
   2762 	DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n",
   2763 		 sc->sc_rr.s.start, audio_stream_get_used(&sc->sc_rr.s),
   2764 		 sc->sc_rr.usedhigh, sc->sc_rr.mmapped));
   2765 
   2766 	if (!audio_can_capture(sc))
   2767 		return EINVAL;
   2768 
   2769 	if (sc->hw_if->trigger_input)
   2770 		error = sc->hw_if->trigger_input(sc->hw_hdl, sc->sc_rr.s.start,
   2771 		    sc->sc_rr.s.end, sc->sc_rr.blksize,
   2772 		    audio_rint, (void *)sc, &sc->sc_rr.s.param);
   2773 	else
   2774 		error = sc->hw_if->start_input(sc->hw_hdl, sc->sc_rr.s.start,
   2775 		    sc->sc_rr.blksize, audio_rint, (void *)sc);
   2776 	if (error) {
   2777 		DPRINTF(("audiostartr failed: %d\n", error));
   2778 		return error;
   2779 	}
   2780 	sc->sc_rbus = true;
   2781 	return 0;
   2782 }
   2783 
   2784 int
   2785 audiostartp(struct audio_softc *sc)
   2786 {
   2787 	int error;
   2788 	int used;
   2789 
   2790 	KASSERT(mutex_owned(sc->sc_lock));
   2791 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2792 
   2793 	used = audio_stream_get_used(&sc->sc_pr.s);
   2794 	DPRINTF(("audiostartp: start=%p used=%d(hi=%d blk=%d) mmapped=%d\n",
   2795 		 sc->sc_pr.s.start, used, sc->sc_pr.usedhigh,
   2796 		 sc->sc_pr.blksize, sc->sc_pr.mmapped));
   2797 
   2798 	if (!audio_can_playback(sc))
   2799 		return EINVAL;
   2800 
   2801 	if (!sc->sc_pr.mmapped && used < sc->sc_pr.blksize) {
   2802 		cv_broadcast(&sc->sc_wchan);
   2803 		DPRINTF(("%s: wakeup and return\n", __func__));
   2804 		return 0;
   2805 	}
   2806 
   2807 	if (sc->hw_if->trigger_output) {
   2808 		DPRINTF(("%s: call trigger_output\n", __func__));
   2809 		error = sc->hw_if->trigger_output(sc->hw_hdl, sc->sc_pr.s.start,
   2810 		    sc->sc_pr.s.end, sc->sc_pr.blksize,
   2811 		    audio_pint, (void *)sc, &sc->sc_pr.s.param);
   2812 	} else {
   2813 		DPRINTF(("%s: call start_output\n", __func__));
   2814 		error = sc->hw_if->start_output(sc->hw_hdl,
   2815 		    __UNCONST(sc->sc_pr.s.outp), sc->sc_pr.blksize,
   2816 		    audio_pint, (void *)sc);
   2817 	}
   2818 	if (error) {
   2819 		DPRINTF(("audiostartp failed: %d\n", error));
   2820 		return error;
   2821 	}
   2822 	sc->sc_pbus = true;
   2823 	return 0;
   2824 }
   2825 
   2826 /*
   2827  * When the play interrupt routine finds that the write isn't keeping
   2828  * the buffer filled it will insert silence in the buffer to make up
   2829  * for this.  The part of the buffer that is filled with silence
   2830  * is kept track of in a very approximate way: it starts at sc_sil_start
   2831  * and extends sc_sil_count bytes.  If there is already silence in
   2832  * the requested area nothing is done; so when the whole buffer is
   2833  * silent nothing happens.  When the writer starts again sc_sil_count
   2834  * is set to 0.
   2835  *
   2836  * XXX
   2837  * Putting silence into the output buffer should not really be done
   2838  * from the device interrupt handler.  Consider deferring to the soft
   2839  * interrupt.
   2840  */
   2841 static inline void
   2842 audio_pint_silence(struct audio_softc *sc, struct audio_ringbuffer *cb,
   2843 		   uint8_t *inp, int cc)
   2844 {
   2845 	uint8_t *s, *e, *p, *q;
   2846 
   2847 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2848 
   2849 	if (sc->sc_sil_count > 0) {
   2850 		s = sc->sc_sil_start; /* start of silence */
   2851 		e = s + sc->sc_sil_count; /* end of sil., may be beyond end */
   2852 		p = inp;	/* adjusted pointer to area to fill */
   2853 		if (p < s)
   2854 			p += cb->s.end - cb->s.start;
   2855 		q = p + cc;
   2856 		/* Check if there is already silence. */
   2857 		if (!(s <= p && p <  e &&
   2858 		      s <= q && q <= e)) {
   2859 			if (s <= p)
   2860 				sc->sc_sil_count = max(sc->sc_sil_count, q-s);
   2861 			DPRINTFN(5,("audio_pint_silence: fill cc=%d inp=%p, "
   2862 				    "count=%d size=%d\n",
   2863 				    cc, inp, sc->sc_sil_count,
   2864 				    (int)(cb->s.end - cb->s.start)));
   2865 			audio_fill_silence(&cb->s.param, inp, cc);
   2866 		} else {
   2867 			DPRINTFN(5,("audio_pint_silence: already silent "
   2868 				    "cc=%d inp=%p\n", cc, inp));
   2869 
   2870 		}
   2871 	} else {
   2872 		sc->sc_sil_start = inp;
   2873 		sc->sc_sil_count = cc;
   2874 		DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n",
   2875 			     inp, cc));
   2876 		audio_fill_silence(&cb->s.param, inp, cc);
   2877 	}
   2878 }
   2879 
   2880 static void
   2881 audio_softintr_rd(void *cookie)
   2882 {
   2883 	struct audio_softc *sc = cookie;
   2884 	proc_t *p;
   2885 	pid_t pid;
   2886 
   2887 	mutex_enter(sc->sc_lock);
   2888 	cv_broadcast(&sc->sc_rchan);
   2889 	selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
   2890 	if ((pid = sc->sc_async_audio) != 0) {
   2891 		DPRINTFN(3, ("audio_softintr_rd: sending SIGIO %d\n", pid));
   2892 		mutex_enter(proc_lock);
   2893 		if ((p = proc_find(pid)) != NULL)
   2894 			psignal(p, SIGIO);
   2895 		mutex_exit(proc_lock);
   2896 	}
   2897 	mutex_exit(sc->sc_lock);
   2898 }
   2899 
   2900 static void
   2901 audio_softintr_wr(void *cookie)
   2902 {
   2903 	struct audio_softc *sc = cookie;
   2904 	proc_t *p;
   2905 	pid_t pid;
   2906 
   2907 	mutex_enter(sc->sc_lock);
   2908 	cv_broadcast(&sc->sc_wchan);
   2909 	selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT);
   2910 	if ((pid = sc->sc_async_audio) != 0) {
   2911 		DPRINTFN(3, ("audio_softintr_wr: sending SIGIO %d\n", pid));
   2912 		mutex_enter(proc_lock);
   2913 		if ((p = proc_find(pid)) != NULL)
   2914 			psignal(p, SIGIO);
   2915 		mutex_exit(proc_lock);
   2916 	}
   2917 	mutex_exit(sc->sc_lock);
   2918 }
   2919 
   2920 /*
   2921  * Called from HW driver module on completion of DMA output.
   2922  * Start output of new block, wrap in ring buffer if needed.
   2923  * If no more buffers to play, output zero instead.
   2924  * Do a wakeup if necessary.
   2925  */
   2926 void
   2927 audio_pint(void *v)
   2928 {
   2929 	stream_fetcher_t null_fetcher;
   2930 	struct audio_softc *sc;
   2931 	const struct audio_hw_if *hw;
   2932 	struct audio_ringbuffer *cb;
   2933 	stream_fetcher_t *fetcher;
   2934 	uint8_t *inp;
   2935 	int cc, used;
   2936 	int blksize;
   2937 	int error;
   2938 
   2939 	sc = v;
   2940 
   2941 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2942 
   2943 	if (!sc->sc_open)
   2944 		return;		/* ignore interrupt if not open */
   2945 
   2946 	hw = sc->hw_if;
   2947 	cb = &sc->sc_pr;
   2948 	blksize = cb->blksize;
   2949 	cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize);
   2950 	cb->stamp += blksize;
   2951 	if (cb->mmapped) {
   2952 		DPRINTFN(5, ("audio_pint: mmapped outp=%p cc=%d inp=%p\n",
   2953 			     cb->s.outp, blksize, cb->s.inp));
   2954 		if (hw->trigger_output == NULL)
   2955 			(void)hw->start_output(sc->hw_hdl, __UNCONST(cb->s.outp),
   2956 			    blksize, audio_pint, (void *)sc);
   2957 		return;
   2958 	}
   2959 
   2960 #ifdef AUDIO_INTR_TIME
   2961 	{
   2962 		struct timeval tv;
   2963 		u_long t;
   2964 		microtime(&tv);
   2965 		t = tv.tv_usec + 1000000 * tv.tv_sec;
   2966 		if (sc->sc_pnintr) {
   2967 			long lastdelta, totdelta;
   2968 			lastdelta = t - sc->sc_plastintr - sc->sc_pblktime;
   2969 			if (lastdelta > sc->sc_pblktime / 3) {
   2970 				printf("audio: play interrupt(%d) off "
   2971 				       "relative by %ld us (%lu)\n",
   2972 				       sc->sc_pnintr, lastdelta,
   2973 				       sc->sc_pblktime);
   2974 			}
   2975 			totdelta = t - sc->sc_pfirstintr -
   2976 				sc->sc_pblktime * sc->sc_pnintr;
   2977 			if (totdelta > sc->sc_pblktime) {
   2978 				printf("audio: play interrupt(%d) off "
   2979 				       "absolute by %ld us (%lu) (LOST)\n",
   2980 				       sc->sc_pnintr, totdelta,
   2981 				       sc->sc_pblktime);
   2982 				sc->sc_pnintr++; /* avoid repeated messages */
   2983 			}
   2984 		} else
   2985 			sc->sc_pfirstintr = t;
   2986 		sc->sc_plastintr = t;
   2987 		sc->sc_pnintr++;
   2988 	}
   2989 #endif
   2990 
   2991 	used = audio_stream_get_used(&cb->s);
   2992 	/*
   2993 	 * "used <= cb->usedlow" should be "used < blksize" ideally.
   2994 	 * Some HW drivers such as uaudio(4) does not call audio_pint()
   2995 	 * at accurate timing.  If used < blksize, uaudio(4) already
   2996 	 * request transfer of garbage data.
   2997 	 */
   2998 	if (used <= cb->usedlow && !cb->copying && sc->sc_npfilters > 0) {
   2999 		/* we might have data in filter pipeline */
   3000 		null_fetcher.fetch_to = null_fetcher_fetch_to;
   3001 		fetcher = &sc->sc_pfilters[sc->sc_npfilters - 1]->base;
   3002 		sc->sc_pfilters[0]->set_fetcher(sc->sc_pfilters[0],
   3003 						&null_fetcher);
   3004 		used = audio_stream_get_used(sc->sc_pustream);
   3005 		cc = cb->s.end - cb->s.start;
   3006 		if (blksize * 2 < cc)
   3007 			cc = blksize * 2;
   3008 		fetcher->fetch_to(sc, fetcher, &cb->s, cc);
   3009 		cb->fstamp += used - audio_stream_get_used(sc->sc_pustream);
   3010 		used = audio_stream_get_used(&cb->s);
   3011 	}
   3012 	if (used < blksize) {
   3013 		/* we don't have a full block to use */
   3014 		if (cb->copying) {
   3015 			/* writer is in progress, don't disturb */
   3016 			cb->needfill = true;
   3017 			DPRINTFN(1, ("audio_pint: copying in progress\n"));
   3018 		} else {
   3019 			inp = cb->s.inp;
   3020 			cc = blksize - (inp - cb->s.start) % blksize;
   3021 			if (cb->pause)
   3022 				cb->pdrops += cc;
   3023 			else {
   3024 				cb->drops += cc;
   3025 				sc->sc_playdrop += cc;
   3026 			}
   3027 			audio_pint_silence(sc, cb, inp, cc);
   3028 			cb->s.inp = audio_stream_add_inp(&cb->s, inp, cc);
   3029 
   3030 			/* Clear next block so we keep ahead of the DMA. */
   3031 			used = audio_stream_get_used(&cb->s);
   3032 			if (used + blksize < cb->s.end - cb->s.start)
   3033 				audio_pint_silence(sc, cb, cb->s.inp, blksize);
   3034 		}
   3035 	}
   3036 
   3037 	DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->s.outp, blksize));
   3038 	if (hw->trigger_output == NULL) {
   3039 		error = hw->start_output(sc->hw_hdl, __UNCONST(cb->s.outp),
   3040 		    blksize, audio_pint, (void *)sc);
   3041 		if (error) {
   3042 			/* XXX does this really help? */
   3043 			DPRINTF(("audio_pint restart failed: %d\n", error));
   3044 			audio_clear(sc);
   3045 		}
   3046 	}
   3047 
   3048 	DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n",
   3049 		     sc->sc_mode, cb->pause,
   3050 		     audio_stream_get_used(sc->sc_pustream), cb->usedlow));
   3051 	if ((sc->sc_mode & AUMODE_PLAY) && !cb->pause) {
   3052 		if (audio_stream_get_used(sc->sc_pustream) <= cb->usedlow)
   3053 			softint_schedule(sc->sc_sih_wr);
   3054 	}
   3055 
   3056 	/* Possible to return one or more "phantom blocks" now. */
   3057 	if (!sc->sc_full_duplex)
   3058 		softint_schedule(sc->sc_sih_rd);
   3059 }
   3060 
   3061 /*
   3062  * Called from HW driver module on completion of DMA input.
   3063  * Mark it as input in the ring buffer (fiddle pointers).
   3064  * Do a wakeup if necessary.
   3065  */
   3066 void
   3067 audio_rint(void *v)
   3068 {
   3069 	stream_fetcher_t null_fetcher;
   3070 	struct audio_softc *sc;
   3071 	const struct audio_hw_if *hw;
   3072 	struct audio_ringbuffer *cb;
   3073 	stream_fetcher_t *last_fetcher;
   3074 	int cc;
   3075 	int used;
   3076 	int blksize;
   3077 	int error;
   3078 
   3079 	sc = v;
   3080 	cb = &sc->sc_rr;
   3081 
   3082 	KASSERT(mutex_owned(sc->sc_intr_lock));
   3083 
   3084 	if (!sc->sc_open)
   3085 		return;		/* ignore interrupt if not open */
   3086 
   3087 	hw = sc->hw_if;
   3088 	blksize = cb->blksize;
   3089 	cb->s.inp = audio_stream_add_inp(&cb->s, cb->s.inp, blksize);
   3090 	cb->stamp += blksize;
   3091 	if (cb->mmapped) {
   3092 		DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n",
   3093 			     cb->s.inp, blksize));
   3094 		if (hw->trigger_input == NULL)
   3095 			(void)hw->start_input(sc->hw_hdl, cb->s.inp, blksize,
   3096 			    audio_rint, (void *)sc);
   3097 		return;
   3098 	}
   3099 
   3100 #ifdef AUDIO_INTR_TIME
   3101 	{
   3102 		struct timeval tv;
   3103 		u_long t;
   3104 		microtime(&tv);
   3105 		t = tv.tv_usec + 1000000 * tv.tv_sec;
   3106 		if (sc->sc_rnintr) {
   3107 			long lastdelta, totdelta;
   3108 			lastdelta = t - sc->sc_rlastintr - sc->sc_rblktime;
   3109 			if (lastdelta > sc->sc_rblktime / 5) {
   3110 				printf("audio: record interrupt(%d) off "
   3111 				       "relative by %ld us (%lu)\n",
   3112 				       sc->sc_rnintr, lastdelta,
   3113 				       sc->sc_rblktime);
   3114 			}
   3115 			totdelta = t - sc->sc_rfirstintr -
   3116 				sc->sc_rblktime * sc->sc_rnintr;
   3117 			if (totdelta > sc->sc_rblktime / 2) {
   3118 				sc->sc_rnintr++;
   3119 				printf("audio: record interrupt(%d) off "
   3120 				       "absolute by %ld us (%lu)\n",
   3121 				       sc->sc_rnintr, totdelta,
   3122 				       sc->sc_rblktime);
   3123 				sc->sc_rnintr++; /* avoid repeated messages */
   3124 			}
   3125 		} else
   3126 			sc->sc_rfirstintr = t;
   3127 		sc->sc_rlastintr = t;
   3128 		sc->sc_rnintr++;
   3129 	}
   3130 #endif
   3131 
   3132 	if (!cb->pause && sc->sc_nrfilters > 0) {
   3133 		null_fetcher.fetch_to = null_fetcher_fetch_to;
   3134 		last_fetcher = &sc->sc_rfilters[sc->sc_nrfilters - 1]->base;
   3135 		sc->sc_rfilters[0]->set_fetcher(sc->sc_rfilters[0],
   3136 						&null_fetcher);
   3137 		used = audio_stream_get_used(sc->sc_rustream);
   3138 		cc = sc->sc_rustream->end - sc->sc_rustream->start;
   3139 		error = last_fetcher->fetch_to
   3140 			(sc, last_fetcher, sc->sc_rustream, cc);
   3141 		cb->fstamp += audio_stream_get_used(sc->sc_rustream) - used;
   3142 		/* XXX what should do for error? */
   3143 	}
   3144 	used = audio_stream_get_used(&sc->sc_rr.s);
   3145 	if (cb->pause) {
   3146 		DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops));
   3147 		cb->pdrops += blksize;
   3148 		cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize);
   3149 	} else if (used + blksize > cb->s.end - cb->s.start && !cb->copying) {
   3150 		DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops));
   3151 		cb->drops += blksize;
   3152 		cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize);
   3153 	}
   3154 
   3155 	DPRINTFN(2, ("audio_rint: inp=%p cc=%d\n", cb->s.inp, blksize));
   3156 	if (hw->trigger_input == NULL) {
   3157 		error = hw->start_input(sc->hw_hdl, cb->s.inp, blksize,
   3158 		    audio_rint, (void *)sc);
   3159 		if (error) {
   3160 			/* XXX does this really help? */
   3161 			DPRINTF(("audio_rint: restart failed: %d\n", error));
   3162 			audio_clear(sc);
   3163 		}
   3164 	}
   3165 
   3166 	softint_schedule(sc->sc_sih_rd);
   3167 }
   3168 
   3169 int
   3170 audio_check_params(struct audio_params *p)
   3171 {
   3172 
   3173 	if (p->encoding == AUDIO_ENCODING_PCM16) {
   3174 		if (p->precision == 8)
   3175 			p->encoding = AUDIO_ENCODING_ULINEAR;
   3176 		else
   3177 			p->encoding = AUDIO_ENCODING_SLINEAR;
   3178 	} else if (p->encoding == AUDIO_ENCODING_PCM8) {
   3179 		if (p->precision == 8)
   3180 			p->encoding = AUDIO_ENCODING_ULINEAR;
   3181 		else
   3182 			return EINVAL;
   3183 	}
   3184 
   3185 	if (p->encoding == AUDIO_ENCODING_SLINEAR)
   3186 #if BYTE_ORDER == LITTLE_ENDIAN
   3187 		p->encoding = AUDIO_ENCODING_SLINEAR_LE;
   3188 #else
   3189 		p->encoding = AUDIO_ENCODING_SLINEAR_BE;
   3190 #endif
   3191 	if (p->encoding == AUDIO_ENCODING_ULINEAR)
   3192 #if BYTE_ORDER == LITTLE_ENDIAN
   3193 		p->encoding = AUDIO_ENCODING_ULINEAR_LE;
   3194 #else
   3195 		p->encoding = AUDIO_ENCODING_ULINEAR_BE;
   3196 #endif
   3197 
   3198 	switch (p->encoding) {
   3199 	case AUDIO_ENCODING_ULAW:
   3200 	case AUDIO_ENCODING_ALAW:
   3201 		if (p->precision != 8)
   3202 			return EINVAL;
   3203 		break;
   3204 	case AUDIO_ENCODING_ADPCM:
   3205 		if (p->precision != 4 && p->precision != 8)
   3206 			return EINVAL;
   3207 		break;
   3208 	case AUDIO_ENCODING_SLINEAR_LE:
   3209 	case AUDIO_ENCODING_SLINEAR_BE:
   3210 	case AUDIO_ENCODING_ULINEAR_LE:
   3211 	case AUDIO_ENCODING_ULINEAR_BE:
   3212 		/* XXX is: our zero-fill can handle any multiple of 8 */
   3213 		if (p->precision !=  8 && p->precision != 16 &&
   3214 		    p->precision != 24 && p->precision != 32)
   3215 			return EINVAL;
   3216 		if (p->precision == 8 && p->encoding == AUDIO_ENCODING_SLINEAR_BE)
   3217 			p->encoding = AUDIO_ENCODING_SLINEAR_LE;
   3218 		if (p->precision == 8 && p->encoding == AUDIO_ENCODING_ULINEAR_BE)
   3219 			p->encoding = AUDIO_ENCODING_ULINEAR_LE;
   3220 		if (p->validbits > p->precision)
   3221 			return EINVAL;
   3222 		break;
   3223 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   3224 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   3225 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   3226 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   3227 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   3228 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   3229 	case AUDIO_ENCODING_AC3:
   3230 		break;
   3231 	default:
   3232 		return EINVAL;
   3233 	}
   3234 
   3235 	/* sanity check # of channels*/
   3236 	if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS)
   3237 		return EINVAL;
   3238 
   3239 	return 0;
   3240 }
   3241 
   3242 int
   3243 audio_set_defaults(struct audio_softc *sc, u_int mode)
   3244 {
   3245 	struct audio_info ai;
   3246 
   3247 	KASSERT(mutex_owned(sc->sc_lock));
   3248 
   3249 	/* default parameters */
   3250 	sc->sc_rparams = audio_default;
   3251 	sc->sc_pparams = audio_default;
   3252 	sc->sc_blkset = false;
   3253 
   3254 	AUDIO_INITINFO(&ai);
   3255 	ai.record.sample_rate = sc->sc_rparams.sample_rate;
   3256 	ai.record.encoding    = sc->sc_rparams.encoding;
   3257 	ai.record.channels    = sc->sc_rparams.channels;
   3258 	ai.record.precision   = sc->sc_rparams.precision;
   3259 	ai.record.pause	      = false;
   3260 	ai.play.sample_rate   = sc->sc_pparams.sample_rate;
   3261 	ai.play.encoding      = sc->sc_pparams.encoding;
   3262 	ai.play.channels      = sc->sc_pparams.channels;
   3263 	ai.play.precision     = sc->sc_pparams.precision;
   3264 	ai.play.pause         = false;
   3265 	ai.mode		      = mode;
   3266 
   3267 	return audiosetinfo(sc, &ai);
   3268 }
   3269 
   3270 int
   3271 au_set_lr_value(struct	audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
   3272 {
   3273 
   3274 	KASSERT(mutex_owned(sc->sc_lock));
   3275 
   3276 	ct->type = AUDIO_MIXER_VALUE;
   3277 	ct->un.value.num_channels = 2;
   3278 	ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   3279 	ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   3280 	if (sc->hw_if->set_port(sc->hw_hdl, ct) == 0)
   3281 		return 0;
   3282 	ct->un.value.num_channels = 1;
   3283 	ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
   3284 	return sc->hw_if->set_port(sc->hw_hdl, ct);
   3285 }
   3286 
   3287 int
   3288 au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   3289 	    int gain, int balance)
   3290 {
   3291 	mixer_ctrl_t ct;
   3292 	int i, error;
   3293 	int l, r;
   3294 	u_int mask;
   3295 	int nset;
   3296 
   3297 	KASSERT(mutex_owned(sc->sc_lock));
   3298 
   3299 	if (balance == AUDIO_MID_BALANCE) {
   3300 		l = r = gain;
   3301 	} else if (balance < AUDIO_MID_BALANCE) {
   3302 		l = gain;
   3303 		r = (balance * gain) / AUDIO_MID_BALANCE;
   3304 	} else {
   3305 		r = gain;
   3306 		l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
   3307 		    / AUDIO_MID_BALANCE;
   3308 	}
   3309 	DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n",
   3310 		 gain, balance, l, r));
   3311 
   3312 	if (ports->index == -1) {
   3313 	usemaster:
   3314 		if (ports->master == -1)
   3315 			return 0; /* just ignore it silently */
   3316 		ct.dev = ports->master;
   3317 		error = au_set_lr_value(sc, &ct, l, r);
   3318 	} else {
   3319 		ct.dev = ports->index;
   3320 		if (ports->isenum) {
   3321 			ct.type = AUDIO_MIXER_ENUM;
   3322 			error = sc->hw_if->get_port(sc->hw_hdl, &ct);
   3323 			if (error)
   3324 				return error;
   3325 			if (ports->isdual) {
   3326 				if (ports->cur_port == -1)
   3327 					ct.dev = ports->master;
   3328 				else
   3329 					ct.dev = ports->miport[ports->cur_port];
   3330 				error = au_set_lr_value(sc, &ct, l, r);
   3331 			} else {
   3332 				for(i = 0; i < ports->nports; i++)
   3333 				    if (ports->misel[i] == ct.un.ord) {
   3334 					    ct.dev = ports->miport[i];
   3335 					    if (ct.dev == -1 ||
   3336 						au_set_lr_value(sc, &ct, l, r))
   3337 						    goto usemaster;
   3338 					    else
   3339 						    break;
   3340 				    }
   3341 			}
   3342 		} else {
   3343 			ct.type = AUDIO_MIXER_SET;
   3344 			error = sc->hw_if->get_port(sc->hw_hdl, &ct);
   3345 			if (error)
   3346 				return error;
   3347 			mask = ct.un.mask;
   3348 			nset = 0;
   3349 			for(i = 0; i < ports->nports; i++) {
   3350 				if (ports->misel[i] & mask) {
   3351 				    ct.dev = ports->miport[i];
   3352 				    if (ct.dev != -1 &&
   3353 					au_set_lr_value(sc, &ct, l, r) == 0)
   3354 					    nset++;
   3355 				}
   3356 			}
   3357 			if (nset == 0)
   3358 				goto usemaster;
   3359 		}
   3360 	}
   3361 	if (!error)
   3362 		mixer_signal(sc);
   3363 	return error;
   3364 }
   3365 
   3366 int
   3367 au_get_lr_value(struct	audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
   3368 {
   3369 	int error;
   3370 
   3371 	KASSERT(mutex_owned(sc->sc_lock));
   3372 
   3373 	ct->un.value.num_channels = 2;
   3374 	if (sc->hw_if->get_port(sc->hw_hdl, ct) == 0) {
   3375 		*l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   3376 		*r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   3377 	} else {
   3378 		ct->un.value.num_channels = 1;
   3379 		error = sc->hw_if->get_port(sc->hw_hdl, ct);
   3380 		if (error)
   3381 			return error;
   3382 		*r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   3383 	}
   3384 	return 0;
   3385 }
   3386 
   3387 void
   3388 au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   3389 	    u_int *pgain, u_char *pbalance)
   3390 {
   3391 	mixer_ctrl_t ct;
   3392 	int i, l, r, n;
   3393 	int lgain, rgain;
   3394 
   3395 	KASSERT(mutex_owned(sc->sc_lock));
   3396 
   3397 	lgain = AUDIO_MAX_GAIN / 2;
   3398 	rgain = AUDIO_MAX_GAIN / 2;
   3399 	if (ports->index == -1) {
   3400 	usemaster:
   3401 		if (ports->master == -1)
   3402 			goto bad;
   3403 		ct.dev = ports->master;
   3404 		ct.type = AUDIO_MIXER_VALUE;
   3405 		if (au_get_lr_value(sc, &ct, &lgain, &rgain))
   3406 			goto bad;
   3407 	} else {
   3408 		ct.dev = ports->index;
   3409 		if (ports->isenum) {
   3410 			ct.type = AUDIO_MIXER_ENUM;
   3411 			if (sc->hw_if->get_port(sc->hw_hdl, &ct))
   3412 				goto bad;
   3413 			ct.type = AUDIO_MIXER_VALUE;
   3414 			if (ports->isdual) {
   3415 				if (ports->cur_port == -1)
   3416 					ct.dev = ports->master;
   3417 				else
   3418 					ct.dev = ports->miport[ports->cur_port];
   3419 				au_get_lr_value(sc, &ct, &lgain, &rgain);
   3420 			} else {
   3421 				for(i = 0; i < ports->nports; i++)
   3422 				    if (ports->misel[i] == ct.un.ord) {
   3423 					    ct.dev = ports->miport[i];
   3424 					    if (ct.dev == -1 ||
   3425 						au_get_lr_value(sc, &ct,
   3426 								&lgain, &rgain))
   3427 						    goto usemaster;
   3428 					    else
   3429 						    break;
   3430 				    }
   3431 			}
   3432 		} else {
   3433 			ct.type = AUDIO_MIXER_SET;
   3434 			if (sc->hw_if->get_port(sc->hw_hdl, &ct))
   3435 				goto bad;
   3436 			ct.type = AUDIO_MIXER_VALUE;
   3437 			lgain = rgain = n = 0;
   3438 			for(i = 0; i < ports->nports; i++) {
   3439 				if (ports->misel[i] & ct.un.mask) {
   3440 					ct.dev = ports->miport[i];
   3441 					if (ct.dev == -1 ||
   3442 					    au_get_lr_value(sc, &ct, &l, &r))
   3443 						goto usemaster;
   3444 					else {
   3445 						lgain += l;
   3446 						rgain += r;
   3447 						n++;
   3448 					}
   3449 				}
   3450 			}
   3451 			if (n != 0) {
   3452 				lgain /= n;
   3453 				rgain /= n;
   3454 			}
   3455 		}
   3456 	}
   3457 bad:
   3458 	if (lgain == rgain) {	/* handles lgain==rgain==0 */
   3459 		*pgain = lgain;
   3460 		*pbalance = AUDIO_MID_BALANCE;
   3461 	} else if (lgain < rgain) {
   3462 		*pgain = rgain;
   3463 		/* balance should be > AUDIO_MID_BALANCE */
   3464 		*pbalance = AUDIO_RIGHT_BALANCE -
   3465 			(AUDIO_MID_BALANCE * lgain) / rgain;
   3466 	} else /* lgain > rgain */ {
   3467 		*pgain = lgain;
   3468 		/* balance should be < AUDIO_MID_BALANCE */
   3469 		*pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
   3470 	}
   3471 }
   3472 
   3473 int
   3474 au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
   3475 {
   3476 	mixer_ctrl_t ct;
   3477 	int i, error, use_mixerout;
   3478 
   3479 	KASSERT(mutex_owned(sc->sc_lock));
   3480 
   3481 	use_mixerout = 1;
   3482 	if (port == 0) {
   3483 		if (ports->allports == 0)
   3484 			return 0;		/* Allow this special case. */
   3485 		else if (ports->isdual) {
   3486 			if (ports->cur_port == -1) {
   3487 				return 0;
   3488 			} else {
   3489 				port = ports->aumask[ports->cur_port];
   3490 				ports->cur_port = -1;
   3491 				use_mixerout = 0;
   3492 			}
   3493 		}
   3494 	}
   3495 	if (ports->index == -1)
   3496 		return EINVAL;
   3497 	ct.dev = ports->index;
   3498 	if (ports->isenum) {
   3499 		if (port & (port-1))
   3500 			return EINVAL; /* Only one port allowed */
   3501 		ct.type = AUDIO_MIXER_ENUM;
   3502 		error = EINVAL;
   3503 		for(i = 0; i < ports->nports; i++)
   3504 			if (ports->aumask[i] == port) {
   3505 				if (ports->isdual && use_mixerout) {
   3506 					ct.un.ord = ports->mixerout;
   3507 					ports->cur_port = i;
   3508 				} else {
   3509 					ct.un.ord = ports->misel[i];
   3510 				}
   3511 				error = sc->hw_if->set_port(sc->hw_hdl, &ct);
   3512 				break;
   3513 			}
   3514 	} else {
   3515 		ct.type = AUDIO_MIXER_SET;
   3516 		ct.un.mask = 0;
   3517 		for(i = 0; i < ports->nports; i++)
   3518 			if (ports->aumask[i] & port)
   3519 				ct.un.mask |= ports->misel[i];
   3520 		if (port != 0 && ct.un.mask == 0)
   3521 			error = EINVAL;
   3522 		else
   3523 			error = sc->hw_if->set_port(sc->hw_hdl, &ct);
   3524 	}
   3525 	if (!error)
   3526 		mixer_signal(sc);
   3527 	return error;
   3528 }
   3529 
   3530 int
   3531 au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
   3532 {
   3533 	mixer_ctrl_t ct;
   3534 	int i, aumask;
   3535 
   3536 	KASSERT(mutex_owned(sc->sc_lock));
   3537 
   3538 	if (ports->index == -1)
   3539 		return 0;
   3540 	ct.dev = ports->index;
   3541 	ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
   3542 	if (sc->hw_if->get_port(sc->hw_hdl, &ct))
   3543 		return 0;
   3544 	aumask = 0;
   3545 	if (ports->isenum) {
   3546 		if (ports->isdual && ports->cur_port != -1) {
   3547 			if (ports->mixerout == ct.un.ord)
   3548 				aumask = ports->aumask[ports->cur_port];
   3549 			else
   3550 				ports->cur_port = -1;
   3551 		}
   3552 		if (aumask == 0)
   3553 			for(i = 0; i < ports->nports; i++)
   3554 				if (ports->misel[i] == ct.un.ord)
   3555 					aumask = ports->aumask[i];
   3556 	} else {
   3557 		for(i = 0; i < ports->nports; i++)
   3558 			if (ct.un.mask & ports->misel[i])
   3559 				aumask |= ports->aumask[i];
   3560 	}
   3561 	return aumask;
   3562 }
   3563 
   3564 int
   3565 audiosetinfo(struct audio_softc *sc, struct audio_info *ai)
   3566 {
   3567 	stream_filter_list_t pfilters, rfilters;
   3568 	audio_params_t pp, rp;
   3569 	struct audio_prinfo *r, *p;
   3570 	const struct audio_hw_if *hw;
   3571 	audio_stream_t *oldpus, *oldrus;
   3572 	int setmode;
   3573 	int error;
   3574 	int np, nr;
   3575 	unsigned int blks;
   3576 	int oldpblksize, oldrblksize;
   3577 	u_int gain;
   3578 	bool rbus, pbus;
   3579 	bool cleared, modechange, pausechange;
   3580 	u_char balance;
   3581 
   3582 	KASSERT(mutex_owned(sc->sc_lock));
   3583 
   3584 	hw = sc->hw_if;
   3585 	if (hw == NULL)		/* HW has not attached */
   3586 		return ENXIO;
   3587 
   3588 	DPRINTF(("%s sc=%p ai=%p\n", __func__, sc, ai));
   3589 	r = &ai->record;
   3590 	p = &ai->play;
   3591 	rbus = sc->sc_rbus;
   3592 	pbus = sc->sc_pbus;
   3593 	error = 0;
   3594 	cleared = false;
   3595 	modechange = false;
   3596 	pausechange = false;
   3597 
   3598 	pp = sc->sc_pparams;	/* Temporary encoding storage in */
   3599 	rp = sc->sc_rparams;	/* case setting the modes fails. */
   3600 	nr = np = 0;
   3601 
   3602 	if (SPECIFIED(p->sample_rate)) {
   3603 		pp.sample_rate = p->sample_rate;
   3604 		np++;
   3605 	}
   3606 	if (SPECIFIED(r->sample_rate)) {
   3607 		rp.sample_rate = r->sample_rate;
   3608 		nr++;
   3609 	}
   3610 	if (SPECIFIED(p->encoding)) {
   3611 		pp.encoding = p->encoding;
   3612 		np++;
   3613 	}
   3614 	if (SPECIFIED(r->encoding)) {
   3615 		rp.encoding = r->encoding;
   3616 		nr++;
   3617 	}
   3618 	if (SPECIFIED(p->precision)) {
   3619 		pp.precision = p->precision;
   3620 		/* we don't have API to specify validbits */
   3621 		pp.validbits = p->precision;
   3622 		np++;
   3623 	}
   3624 	if (SPECIFIED(r->precision)) {
   3625 		rp.precision = r->precision;
   3626 		/* we don't have API to specify validbits */
   3627 		rp.validbits = r->precision;
   3628 		nr++;
   3629 	}
   3630 	if (SPECIFIED(p->channels)) {
   3631 		pp.channels = p->channels;
   3632 		np++;
   3633 	}
   3634 	if (SPECIFIED(r->channels)) {
   3635 		rp.channels = r->channels;
   3636 		nr++;
   3637 	}
   3638 
   3639 	if (!audio_can_capture(sc))
   3640 		nr = 0;
   3641 	if (!audio_can_playback(sc))
   3642 		np = 0;
   3643 
   3644 #ifdef AUDIO_DEBUG
   3645 	if (audiodebug && nr > 0)
   3646 	    audio_print_params("audiosetinfo() Setting record params:", &rp);
   3647 	if (audiodebug && np > 0)
   3648 	    audio_print_params("audiosetinfo() Setting play params:", &pp);
   3649 #endif
   3650 	if (nr > 0 && (error = audio_check_params(&rp)))
   3651 		return error;
   3652 	if (np > 0 && (error = audio_check_params(&pp)))
   3653 		return error;
   3654 
   3655 	oldpblksize = sc->sc_pr.blksize;
   3656 	oldrblksize = sc->sc_rr.blksize;
   3657 
   3658 	setmode = 0;
   3659 	if (nr > 0) {
   3660 		if (!cleared) {
   3661 			audio_clear_intr_unlocked(sc);
   3662 			cleared = true;
   3663 		}
   3664 		modechange = true;
   3665 		setmode |= AUMODE_RECORD;
   3666 	}
   3667 	if (np > 0) {
   3668 		if (!cleared) {
   3669 			audio_clear_intr_unlocked(sc);
   3670 			cleared = true;
   3671 		}
   3672 		modechange = true;
   3673 		setmode |= AUMODE_PLAY;
   3674 	}
   3675 
   3676 	if (SPECIFIED(ai->mode)) {
   3677 		if (!cleared) {
   3678 			audio_clear_intr_unlocked(sc);
   3679 			cleared = true;
   3680 		}
   3681 		modechange = true;
   3682 		sc->sc_mode = ai->mode;
   3683 		if (sc->sc_mode & AUMODE_PLAY_ALL)
   3684 			sc->sc_mode |= AUMODE_PLAY;
   3685 		if ((sc->sc_mode & AUMODE_PLAY) && !sc->sc_full_duplex)
   3686 			/* Play takes precedence */
   3687 			sc->sc_mode &= ~AUMODE_RECORD;
   3688 	}
   3689 
   3690 	oldpus = sc->sc_pustream;
   3691 	oldrus = sc->sc_rustream;
   3692 	if (modechange) {
   3693 		int indep;
   3694 
   3695 		indep = audio_get_props(sc) & AUDIO_PROP_INDEPENDENT;
   3696 		if (!indep) {
   3697 			if (setmode == AUMODE_RECORD)
   3698 				pp = rp;
   3699 			else if (setmode == AUMODE_PLAY)
   3700 				rp = pp;
   3701 		}
   3702 		memset(&pfilters, 0, sizeof(pfilters));
   3703 		memset(&rfilters, 0, sizeof(rfilters));
   3704 		pfilters.append = stream_filter_list_append;
   3705 		pfilters.prepend = stream_filter_list_prepend;
   3706 		pfilters.set = stream_filter_list_set;
   3707 		rfilters.append = stream_filter_list_append;
   3708 		rfilters.prepend = stream_filter_list_prepend;
   3709 		rfilters.set = stream_filter_list_set;
   3710 		/* Some device drivers change channels/sample_rate and change
   3711 		 * no channels/sample_rate. */
   3712 		error = hw->set_params(sc->hw_hdl, setmode,
   3713 		    sc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp,
   3714 		    &pfilters, &rfilters);
   3715 		if (error) {
   3716 			DPRINTF(("%s: hw->set_params() failed with %d\n",
   3717 				 __func__, error));
   3718 			goto cleanup;
   3719 		}
   3720 
   3721 		audio_check_params(&pp);
   3722 		audio_check_params(&rp);
   3723 		if (!indep) {
   3724 			/* XXX for !indep device, we have to use the same
   3725 			 * parameters for the hardware, not userland */
   3726 			if (setmode == AUMODE_RECORD) {
   3727 				pp = rp;
   3728 			} else if (setmode == AUMODE_PLAY) {
   3729 				rp = pp;
   3730 			}
   3731 		}
   3732 
   3733 		if (sc->sc_pr.mmapped && pfilters.req_size > 0) {
   3734 			DPRINTF(("%s: mmapped, and filters are requested.\n",
   3735 				 __func__));
   3736 			error = EINVAL;
   3737 			goto cleanup;
   3738 		}
   3739 
   3740 		/* construct new filter chain */
   3741 		if (setmode & AUMODE_PLAY) {
   3742 			error = audio_setup_pfilters(sc, &pp, &pfilters);
   3743 			if (error)
   3744 				goto cleanup;
   3745 		}
   3746 		if (setmode & AUMODE_RECORD) {
   3747 			error = audio_setup_rfilters(sc, &rp, &rfilters);
   3748 			if (error)
   3749 				goto cleanup;
   3750 		}
   3751 		DPRINTF(("%s: filter setup is completed.\n", __func__));
   3752 
   3753 		/* userland formats */
   3754 		sc->sc_pparams = pp;
   3755 		sc->sc_rparams = rp;
   3756 	}
   3757 
   3758 	/* Play params can affect the record params, so recalculate blksize. */
   3759 	if (nr > 0 || np > 0) {
   3760 		audio_calc_blksize(sc, AUMODE_RECORD);
   3761 		audio_calc_blksize(sc, AUMODE_PLAY);
   3762 	}
   3763 #ifdef AUDIO_DEBUG
   3764 	if (audiodebug > 1 && nr > 0)
   3765 	    audio_print_params("audiosetinfo() After setting record params:",
   3766 		&sc->sc_rparams);
   3767 	if (audiodebug > 1 && np > 0)
   3768 	    audio_print_params("audiosetinfo() After setting play params:",
   3769 		&sc->sc_pparams);
   3770 #endif
   3771 
   3772 	if (SPECIFIED(p->port)) {
   3773 		if (!cleared) {
   3774 			audio_clear_intr_unlocked(sc);
   3775 			cleared = true;
   3776 		}
   3777 		error = au_set_port(sc, &sc->sc_outports, p->port);
   3778 		if (error)
   3779 			goto cleanup;
   3780 	}
   3781 	if (SPECIFIED(r->port)) {
   3782 		if (!cleared) {
   3783 			audio_clear_intr_unlocked(sc);
   3784 			cleared = true;
   3785 		}
   3786 		error = au_set_port(sc, &sc->sc_inports, r->port);
   3787 		if (error)
   3788 			goto cleanup;
   3789 	}
   3790 	if (SPECIFIED(p->gain)) {
   3791 		au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   3792 		error = au_set_gain(sc, &sc->sc_outports, p->gain, balance);
   3793 		if (error)
   3794 			goto cleanup;
   3795 	}
   3796 	if (SPECIFIED(r->gain)) {
   3797 		au_get_gain(sc, &sc->sc_inports, &gain, &balance);
   3798 		error = au_set_gain(sc, &sc->sc_inports, r->gain, balance);
   3799 		if (error)
   3800 			goto cleanup;
   3801 	}
   3802 
   3803 	if (SPECIFIED_CH(p->balance)) {
   3804 		au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   3805 		error = au_set_gain(sc, &sc->sc_outports, gain, p->balance);
   3806 		if (error)
   3807 			goto cleanup;
   3808 	}
   3809 	if (SPECIFIED_CH(r->balance)) {
   3810 		au_get_gain(sc, &sc->sc_inports, &gain, &balance);
   3811 		error = au_set_gain(sc, &sc->sc_inports, gain, r->balance);
   3812 		if (error)
   3813 			goto cleanup;
   3814 	}
   3815 
   3816 	if (SPECIFIED(ai->monitor_gain) && sc->sc_monitor_port != -1) {
   3817 		mixer_ctrl_t ct;
   3818 
   3819 		ct.dev = sc->sc_monitor_port;
   3820 		ct.type = AUDIO_MIXER_VALUE;
   3821 		ct.un.value.num_channels = 1;
   3822 		ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain;
   3823 		error = sc->hw_if->set_port(sc->hw_hdl, &ct);
   3824 		if (error)
   3825 			goto cleanup;
   3826 	}
   3827 
   3828 	if (SPECIFIED_CH(p->pause)) {
   3829 		sc->sc_pr.pause = p->pause;
   3830 		pbus = !p->pause;
   3831 		pausechange = true;
   3832 	}
   3833 	if (SPECIFIED_CH(r->pause)) {
   3834 		sc->sc_rr.pause = r->pause;
   3835 		rbus = !r->pause;
   3836 		pausechange = true;
   3837 	}
   3838 
   3839 	if (SPECIFIED(ai->blocksize)) {
   3840 		int pblksize, rblksize;
   3841 
   3842 		/* Block size specified explicitly. */
   3843 		if (ai->blocksize == 0) {
   3844 			if (!cleared) {
   3845 				audio_clear_intr_unlocked(sc);
   3846 				cleared = true;
   3847 			}
   3848 			sc->sc_blkset = false;
   3849 			audio_calc_blksize(sc, AUMODE_RECORD);
   3850 			audio_calc_blksize(sc, AUMODE_PLAY);
   3851 		} else {
   3852 			sc->sc_blkset = true;
   3853 			/* check whether new blocksize changes actually */
   3854 			if (hw->round_blocksize == NULL) {
   3855 				if (!cleared) {
   3856 					audio_clear_intr_unlocked(sc);
   3857 					cleared = true;
   3858 				}
   3859 				sc->sc_pr.blksize = ai->blocksize;
   3860 				sc->sc_rr.blksize = ai->blocksize;
   3861 			} else {
   3862 				pblksize = hw->round_blocksize(sc->hw_hdl,
   3863 				    ai->blocksize, AUMODE_PLAY, &sc->sc_pr.s.param);
   3864 				rblksize = hw->round_blocksize(sc->hw_hdl,
   3865 				    ai->blocksize, AUMODE_RECORD, &sc->sc_rr.s.param);
   3866 				if (pblksize != sc->sc_pr.blksize ||
   3867 				    rblksize != sc->sc_rr.blksize) {
   3868 					if (!cleared) {
   3869 						audio_clear_intr_unlocked(sc);
   3870 						cleared = true;
   3871 					}
   3872 					sc->sc_pr.blksize = ai->blocksize;
   3873 					sc->sc_rr.blksize = ai->blocksize;
   3874 				}
   3875 			}
   3876 		}
   3877 	}
   3878 
   3879 	if (SPECIFIED(ai->mode)) {
   3880 		if (sc->sc_mode & AUMODE_PLAY)
   3881 			audio_init_play(sc);
   3882 		if (sc->sc_mode & AUMODE_RECORD)
   3883 			audio_init_record(sc);
   3884 	}
   3885 
   3886 	if (hw->commit_settings) {
   3887 		error = hw->commit_settings(sc->hw_hdl);
   3888 		if (error)
   3889 			goto cleanup;
   3890 	}
   3891 
   3892 	sc->sc_lastinfo = *ai;
   3893 	sc->sc_lastinfovalid = true;
   3894 
   3895 cleanup:
   3896 	if (cleared || pausechange) {
   3897 		int init_error;
   3898 
   3899 		mutex_enter(sc->sc_intr_lock);
   3900 		init_error = audio_initbufs(sc);
   3901 		if (init_error) goto err;
   3902 		if (sc->sc_pr.blksize != oldpblksize ||
   3903 		    sc->sc_rr.blksize != oldrblksize ||
   3904 		    sc->sc_pustream != oldpus ||
   3905 		    sc->sc_rustream != oldrus)
   3906 			audio_calcwater(sc);
   3907 		if ((sc->sc_mode & AUMODE_PLAY) &&
   3908 		    pbus && !sc->sc_pbus)
   3909 			init_error = audiostartp(sc);
   3910 		if (!init_error &&
   3911 		    (sc->sc_mode & AUMODE_RECORD) &&
   3912 		    rbus && !sc->sc_rbus)
   3913 			init_error = audiostartr(sc);
   3914 	err:
   3915 		mutex_exit(sc->sc_intr_lock);
   3916 		if (init_error)
   3917 			return init_error;
   3918 	}
   3919 
   3920 	/* Change water marks after initializing the buffers. */
   3921 	if (SPECIFIED(ai->hiwat)) {
   3922 		blks = ai->hiwat;
   3923 		if (blks > sc->sc_pr.maxblks)
   3924 			blks = sc->sc_pr.maxblks;
   3925 		if (blks < 2)
   3926 			blks = 2;
   3927 		sc->sc_pr.usedhigh = blks * sc->sc_pr.blksize;
   3928 	}
   3929 	if (SPECIFIED(ai->lowat)) {
   3930 		blks = ai->lowat;
   3931 		if (blks > sc->sc_pr.maxblks - 1)
   3932 			blks = sc->sc_pr.maxblks - 1;
   3933 		sc->sc_pr.usedlow = blks * sc->sc_pr.blksize;
   3934 	}
   3935 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   3936 		if (sc->sc_pr.usedlow > sc->sc_pr.usedhigh - sc->sc_pr.blksize)
   3937 			sc->sc_pr.usedlow =
   3938 				sc->sc_pr.usedhigh - sc->sc_pr.blksize;
   3939 	}
   3940 
   3941 	return error;
   3942 }
   3943 
   3944 int
   3945 audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int buf_only_mode)
   3946 {
   3947 	struct audio_prinfo *r, *p;
   3948 	const struct audio_hw_if *hw;
   3949 
   3950 	KASSERT(mutex_owned(sc->sc_lock));
   3951 
   3952 	r = &ai->record;
   3953 	p = &ai->play;
   3954 	hw = sc->hw_if;
   3955 	if (hw == NULL)		/* HW has not attached */
   3956 		return ENXIO;
   3957 
   3958 	p->sample_rate = sc->sc_pparams.sample_rate;
   3959 	r->sample_rate = sc->sc_rparams.sample_rate;
   3960 	p->channels = sc->sc_pparams.channels;
   3961 	r->channels = sc->sc_rparams.channels;
   3962 	p->precision = sc->sc_pparams.precision;
   3963 	r->precision = sc->sc_rparams.precision;
   3964 	p->encoding = sc->sc_pparams.encoding;
   3965 	r->encoding = sc->sc_rparams.encoding;
   3966 
   3967 	if (buf_only_mode) {
   3968 		r->port = 0;
   3969 		p->port = 0;
   3970 
   3971 		r->avail_ports = 0;
   3972 		p->avail_ports = 0;
   3973 
   3974 		r->gain = 0;
   3975 		r->balance = 0;
   3976 
   3977 		p->gain = 0;
   3978 		p->balance = 0;
   3979 	} else {
   3980 		r->port = au_get_port(sc, &sc->sc_inports);
   3981 		p->port = au_get_port(sc, &sc->sc_outports);
   3982 
   3983 		r->avail_ports = sc->sc_inports.allports;
   3984 		p->avail_ports = sc->sc_outports.allports;
   3985 
   3986 		au_get_gain(sc, &sc->sc_inports,  &r->gain, &r->balance);
   3987 		au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance);
   3988 	}
   3989 
   3990 	if (sc->sc_monitor_port != -1 && buf_only_mode == 0) {
   3991 		mixer_ctrl_t ct;
   3992 
   3993 		ct.dev = sc->sc_monitor_port;
   3994 		ct.type = AUDIO_MIXER_VALUE;
   3995 		ct.un.value.num_channels = 1;
   3996 		if (sc->hw_if->get_port(sc->hw_hdl, &ct))
   3997 			ai->monitor_gain = 0;
   3998 		else
   3999 			ai->monitor_gain =
   4000 				ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
   4001 	} else
   4002 		ai->monitor_gain = 0;
   4003 
   4004 	p->seek = audio_stream_get_used(sc->sc_pustream);
   4005 	r->seek = audio_stream_get_used(sc->sc_rustream);
   4006 
   4007 	/*
   4008 	 * XXX samples should be a value for userland data.
   4009 	 * But drops is a value for HW data.
   4010 	 */
   4011 	p->samples = (sc->sc_pustream == &sc->sc_pr.s
   4012 		      ? sc->sc_pr.stamp : sc->sc_pr.fstamp) - sc->sc_pr.drops;
   4013 	r->samples = (sc->sc_rustream == &sc->sc_rr.s
   4014 		      ? sc->sc_rr.stamp : sc->sc_rr.fstamp) - sc->sc_rr.drops;
   4015 
   4016 	p->eof = sc->sc_eof;
   4017 	r->eof = 0;
   4018 
   4019 	p->pause = sc->sc_pr.pause;
   4020 	r->pause = sc->sc_rr.pause;
   4021 
   4022 	p->error = sc->sc_pr.drops != 0;
   4023 	r->error = sc->sc_rr.drops != 0;
   4024 
   4025 	p->waiting = r->waiting = 0;		/* open never hangs */
   4026 
   4027 	p->open = (sc->sc_open & AUOPEN_WRITE) != 0;
   4028 	r->open = (sc->sc_open & AUOPEN_READ) != 0;
   4029 
   4030 	p->active = sc->sc_pbus;
   4031 	r->active = sc->sc_rbus;
   4032 
   4033 	p->buffer_size = sc->sc_pustream ? sc->sc_pustream->bufsize : 0;
   4034 	r->buffer_size = sc->sc_rustream ? sc->sc_rustream->bufsize : 0;
   4035 
   4036 	ai->blocksize = sc->sc_pr.blksize;
   4037 	if (sc->sc_pr.blksize > 0) {
   4038 		ai->hiwat = sc->sc_pr.usedhigh / sc->sc_pr.blksize;
   4039 		ai->lowat = sc->sc_pr.usedlow / sc->sc_pr.blksize;
   4040 	} else
   4041 		ai->hiwat = ai->lowat = 0;
   4042 	ai->mode = sc->sc_mode;
   4043 
   4044 	return 0;
   4045 }
   4046 
   4047 /*
   4048  * Mixer driver
   4049  */
   4050 int
   4051 mixer_open(dev_t dev, struct audio_softc *sc, int flags,
   4052     int ifmt, struct lwp *l)
   4053 {
   4054 
   4055 	KASSERT(mutex_owned(sc->sc_lock));
   4056 
   4057 	if (sc->hw_if == NULL)
   4058 		return  ENXIO;
   4059 
   4060 	DPRINTF(("mixer_open: flags=0x%x sc=%p\n", flags, sc));
   4061 
   4062 	return 0;
   4063 }
   4064 
   4065 /*
   4066  * Remove a process from those to be signalled on mixer activity.
   4067  */
   4068 static void
   4069 mixer_remove(struct audio_softc *sc)
   4070 {
   4071 	struct mixer_asyncs **pm, *m;
   4072 	pid_t pid;
   4073 
   4074 	KASSERT(mutex_owned(sc->sc_lock));
   4075 
   4076 	pid = curproc->p_pid;
   4077 	for (pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) {
   4078 		if ((*pm)->pid == pid) {
   4079 			m = *pm;
   4080 			*pm = m->next;
   4081 			kmem_free(m, sizeof(*m));
   4082 			return;
   4083 		}
   4084 	}
   4085 }
   4086 
   4087 /*
   4088  * Signal all processes waiting for the mixer.
   4089  */
   4090 static void
   4091 mixer_signal(struct audio_softc *sc)
   4092 {
   4093 	struct mixer_asyncs *m;
   4094 	proc_t *p;
   4095 
   4096 	for (m = sc->sc_async_mixer; m; m = m->next) {
   4097 		mutex_enter(proc_lock);
   4098 		if ((p = proc_find(m->pid)) != NULL)
   4099 			psignal(p, SIGIO);
   4100 		mutex_exit(proc_lock);
   4101 	}
   4102 }
   4103 
   4104 /*
   4105  * Close a mixer device
   4106  */
   4107 /* ARGSUSED */
   4108 int
   4109 mixer_close(struct audio_softc *sc, int flags, int ifmt, struct lwp *l)
   4110 {
   4111 
   4112 	KASSERT(mutex_owned(sc->sc_lock));
   4113 
   4114 	DPRINTF(("mixer_close: sc %p\n", sc));
   4115 	mixer_remove(sc);
   4116 	return 0;
   4117 }
   4118 
   4119 int
   4120 mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
   4121 	    struct lwp *l)
   4122 {
   4123 	const struct audio_hw_if *hw;
   4124 	struct mixer_asyncs *ma;
   4125 	mixer_ctrl_t *mc;
   4126 	int error;
   4127 
   4128 	DPRINTF(("mixer_ioctl(%lu,'%c',%lu)\n",
   4129 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
   4130 	hw = sc->hw_if;
   4131 	error = EINVAL;
   4132 
   4133 	/* we can return cached values if we are sleeping */
   4134 	if (cmd != AUDIO_MIXER_READ)
   4135 		device_active(sc->dev, DVA_SYSTEM);
   4136 
   4137 	switch (cmd) {
   4138 	case FIOASYNC:
   4139 		if (*(int *)addr) {
   4140 			mutex_exit(sc->sc_lock);
   4141 			ma = kmem_alloc(sizeof(struct mixer_asyncs), KM_SLEEP);
   4142 			mutex_enter(sc->sc_lock);
   4143 		} else {
   4144 			ma = NULL;
   4145 		}
   4146 		mixer_remove(sc);	/* remove old entry */
   4147 		if (ma != NULL) {
   4148 			ma->next = sc->sc_async_mixer;
   4149 			ma->pid = curproc->p_pid;
   4150 			sc->sc_async_mixer = ma;
   4151 		}
   4152 		error = 0;
   4153 		break;
   4154 
   4155 	case AUDIO_GETDEV:
   4156 		DPRINTF(("AUDIO_GETDEV\n"));
   4157 		error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
   4158 		break;
   4159 
   4160 	case AUDIO_MIXER_DEVINFO:
   4161 		DPRINTF(("AUDIO_MIXER_DEVINFO\n"));
   4162 		((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */
   4163 		error = hw->query_devinfo(sc->hw_hdl, (mixer_devinfo_t *)addr);
   4164 		break;
   4165 
   4166 	case AUDIO_MIXER_READ:
   4167 		DPRINTF(("AUDIO_MIXER_READ\n"));
   4168 		mc = (mixer_ctrl_t *)addr;
   4169 
   4170 		if (device_is_active(sc->sc_dev))
   4171 			error = hw->get_port(sc->hw_hdl, mc);
   4172 		else if (mc->dev >= sc->sc_nmixer_states)
   4173 			error = ENXIO;
   4174 		else {
   4175 			int dev = mc->dev;
   4176 			memcpy(mc, &sc->sc_mixer_state[dev],
   4177 			    sizeof(mixer_ctrl_t));
   4178 			error = 0;
   4179 		}
   4180 		break;
   4181 
   4182 	case AUDIO_MIXER_WRITE:
   4183 		DPRINTF(("AUDIO_MIXER_WRITE\n"));
   4184 		error = hw->set_port(sc->hw_hdl, (mixer_ctrl_t *)addr);
   4185 		if (!error && hw->commit_settings)
   4186 			error = hw->commit_settings(sc->hw_hdl);
   4187 		if (!error)
   4188 			mixer_signal(sc);
   4189 		break;
   4190 
   4191 	default:
   4192 		if (hw->dev_ioctl)
   4193 			error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l);
   4194 		else
   4195 			error = EINVAL;
   4196 		break;
   4197 	}
   4198 	DPRINTF(("mixer_ioctl(%lu,'%c',%lu) result %d\n",
   4199 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
   4200 	return error;
   4201 }
   4202 #endif /* NAUDIO > 0 */
   4203 
   4204 #include "midi.h"
   4205 
   4206 #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
   4207 #include <sys/param.h>
   4208 #include <sys/systm.h>
   4209 #include <sys/device.h>
   4210 #include <sys/audioio.h>
   4211 #include <dev/audio_if.h>
   4212 #endif
   4213 
   4214 #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
   4215 int
   4216 audioprint(void *aux, const char *pnp)
   4217 {
   4218 	struct audio_attach_args *arg;
   4219 	const char *type;
   4220 
   4221 	if (pnp != NULL) {
   4222 		arg = aux;
   4223 		switch (arg->type) {
   4224 		case AUDIODEV_TYPE_AUDIO:
   4225 			type = "audio";
   4226 			break;
   4227 		case AUDIODEV_TYPE_MIDI:
   4228 			type = "midi";
   4229 			break;
   4230 		case AUDIODEV_TYPE_OPL:
   4231 			type = "opl";
   4232 			break;
   4233 		case AUDIODEV_TYPE_MPU:
   4234 			type = "mpu";
   4235 			break;
   4236 		default:
   4237 			panic("audioprint: unknown type %d", arg->type);
   4238 		}
   4239 		aprint_normal("%s at %s", type, pnp);
   4240 	}
   4241 	return UNCONF;
   4242 }
   4243 
   4244 #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */
   4245 
   4246 #if NAUDIO > 0
   4247 device_t
   4248 audio_get_device(struct audio_softc *sc)
   4249 {
   4250 	return sc->sc_dev;
   4251 }
   4252 #endif
   4253 
   4254 #if NAUDIO > 0
   4255 static void
   4256 audio_mixer_capture(struct audio_softc *sc)
   4257 {
   4258 	mixer_devinfo_t mi;
   4259 	mixer_ctrl_t *mc;
   4260 
   4261 	KASSERT(mutex_owned(sc->sc_lock));
   4262 
   4263 	for (mi.index = 0;; mi.index++) {
   4264 		if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) != 0)
   4265 			break;
   4266 		KASSERT(mi.index < sc->sc_nmixer_states);
   4267 		if (mi.type == AUDIO_MIXER_CLASS)
   4268 			continue;
   4269 		mc = &sc->sc_mixer_state[mi.index];
   4270 		mc->dev = mi.index;
   4271 		mc->type = mi.type;
   4272 		mc->un.value.num_channels = mi.un.v.num_channels;
   4273 		(void)sc->hw_if->get_port(sc->hw_hdl, mc);
   4274 	}
   4275 
   4276 	return;
   4277 }
   4278 
   4279 static void
   4280 audio_mixer_restore(struct audio_softc *sc)
   4281 {
   4282 	mixer_devinfo_t mi;
   4283 	mixer_ctrl_t *mc;
   4284 
   4285 	KASSERT(mutex_owned(sc->sc_lock));
   4286 
   4287 	for (mi.index = 0; ; mi.index++) {
   4288 		if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) != 0)
   4289 			break;
   4290 		if (mi.type == AUDIO_MIXER_CLASS)
   4291 			continue;
   4292 		mc = &sc->sc_mixer_state[mi.index];
   4293 		(void)sc->hw_if->set_port(sc->hw_hdl, mc);
   4294 	}
   4295 	if (sc->hw_if->commit_settings)
   4296 		sc->hw_if->commit_settings(sc->hw_hdl);
   4297 
   4298 	return;
   4299 }
   4300 
   4301 #ifdef AUDIO_PM_IDLE
   4302 static void
   4303 audio_idle(void *arg)
   4304 {
   4305 	device_t dv = arg;
   4306 	struct audio_softc *sc = device_private(dv);
   4307 
   4308 #ifdef PNP_DEBUG
   4309 	extern int pnp_debug_idle;
   4310 	if (pnp_debug_idle)
   4311 		printf("%s: idle handler called\n", device_xname(dv));
   4312 #endif
   4313 
   4314 	sc->sc_idle = true;
   4315 
   4316 	/* XXX joerg Make pmf_device_suspend handle children? */
   4317 	if (!pmf_device_suspend(dv, PMF_Q_SELF))
   4318 		return;
   4319 
   4320 	if (!pmf_device_suspend(sc->sc_dev, PMF_Q_SELF))
   4321 		pmf_device_resume(dv, PMF_Q_SELF);
   4322 }
   4323 
   4324 static void
   4325 audio_activity(device_t dv, devactive_t type)
   4326 {
   4327 	struct audio_softc *sc = device_private(dv);
   4328 
   4329 	if (type != DVA_SYSTEM)
   4330 		return;
   4331 
   4332 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   4333 
   4334 	sc->sc_idle = false;
   4335 	if (!device_is_active(dv)) {
   4336 		/* XXX joerg How to deal with a failing resume... */
   4337 		pmf_device_resume(sc->sc_dev, PMF_Q_SELF);
   4338 		pmf_device_resume(dv, PMF_Q_SELF);
   4339 	}
   4340 }
   4341 #endif
   4342 
   4343 static bool
   4344 audio_suspend(device_t dv, const pmf_qual_t *qual)
   4345 {
   4346 	struct audio_softc *sc = device_private(dv);
   4347 	const struct audio_hw_if *hwp = sc->hw_if;
   4348 
   4349 	mutex_enter(sc->sc_lock);
   4350 	audio_mixer_capture(sc);
   4351 	mutex_enter(sc->sc_intr_lock);
   4352 	if (sc->sc_pbus == true)
   4353 		hwp->halt_output(sc->hw_hdl);
   4354 	if (sc->sc_rbus == true)
   4355 		hwp->halt_input(sc->hw_hdl);
   4356 	mutex_exit(sc->sc_intr_lock);
   4357 #ifdef AUDIO_PM_IDLE
   4358 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   4359 #endif
   4360 	mutex_exit(sc->sc_lock);
   4361 
   4362 	return true;
   4363 }
   4364 
   4365 static bool
   4366 audio_resume(device_t dv, const pmf_qual_t *qual)
   4367 {
   4368 	struct audio_softc *sc = device_private(dv);
   4369 
   4370 	mutex_enter(sc->sc_lock);
   4371 	if (sc->sc_lastinfovalid)
   4372 		audiosetinfo(sc, &sc->sc_lastinfo);
   4373 	audio_mixer_restore(sc);
   4374 	mutex_enter(sc->sc_intr_lock);
   4375 	if ((sc->sc_pbus == true) && !sc->sc_pr.pause)
   4376 		audiostartp(sc);
   4377 	if ((sc->sc_rbus == true) && !sc->sc_rr.pause)
   4378 		audiostartr(sc);
   4379 	mutex_exit(sc->sc_intr_lock);
   4380 	mutex_exit(sc->sc_lock);
   4381 
   4382 	return true;
   4383 }
   4384 
   4385 static void
   4386 audio_volume_down(device_t dv)
   4387 {
   4388 	struct audio_softc *sc = device_private(dv);
   4389 	mixer_devinfo_t mi;
   4390 	int newgain;
   4391 	u_int gain;
   4392 	u_char balance;
   4393 
   4394 	mutex_enter(sc->sc_lock);
   4395 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   4396 		mi.index = sc->sc_outports.master;
   4397 		mi.un.v.delta = 0;
   4398 		if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0) {
   4399 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   4400 			newgain = gain - mi.un.v.delta;
   4401 			if (newgain < AUDIO_MIN_GAIN)
   4402 				newgain = AUDIO_MIN_GAIN;
   4403 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   4404 		}
   4405 	}
   4406 	mutex_exit(sc->sc_lock);
   4407 }
   4408 
   4409 static void
   4410 audio_volume_up(device_t dv)
   4411 {
   4412 	struct audio_softc *sc = device_private(dv);
   4413 	mixer_devinfo_t mi;
   4414 	u_int gain, newgain;
   4415 	u_char balance;
   4416 
   4417 	mutex_enter(sc->sc_lock);
   4418 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   4419 		mi.index = sc->sc_outports.master;
   4420 		mi.un.v.delta = 0;
   4421 		if (sc->hw_if->query_devinfo(sc->hw_hdl, &mi) == 0) {
   4422 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   4423 			newgain = gain + mi.un.v.delta;
   4424 			if (newgain > AUDIO_MAX_GAIN)
   4425 				newgain = AUDIO_MAX_GAIN;
   4426 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   4427 		}
   4428 	}
   4429 	mutex_exit(sc->sc_lock);
   4430 }
   4431 
   4432 static void
   4433 audio_volume_toggle(device_t dv)
   4434 {
   4435 	struct audio_softc *sc = device_private(dv);
   4436 	u_int gain, newgain;
   4437 	u_char balance;
   4438 
   4439 	mutex_enter(sc->sc_lock);
   4440 	au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   4441 	if (gain != 0) {
   4442 		sc->sc_lastgain = gain;
   4443 		newgain = 0;
   4444 	} else
   4445 		newgain = sc->sc_lastgain;
   4446 	au_set_gain(sc, &sc->sc_outports, newgain, balance);
   4447 	mutex_exit(sc->sc_lock);
   4448 }
   4449 
   4450 static int
   4451 audio_get_props(struct audio_softc *sc)
   4452 {
   4453 	const struct audio_hw_if *hw;
   4454 	int props;
   4455 
   4456 	KASSERT(mutex_owned(sc->sc_lock));
   4457 
   4458 	hw = sc->hw_if;
   4459 	props = hw->get_props(sc->hw_hdl);
   4460 
   4461 	/*
   4462 	 * if neither playback nor capture properties are reported,
   4463 	 * assume both are supported by the device driver
   4464 	 */
   4465 	if ((props & (AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE)) == 0)
   4466 		props |= (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE);
   4467 
   4468 	return props;
   4469 }
   4470 
   4471 static bool
   4472 audio_can_playback(struct audio_softc *sc)
   4473 {
   4474 	return audio_get_props(sc) & AUDIO_PROP_PLAYBACK ? true : false;
   4475 }
   4476 
   4477 static bool
   4478 audio_can_capture(struct audio_softc *sc)
   4479 {
   4480 	return audio_get_props(sc) & AUDIO_PROP_CAPTURE ? true : false;
   4481 }
   4482 
   4483 #endif /* NAUDIO > 0 */
   4484