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