Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: audio.c,v 1.311 2017/02/24 16:53:24 nat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      5  * All rights reserved.
      6  *
      7  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      8  * All rights reserved.
      9  *
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Andrew Doran.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1991-1993 Regents of the University of California.
     37  * All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *	This product includes software developed by the Computer Systems
     50  *	Engineering Group at Lawrence Berkeley Laboratory.
     51  * 4. Neither the name of the University nor of the Laboratory may be used
     52  *    to endorse or promote products derived from this software without
     53  *    specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * This is a (partially) SunOS-compatible /dev/audio driver for NetBSD.
     70  *
     71  * This code tries to do something half-way sensible with
     72  * half-duplex hardware, such as with the SoundBlaster hardware.  With
     73  * half-duplex hardware allowing O_RDWR access doesn't really make
     74  * sense.  However, closing and opening the device to "turn around the
     75  * line" is relatively expensive and costs a card reset (which can
     76  * take some time, at least for the SoundBlaster hardware).  Instead
     77  * we allow O_RDWR access, and provide an ioctl to set the "mode",
     78  * i.e. playing or recording.
     79  *
     80  * If you write to a half-duplex device in record mode, the data is
     81  * tossed.  If you read from the device in play mode, you get silence
     82  * filled buffers at the rate at which samples are naturally
     83  * generated.
     84  *
     85  * If you try to set both play and record mode on a half-duplex
     86  * device, playing takes precedence.
     87  */
     88 
     89 /*
     90  * Locking: there are two locks.
     91  *
     92  * - sc_lock, provided by the underlying driver.  This is an adaptive lock,
     93  *   returned in the second parameter to hw_if->get_locks().  It is known
     94  *   as the "thread lock".
     95  *
     96  *   It serializes access to state in all places except the
     97  *   driver's interrupt service routine.  This lock is taken from process
     98  *   context (example: access to /dev/audio).  It is also taken from soft
     99  *   interrupt handlers in this module, primarily to serialize delivery of
    100  *   wakeups.  This lock may be used/provided by modules external to the
    101  *   audio subsystem, so take care not to introduce a lock order problem.
    102  *   LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
    103  *
    104  * - sc_intr_lock, provided by the underlying driver.  This may be either a
    105  *   spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
    106  *   IPL_SOFT*), returned in the first parameter to hw_if->get_locks().  It
    107  *   is known as the "interrupt lock".
    108  *
    109  *   It provides atomic access to the device's hardware state, and to audio
    110  *   channel data that may be accessed by the hardware driver's ISR.
    111  *   In all places outside the ISR, sc_lock must be held before taking
    112  *   sc_intr_lock.  This is to ensure that groups of hardware operations are
    113  *   made atomically.  SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
    114  *
    115  * List of hardware interface methods, and which locks are held when each
    116  * is called by this module:
    117  *
    118  *	METHOD			INTR	THREAD  NOTES
    119  *	----------------------- ------- -------	-------------------------
    120  *	open 			x	x
    121  *	close 			x	x
    122  *	drain 			x	x
    123  *	query_encoding		-	x
    124  *	set_params 		-	x
    125  *	round_blocksize		-	x
    126  *	commit_settings		-	x
    127  *	init_output 		x	x
    128  *	init_input 		x	x
    129  *	start_output 		x	x
    130  *	start_input 		x	x
    131  *	halt_output 		x	x
    132  *	halt_input 		x	x
    133  *	speaker_ctl 		x	x
    134  *	getdev 			-	x
    135  *	setfd 			-	x
    136  *	set_port 		-	x
    137  *	get_port 		-	x
    138  *	query_devinfo 		-	x
    139  *	allocm 			-	-	Called at attach time
    140  *	freem 			-	-	Called at attach time
    141  *	round_buffersize 	-	x
    142  *	mappage 		-	-	Mem. unchanged after attach
    143  *	get_props 		-	x
    144  *	trigger_output 		x	x
    145  *	trigger_input 		x	x
    146  *	dev_ioctl 		-	x
    147  *	get_locks 		-	-	Called at attach time
    148  */
    149 
    150 #include <sys/cdefs.h>
    151 __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.311 2017/02/24 16:53:24 nat Exp $");
    152 
    153 #include "audio.h"
    154 #if NAUDIO > 0
    155 
    156 #include <sys/types.h>
    157 #include <sys/param.h>
    158 #include <sys/ioctl.h>
    159 #include <sys/fcntl.h>
    160 #include <sys/file.h>
    161 #include <sys/filedesc.h>
    162 #include <sys/vnode.h>
    163 #include <sys/select.h>
    164 #include <sys/poll.h>
    165 #include <sys/kauth.h>
    166 #include <sys/kmem.h>
    167 #include <sys/malloc.h>
    168 #include <sys/proc.h>
    169 #include <sys/queue.h>
    170 #include <sys/stat.h>
    171 #include <sys/systm.h>
    172 #include <sys/sysctl.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/kthread.h>
    181 #include <sys/cpu.h>
    182 
    183 #include <dev/audio_if.h>
    184 #include <dev/audiovar.h>
    185 #include <dev/auconv.h>
    186 #include <dev/auvolconv.h>
    187 
    188 #include <machine/endian.h>
    189 
    190 /* #define AUDIO_DEBUG	1 */
    191 #ifdef AUDIO_DEBUG
    192 #define DPRINTF(x)	if (audiodebug) printf x
    193 #define DPRINTFN(n,x)	if (audiodebug>(n)) printf x
    194 int	audiodebug = AUDIO_DEBUG;
    195 #else
    196 #define DPRINTF(x)
    197 #define DPRINTFN(n,x)
    198 #endif
    199 
    200 #define ROUNDSIZE(x)	(x) &= -16	/* round to nice boundary */
    201 #define SPECIFIED(x)	((x) != ~0)
    202 #define SPECIFIED_CH(x)	((x) != (u_char)~0)
    203 
    204 /* #define AUDIO_PM_IDLE */
    205 #ifdef AUDIO_PM_IDLE
    206 int	audio_idle_timeout = 30;
    207 #endif
    208 
    209 #define HW_LOCK(x)	if ((x) == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc) \
    210 				mutex_enter(sc->sc_intr_lock);
    211 
    212 #define HW_UNLOCK(x)	if ((x) == SIMPLEQ_FIRST(&sc->sc_audiochan)->vc) \
    213 				mutex_exit(sc->sc_intr_lock);
    214 
    215 int	audio_blk_ms = AUDIO_BLK_MS;
    216 
    217 int	audiosetinfo(struct audio_softc *, struct audio_info *, bool,
    218 		     struct virtual_channel *);
    219 int	audiogetinfo(struct audio_softc *, struct audio_info *, int,
    220 		     struct virtual_channel *);
    221 
    222 int	audio_open(dev_t, struct audio_softc *, int, int, struct lwp *,
    223 		   struct file **);
    224 int	audio_close(struct audio_softc *, int, struct audio_chan *);
    225 int	audio_read(struct audio_softc *, struct uio *, int,
    226 		   struct virtual_channel *);
    227 int	audio_write(struct audio_softc *, struct uio *, int,
    228 		    struct virtual_channel *);
    229 int	audio_ioctl(dev_t, struct audio_softc *, u_long, void *, int,
    230 		    struct lwp *, struct audio_chan *);
    231 int	audio_poll(struct audio_softc *, int, struct lwp *,
    232 		   struct virtual_channel *);
    233 int	audio_kqfilter(struct audio_chan *, struct knote *);
    234 paddr_t audiommap(dev_t, off_t, int, struct virtual_channel *);
    235 paddr_t audio_mmap(struct audio_softc *, off_t, int, struct virtual_channel *);
    236 int	audio_fop_mmap(struct file *, off_t *, size_t, int, int *, int *,
    237 			     struct uvm_object **, int *);
    238 
    239 int	mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *,
    240 		   struct file **);
    241 int	mixer_close(struct audio_softc *, int, struct audio_chan *);
    242 int	mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
    243 static	void mixer_remove(struct audio_softc *);
    244 static	void mixer_signal(struct audio_softc *);
    245 static	void grow_mixer_states(struct audio_softc *, int);
    246 static	void shrink_mixer_states(struct audio_softc *, int);
    247 
    248 void	audio_init_record(struct audio_softc *, struct virtual_channel *);
    249 void	audio_init_play(struct audio_softc *, struct virtual_channel *);
    250 int	audiostartr(struct audio_softc *, struct virtual_channel *);
    251 int	audiostartp(struct audio_softc *, struct virtual_channel *);
    252 void	audio_rint(void *);
    253 void	audio_pint(void *);
    254 void	audio_mix(void *);
    255 void	audio_upmix(void *);
    256 void	audio_play_thread(void *);
    257 void	audio_rec_thread(void *);
    258 void	recswvol_func(struct audio_softc *, struct audio_ringbuffer *,
    259 		      size_t, struct virtual_channel *);
    260 void	recswvol_func8(struct audio_softc *, struct audio_ringbuffer *,
    261 		       size_t, struct virtual_channel *);
    262 void	recswvol_func16(struct audio_softc *, struct audio_ringbuffer *,
    263 			size_t, struct virtual_channel *);
    264 void	recswvol_func32(struct audio_softc *, struct audio_ringbuffer *,
    265 			size_t, struct virtual_channel *);
    266 void	saturate_func(struct audio_softc *);
    267 void	saturate_func8(struct audio_softc *);
    268 void	saturate_func16(struct audio_softc *);
    269 void	saturate_func32(struct audio_softc *);
    270 void	mix_func(struct audio_softc *, struct audio_ringbuffer *,
    271 		 struct virtual_channel *);
    272 void	mix_func8(struct audio_softc *, struct audio_ringbuffer *,
    273 		 struct virtual_channel *);
    274 void	mix_func16(struct audio_softc *, struct audio_ringbuffer *,
    275 		 struct virtual_channel *);
    276 void	mix_func32(struct audio_softc *, struct audio_ringbuffer *,
    277 		 struct virtual_channel *);
    278 void	mix_write(void *);
    279 void	mix_read(void *);
    280 int	audio_check_params(struct audio_params *);
    281 
    282 void	audio_calc_blksize(struct audio_softc *, int, struct virtual_channel *);
    283 void	audio_fill_silence(struct audio_params *, uint8_t *, int);
    284 int	audio_silence_copyout(struct audio_softc *, int, struct uio *);
    285 
    286 void	audio_init_ringbuffer(struct audio_softc *,
    287 			      struct audio_ringbuffer *, int);
    288 int	audio_initbufs(struct audio_softc *, struct virtual_channel *);
    289 void	audio_calcwater(struct audio_softc *, struct virtual_channel *);
    290 int	audio_drain(struct audio_softc *, struct audio_chan *);
    291 void	audio_clear(struct audio_softc *, struct virtual_channel *);
    292 void	audio_clear_intr_unlocked(struct audio_softc *sc,
    293 				  struct virtual_channel *);
    294 static inline void
    295 	audio_pint_silence(struct audio_softc *, struct audio_ringbuffer *,
    296 			   uint8_t *, int, struct virtual_channel *);
    297 int	audio_alloc_ring(struct audio_softc *, struct audio_ringbuffer *, int,
    298 			 size_t);
    299 void	audio_free_ring(struct audio_softc *, struct audio_ringbuffer *);
    300 static int audio_setup_pfilters(struct audio_softc *, const audio_params_t *,
    301 			      stream_filter_list_t *, struct virtual_channel *);
    302 static int audio_setup_rfilters(struct audio_softc *, const audio_params_t *,
    303 			      stream_filter_list_t *, struct virtual_channel *);
    304 static void audio_stream_dtor(audio_stream_t *);
    305 static int audio_stream_ctor(audio_stream_t *, const audio_params_t *, int);
    306 static void stream_filter_list_append(stream_filter_list_t *,
    307 		stream_filter_factory_t, const audio_params_t *);
    308 static void stream_filter_list_prepend(stream_filter_list_t *,
    309 	    	stream_filter_factory_t, const audio_params_t *);
    310 static void stream_filter_list_set(stream_filter_list_t *, int,
    311 		stream_filter_factory_t, const audio_params_t *);
    312 int	audio_set_defaults(struct audio_softc *, u_int,
    313 						struct virtual_channel *);
    314 static int audio_sysctl_frequency(SYSCTLFN_PROTO);
    315 static int audio_sysctl_precision(SYSCTLFN_PROTO);
    316 static int audio_sysctl_channels(SYSCTLFN_PROTO);
    317 
    318 int	audioprobe(device_t, cfdata_t, void *);
    319 void	audioattach(device_t, device_t, void *);
    320 int	audiodetach(device_t, int);
    321 int	audioactivate(device_t, enum devact);
    322 void	audiochilddet(device_t, device_t);
    323 int	audiorescan(device_t, const char *, const int *);
    324 
    325 #ifdef AUDIO_PM_IDLE
    326 static void	audio_idle(void *);
    327 static void	audio_activity(device_t, devactive_t);
    328 #endif
    329 
    330 static bool	audio_suspend(device_t dv, const pmf_qual_t *);
    331 static bool	audio_resume(device_t dv, const pmf_qual_t *);
    332 static void	audio_volume_down(device_t);
    333 static void	audio_volume_up(device_t);
    334 static void	audio_volume_toggle(device_t);
    335 
    336 static void	audio_mixer_capture(struct audio_softc *);
    337 static void	audio_mixer_restore(struct audio_softc *);
    338 
    339 static int	audio_get_props(struct audio_softc *);
    340 static bool	audio_can_playback(struct audio_softc *);
    341 static bool	audio_can_capture(struct audio_softc *);
    342 
    343 static void	audio_softintr_rd(void *);
    344 static void	audio_softintr_wr(void *);
    345 
    346 static int	audio_enter(dev_t, krw_t, struct audio_softc **);
    347 static void	audio_exit(struct audio_softc *);
    348 static int	audio_waitio(struct audio_softc *, kcondvar_t *,
    349 			     struct virtual_channel *);
    350 
    351 int audioclose(struct file *);
    352 int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    353 int audiowrite(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    354 int audioioctl(struct file *, u_long, void *);
    355 int audiopoll(struct file *, int);
    356 int audiokqfilter(struct file *, struct knote *);
    357 int audiostat(struct file *, struct stat *);
    358 
    359 struct portname {
    360 	const char *name;
    361 	int mask;
    362 };
    363 static const struct portname itable[] = {
    364 	{ AudioNmicrophone,	AUDIO_MICROPHONE },
    365 	{ AudioNline,		AUDIO_LINE_IN },
    366 	{ AudioNcd,		AUDIO_CD },
    367 	{ 0, 0 }
    368 };
    369 static const struct portname otable[] = {
    370 	{ AudioNspeaker,	AUDIO_SPEAKER },
    371 	{ AudioNheadphone,	AUDIO_HEADPHONE },
    372 	{ AudioNline,		AUDIO_LINE_OUT },
    373 	{ 0, 0 }
    374 };
    375 void	au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
    376 			mixer_devinfo_t *, const struct portname *);
    377 int	au_set_gain(struct audio_softc *, struct au_mixer_ports *,
    378 			int, int);
    379 void	au_get_gain(struct audio_softc *, struct au_mixer_ports *,
    380 			u_int *, u_char *);
    381 int	au_set_port(struct audio_softc *, struct au_mixer_ports *,
    382 			u_int);
    383 int	au_get_port(struct audio_softc *, struct au_mixer_ports *);
    384 static int
    385 	audio_get_port(struct audio_softc *, mixer_ctrl_t *);
    386 static int
    387 	audio_set_port(struct audio_softc *, mixer_ctrl_t *);
    388 static int
    389 	audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
    390 static int audio_set_params (struct audio_softc *, int, int,
    391 		 audio_params_t *, audio_params_t *,
    392 		 stream_filter_list_t *, stream_filter_list_t *,
    393 		 struct virtual_channel *);
    394 static int
    395 audio_query_encoding(struct audio_softc *, struct audio_encoding *);
    396 static int audio_set_vchan_defaults
    397 	(struct audio_softc *, u_int, const struct audio_format *);
    398 static int vchan_autoconfig(struct audio_softc *);
    399 int	au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
    400 int	au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
    401 int	au_portof(struct audio_softc *, char *, int);
    402 
    403 typedef struct uio_fetcher {
    404 	stream_fetcher_t base;
    405 	struct uio *uio;
    406 	int usedhigh;
    407 	int last_used;
    408 } uio_fetcher_t;
    409 
    410 static void	uio_fetcher_ctor(uio_fetcher_t *, struct uio *, int);
    411 static int	uio_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *,
    412 				     audio_stream_t *, int);
    413 static int	null_fetcher_fetch_to(struct audio_softc *, stream_fetcher_t *,
    414 				      audio_stream_t *, int);
    415 
    416 int audiobellopen(dev_t, int, int, struct lwp *, struct file **);
    417 
    418 dev_type_open(audioopen);
    419 
    420 const struct cdevsw audio_cdevsw = {
    421 	.d_open = audioopen,
    422 	.d_close = noclose,
    423 	.d_read = noread,
    424 	.d_write = nowrite,
    425 	.d_ioctl = noioctl,
    426 	.d_stop = nostop,
    427 	.d_tty = notty,
    428 	.d_poll = nopoll,
    429 	.d_mmap = nommap,
    430 	.d_kqfilter = nokqfilter,
    431 	.d_discard = nodiscard,
    432 	.d_flag = D_OTHER | D_MPSAFE
    433 };
    434 
    435 const struct fileops audio_fileops = {
    436 	.fo_read = audioread,
    437 	.fo_write = audiowrite,
    438 	.fo_ioctl = audioioctl,
    439 	.fo_fcntl = fnullop_fcntl,
    440 	.fo_stat = audiostat,
    441 	.fo_poll = audiopoll,
    442 	.fo_close = audioclose,
    443 	.fo_mmap = audio_fop_mmap,
    444 	.fo_kqfilter = audiokqfilter,
    445 	.fo_restart = fnullop_restart
    446 };
    447 
    448 /* The default audio mode: 8 kHz mono mu-law */
    449 const struct audio_params audio_default = {
    450 	.sample_rate = 8000,
    451 	.encoding = AUDIO_ENCODING_ULAW,
    452 	.precision = 8,
    453 	.validbits = 8,
    454 	.channels = 1,
    455 };
    456 
    457 int auto_config_precision[] = { 32, 24, 16, 8 };
    458 int auto_config_channels[] = { 32, 24, 16, 8, 6, 4, 2, 1};
    459 int auto_config_freq[] = { 48000, 44100, 96000, 192000, 32000,
    460 			   22050, 16000, 11025, 8000, 4000 };
    461 
    462 CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
    463     audioprobe, audioattach, audiodetach, audioactivate, audiorescan,
    464     audiochilddet, DVF_DETACH_SHUTDOWN);
    465 
    466 extern struct cfdriver audio_cd;
    467 
    468 int
    469 audioprobe(device_t parent, cfdata_t match, void *aux)
    470 {
    471 	struct audio_attach_args *sa;
    472 
    473 	sa = aux;
    474 	DPRINTF(("audioprobe: type=%d sa=%p hw=%p\n",
    475 		 sa->type, sa, sa->hwif));
    476 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
    477 }
    478 
    479 void
    480 audioattach(device_t parent, device_t self, void *aux)
    481 {
    482 	struct audio_softc *sc;
    483 	struct audio_attach_args *sa;
    484 	struct virtual_channel *vc;
    485 	struct audio_chan *chan;
    486 	const struct audio_hw_if *hwp;
    487 	const struct sysctlnode *node;
    488 	void *hdlp;
    489 	int error;
    490 	mixer_devinfo_t mi;
    491 	int iclass, mclass, oclass, rclass, props;
    492 	int record_master_found, record_source_found;
    493 	bool can_capture, can_playback;
    494 
    495 	sc = device_private(self);
    496 	sc->dev = self;
    497 	sa = aux;
    498 	hwp = sa->hwif;
    499 	hdlp = sa->hdl;
    500 	sc->sc_opens = 0;
    501 	sc->sc_recopens = 0;
    502 	sc->sc_aivalid = false;
    503  	sc->sc_ready = true;
    504 
    505  	sc->sc_format[0].mode = AUMODE_PLAY | AUMODE_RECORD;
    506  	sc->sc_format[0].encoding =
    507 #if BYTE_ORDER == LITTLE_ENDIAN
    508 		 AUDIO_ENCODING_SLINEAR_LE;
    509 #else
    510 		 AUDIO_ENCODING_SLINEAR_BE;
    511 #endif
    512  	sc->sc_format[0].precision = 16;
    513  	sc->sc_format[0].validbits = 16;
    514  	sc->sc_format[0].channels = 2;
    515  	sc->sc_format[0].channel_mask = AUFMT_STEREO;
    516  	sc->sc_format[0].frequency_type = 1;
    517  	sc->sc_format[0].frequency[0] = 44100;
    518 
    519 	sc->sc_vchan_params.sample_rate = 44100;
    520 #if BYTE_ORDER == LITTLE_ENDIAN
    521 	sc->sc_vchan_params.encoding = AUDIO_ENCODING_SLINEAR_LE;
    522 #else
    523 	sc->sc_vchan_params.encoding = AUDIO_ENCODING_SLINEAR_BE;
    524 #endif
    525 	sc->sc_vchan_params.precision = 16;
    526 	sc->sc_vchan_params.validbits = 16;
    527 	sc->sc_vchan_params.channels = 2;
    528 
    529 	sc->sc_trigger_started = false;
    530 	sc->sc_rec_started = false;
    531 	sc->sc_dying = false;
    532 	chan = kmem_zalloc(sizeof(struct audio_chan), KM_SLEEP);
    533 	vc = kmem_zalloc(sizeof(struct virtual_channel), KM_SLEEP);
    534 	chan->vc = vc;
    535 	SIMPLEQ_INIT(&sc->sc_audiochan);
    536 	SIMPLEQ_INSERT_HEAD(&sc->sc_audiochan, chan, entries);
    537 	vc->sc_open = 0;
    538 	vc->sc_mode = 0;
    539 	vc->sc_npfilters = 0;
    540 	memset(vc->sc_pfilters, 0,
    541 	    sizeof(vc->sc_pfilters));
    542 	vc->sc_lastinfovalid = false;
    543 	vc->sc_swvol = 255;
    544 	vc->sc_recswvol = 255;
    545 	sc->sc_iffreq = 44100;
    546 	sc->sc_precision = 16;
    547 	sc->sc_channels = 2;
    548 
    549 	if (auconv_create_encodings(sc->sc_format, VAUDIO_NFORMATS,
    550 	    &sc->sc_encodings) != 0) {
    551 		aprint_error_dev(self, "couldn't create encodings\n");
    552 		return;
    553 	}
    554 
    555 	cv_init(&sc->sc_rchan, "audiord");
    556 	cv_init(&sc->sc_wchan, "audiowr");
    557 	cv_init(&sc->sc_lchan, "audiolk");
    558 	cv_init(&sc->sc_condvar,"play");
    559 	cv_init(&sc->sc_rcondvar,"record");
    560 
    561 	if (hwp == 0 || hwp->get_locks == 0) {
    562 		aprint_error(": missing method\n");
    563 		panic("audioattach");
    564 	}
    565 
    566 	hwp->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
    567 
    568 #ifdef DIAGNOSTIC
    569 	if (hwp->query_encoding == 0 ||
    570 	    hwp->set_params == 0 ||
    571 	    (hwp->start_output == 0 && hwp->trigger_output == 0) ||
    572 	    (hwp->start_input == 0 && hwp->trigger_input == 0) ||
    573 	    hwp->halt_output == 0 ||
    574 	    hwp->halt_input == 0 ||
    575 	    hwp->getdev == 0 ||
    576 	    hwp->set_port == 0 ||
    577 	    hwp->get_port == 0 ||
    578 	    hwp->query_devinfo == 0 ||
    579 	    hwp->get_props == 0) {
    580 		aprint_error(": missing method\n");
    581 		sc->hw_if = NULL;
    582 		return;
    583 	}
    584 #endif
    585 
    586 	sc->hw_if = hwp;
    587 	sc->hw_hdl = hdlp;
    588 	sc->sc_dev = parent;
    589 
    590 	mutex_enter(sc->sc_lock);
    591 	props = audio_get_props(sc);
    592 	mutex_exit(sc->sc_lock);
    593 
    594 	if (props & AUDIO_PROP_FULLDUPLEX)
    595 		aprint_normal(": full duplex");
    596 	else
    597 		aprint_normal(": half duplex");
    598 
    599 	if (props & AUDIO_PROP_PLAYBACK)
    600 		aprint_normal(", playback");
    601 	if (props & AUDIO_PROP_CAPTURE)
    602 		aprint_normal(", capture");
    603 	if (props & AUDIO_PROP_MMAP)
    604 		aprint_normal(", mmap");
    605 	if (props & AUDIO_PROP_INDEPENDENT)
    606 		aprint_normal(", independent");
    607 
    608 	aprint_naive("\n");
    609 	aprint_normal("\n");
    610 
    611 	mutex_enter(sc->sc_lock);
    612 	can_playback = audio_can_playback(sc);
    613 	can_capture = audio_can_capture(sc);
    614 
    615 	if (can_playback) {
    616 		error = audio_alloc_ring(sc, &sc->sc_pr,
    617 	    	    AUMODE_PLAY, AU_RING_SIZE);
    618 		if (error)
    619 			goto bad_play;
    620 
    621 		error = audio_alloc_ring(sc, &vc->sc_mpr,
    622 	    	    AUMODE_PLAY, AU_RING_SIZE);
    623 bad_play:
    624 		if (error) {
    625 			if (sc->sc_pr.s.start != NULL)
    626 				audio_free_ring(sc, &sc->sc_pr);
    627 			sc->hw_if = NULL;
    628 			if (vc->sc_mpr.s.start != 0)
    629 				audio_free_ring(sc, &vc->sc_mpr);
    630 			sc->hw_if = NULL;
    631 			aprint_error_dev(sc->sc_dev, "could not allocate play "
    632 			    "buffer\n");
    633 			return;
    634 		}
    635 	}
    636 	if (can_capture) {
    637 		error = audio_alloc_ring(sc, &sc->sc_rr,
    638 		    AUMODE_RECORD, AU_RING_SIZE);
    639 		if (error)
    640 			goto bad_rec;
    641 
    642 		error = audio_alloc_ring(sc, &vc->sc_mrr,
    643 		    AUMODE_RECORD, AU_RING_SIZE);
    644 bad_rec:
    645 		if (error) {
    646 			if (vc->sc_mrr.s.start != NULL)
    647 				audio_free_ring(sc, &vc->sc_mrr);
    648 			if (sc->sc_pr.s.start != NULL)
    649 				audio_free_ring(sc, &sc->sc_pr);
    650 			if (vc->sc_mpr.s.start != 0)
    651 				audio_free_ring(sc, &vc->sc_mpr);
    652 			sc->hw_if = NULL;
    653 			aprint_error_dev(sc->sc_dev, "could not allocate record"
    654 			   " buffer\n");
    655 			return;
    656 		}
    657 	}
    658 
    659 	sc->sc_lastgain = 128;
    660 	sc->sc_saturate = true;
    661 	sc->sc_multiuser = false;
    662 	mutex_exit(sc->sc_lock);
    663 
    664 	error = vchan_autoconfig(sc);
    665 	if (error != 0) {
    666 		aprint_error_dev(sc->sc_dev, "%s: audio_set_vchan_defaults() "
    667 		    "failed\n", __func__);
    668 	}
    669 
    670 	sc->sc_pr.blksize = vc->sc_mpr.blksize;
    671 	sc->sc_rr.blksize = vc->sc_mrr.blksize;
    672 	sc->sc_sih_rd = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
    673 	    audio_softintr_rd, sc);
    674 	sc->sc_sih_wr = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
    675 	    audio_softintr_wr, sc);
    676 
    677 	iclass = mclass = oclass = rclass = -1;
    678 	sc->sc_inports.index = -1;
    679 	sc->sc_inports.master = -1;
    680 	sc->sc_inports.nports = 0;
    681 	sc->sc_inports.isenum = false;
    682 	sc->sc_inports.allports = 0;
    683 	sc->sc_inports.isdual = false;
    684 	sc->sc_inports.mixerout = -1;
    685 	sc->sc_inports.cur_port = -1;
    686 	sc->sc_outports.index = -1;
    687 	sc->sc_outports.master = -1;
    688 	sc->sc_outports.nports = 0;
    689 	sc->sc_outports.isenum = false;
    690 	sc->sc_outports.allports = 0;
    691 	sc->sc_outports.isdual = false;
    692 	sc->sc_outports.mixerout = -1;
    693 	sc->sc_outports.cur_port = -1;
    694 	sc->sc_monitor_port = -1;
    695 	/*
    696 	 * Read through the underlying driver's list, picking out the class
    697 	 * names from the mixer descriptions. We'll need them to decode the
    698 	 * mixer descriptions on the next pass through the loop.
    699 	 */
    700 	mutex_enter(sc->sc_lock);
    701 	for(mi.index = 0; ; mi.index++) {
    702 		if (audio_query_devinfo(sc, &mi) != 0)
    703 			break;
    704 		 /*
    705 		  * The type of AUDIO_MIXER_CLASS merely introduces a class.
    706 		  * All the other types describe an actual mixer.
    707 		  */
    708 		if (mi.type == AUDIO_MIXER_CLASS) {
    709 			if (strcmp(mi.label.name, AudioCinputs) == 0)
    710 				iclass = mi.mixer_class;
    711 			if (strcmp(mi.label.name, AudioCmonitor) == 0)
    712 				mclass = mi.mixer_class;
    713 			if (strcmp(mi.label.name, AudioCoutputs) == 0)
    714 				oclass = mi.mixer_class;
    715 			if (strcmp(mi.label.name, AudioCrecord) == 0)
    716 				rclass = mi.mixer_class;
    717 		}
    718 	}
    719 	mutex_exit(sc->sc_lock);
    720 
    721 	/* Allocate save area.  Ensure non-zero allocation. */
    722 	sc->sc_static_nmixer_states = mi.index;
    723 	sc->sc_static_nmixer_states++;
    724 	sc->sc_nmixer_states = sc->sc_static_nmixer_states;
    725 	sc->sc_mixer_state = kmem_zalloc(sizeof(mixer_ctrl_t) *
    726 	    (sc->sc_nmixer_states + 1), KM_SLEEP);
    727 
    728 	/*
    729 	 * This is where we assign each control in the "audio" model, to the
    730 	 * underlying "mixer" control.  We walk through the whole list once,
    731 	 * assigning likely candidates as we come across them.
    732 	 */
    733 	record_master_found = 0;
    734 	record_source_found = 0;
    735 	mutex_enter(sc->sc_lock);
    736 	for(mi.index = 0; ; mi.index++) {
    737 		if (audio_query_devinfo(sc, &mi) != 0)
    738 			break;
    739 		KASSERT(mi.index < sc->sc_nmixer_states);
    740 		if (mi.type == AUDIO_MIXER_CLASS)
    741 			continue;
    742 		if (mi.mixer_class == iclass) {
    743 			/*
    744 			 * AudioCinputs is only a fallback, when we don't
    745 			 * find what we're looking for in AudioCrecord, so
    746 			 * check the flags before accepting one of these.
    747 			 */
    748 			if (strcmp(mi.label.name, AudioNmaster) == 0
    749 			    && record_master_found == 0)
    750 				sc->sc_inports.master = mi.index;
    751 			if (strcmp(mi.label.name, AudioNsource) == 0
    752 			    && record_source_found == 0) {
    753 				if (mi.type == AUDIO_MIXER_ENUM) {
    754 				    int i;
    755 				    for(i = 0; i < mi.un.e.num_mem; i++)
    756 					if (strcmp(mi.un.e.member[i].label.name,
    757 						    AudioNmixerout) == 0)
    758 						sc->sc_inports.mixerout =
    759 						    mi.un.e.member[i].ord;
    760 				}
    761 				au_setup_ports(sc, &sc->sc_inports, &mi,
    762 				    itable);
    763 			}
    764 			if (strcmp(mi.label.name, AudioNdac) == 0 &&
    765 			    sc->sc_outports.master == -1)
    766 				sc->sc_outports.master = mi.index;
    767 		} else if (mi.mixer_class == mclass) {
    768 			if (strcmp(mi.label.name, AudioNmonitor) == 0)
    769 				sc->sc_monitor_port = mi.index;
    770 		} else if (mi.mixer_class == oclass) {
    771 			if (strcmp(mi.label.name, AudioNmaster) == 0)
    772 				sc->sc_outports.master = mi.index;
    773 			if (strcmp(mi.label.name, AudioNselect) == 0)
    774 				au_setup_ports(sc, &sc->sc_outports, &mi,
    775 				    otable);
    776 		} else if (mi.mixer_class == rclass) {
    777 			/*
    778 			 * These are the preferred mixers for the audio record
    779 			 * controls, so set the flags here, but don't check.
    780 			 */
    781 			if (strcmp(mi.label.name, AudioNmaster) == 0) {
    782 				sc->sc_inports.master = mi.index;
    783 				record_master_found = 1;
    784 			}
    785 #if 1	/* Deprecated. Use AudioNmaster. */
    786 			if (strcmp(mi.label.name, AudioNrecord) == 0) {
    787 				sc->sc_inports.master = mi.index;
    788 				record_master_found = 1;
    789 			}
    790 			if (strcmp(mi.label.name, AudioNvolume) == 0) {
    791 				sc->sc_inports.master = mi.index;
    792 				record_master_found = 1;
    793 			}
    794 #endif
    795 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    796 				if (mi.type == AUDIO_MIXER_ENUM) {
    797 				    int i;
    798 				    for(i = 0; i < mi.un.e.num_mem; i++)
    799 					if (strcmp(mi.un.e.member[i].label.name,
    800 						    AudioNmixerout) == 0)
    801 						sc->sc_inports.mixerout =
    802 						    mi.un.e.member[i].ord;
    803 				}
    804 				au_setup_ports(sc, &sc->sc_inports, &mi,
    805 				    itable);
    806 				record_source_found = 1;
    807 			}
    808 		}
    809 	}
    810 	mutex_exit(sc->sc_lock);
    811 	DPRINTF(("audio_attach: inputs ports=0x%x, input master=%d, "
    812 		 "output ports=0x%x, output master=%d\n",
    813 		 sc->sc_inports.allports, sc->sc_inports.master,
    814 		 sc->sc_outports.allports, sc->sc_outports.master));
    815 
    816 	/* sysctl set-up for alternate configs */
    817 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
    818 		0,
    819 		CTLTYPE_NODE, device_xname(sc->sc_dev),
    820 		SYSCTL_DESCR("audio format information"),
    821 		NULL, 0,
    822 		NULL, 0,
    823 		CTL_HW,
    824 		CTL_CREATE, CTL_EOL);
    825 
    826 	if (node != NULL) {
    827 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    828 			CTLFLAG_READWRITE,
    829 			CTLTYPE_INT, "frequency",
    830 			SYSCTL_DESCR("intermediate frequency"),
    831 			audio_sysctl_frequency, 0,
    832 			(void *)sc, 0,
    833 			CTL_HW, node->sysctl_num,
    834 			CTL_CREATE, CTL_EOL);
    835 
    836 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    837 			CTLFLAG_READWRITE,
    838 			CTLTYPE_INT, "precision",
    839 			SYSCTL_DESCR("intermediate precision"),
    840 			audio_sysctl_precision, 0,
    841 			(void *)sc, 0,
    842 			CTL_HW, node->sysctl_num,
    843 			CTL_CREATE, CTL_EOL);
    844 
    845 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    846 			CTLFLAG_READWRITE,
    847 			CTLTYPE_INT, "channels",
    848 			SYSCTL_DESCR("intermediate channels"),
    849 			audio_sysctl_channels, 0,
    850 			(void *)sc, 0,
    851 			CTL_HW, node->sysctl_num,
    852 			CTL_CREATE, CTL_EOL);
    853 
    854 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    855 			CTLFLAG_READWRITE,
    856 			CTLTYPE_BOOL, "saturate",
    857 			SYSCTL_DESCR("saturate to max. volume"),
    858 			NULL, 0,
    859 			&sc->sc_saturate, 0,
    860 			CTL_HW, node->sysctl_num,
    861 			CTL_CREATE, CTL_EOL);
    862 
    863 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    864 			CTLFLAG_READWRITE,
    865 			CTLTYPE_BOOL, "multiuser",
    866 			SYSCTL_DESCR("allow multiple user acess"),
    867 			NULL, 0,
    868 			&sc->sc_multiuser, 0,
    869 			CTL_HW, node->sysctl_num,
    870 			CTL_CREATE, CTL_EOL);
    871 	}
    872 
    873 	selinit(&sc->sc_rsel);
    874 	selinit(&sc->sc_wsel);
    875 
    876 #ifdef AUDIO_PM_IDLE
    877 	callout_init(&sc->sc_idle_counter, 0);
    878 	callout_setfunc(&sc->sc_idle_counter, audio_idle, self);
    879 #endif
    880 
    881 	if (!pmf_device_register(self, audio_suspend, audio_resume))
    882 		aprint_error_dev(self, "couldn't establish power handler\n");
    883 #ifdef AUDIO_PM_IDLE
    884 	if (!device_active_register(self, audio_activity))
    885 		aprint_error_dev(self, "couldn't register activity handler\n");
    886 #endif
    887 
    888 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
    889 	    audio_volume_down, true))
    890 		aprint_error_dev(self, "couldn't add volume down handler\n");
    891 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
    892 	    audio_volume_up, true))
    893 		aprint_error_dev(self, "couldn't add volume up handler\n");
    894 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE,
    895 	    audio_volume_toggle, true))
    896 		aprint_error_dev(self, "couldn't add volume toggle handler\n");
    897 
    898 #ifdef AUDIO_PM_IDLE
    899 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
    900 #endif
    901 	kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
    902 	    audio_rec_thread, sc, &sc->sc_recthread, "audiorec");
    903 	kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
    904 	    audio_play_thread, sc, &sc->sc_playthread, "audiomix");
    905 	audiorescan(self, "audio", NULL);
    906 }
    907 
    908 int
    909 audioactivate(device_t self, enum devact act)
    910 {
    911 	struct audio_softc *sc = device_private(self);
    912 
    913 	switch (act) {
    914 	case DVACT_DEACTIVATE:
    915 		mutex_enter(sc->sc_lock);
    916 		sc->sc_dying = true;
    917 		mutex_enter(sc->sc_intr_lock);
    918 		cv_broadcast(&sc->sc_condvar);
    919 		mutex_exit(sc->sc_intr_lock);
    920 		mutex_exit(sc->sc_lock);
    921 		return 0;
    922 	default:
    923 		return EOPNOTSUPP;
    924 	}
    925 }
    926 
    927 int
    928 audiodetach(device_t self, int flags)
    929 {
    930 	struct audio_softc *sc;
    931 	struct audio_chan *chan;
    932 	int maj, mn, i, rc;
    933 
    934 	sc = device_private(self);
    935 	DPRINTF(("audio_detach: sc=%p flags=%d\n", sc, flags));
    936 
    937 	/* Start draining existing accessors of the device. */
    938 	if ((rc = config_detach_children(self, flags)) != 0)
    939 		return rc;
    940 	mutex_enter(sc->sc_lock);
    941 	sc->sc_dying = true;
    942 	cv_broadcast(&sc->sc_wchan);
    943 	cv_broadcast(&sc->sc_rchan);
    944 	mutex_enter(sc->sc_intr_lock);
    945 	cv_broadcast(&sc->sc_condvar);
    946 	cv_broadcast(&sc->sc_rcondvar);
    947 	mutex_exit(sc->sc_intr_lock);
    948 	mutex_exit(sc->sc_lock);
    949 	kthread_join(sc->sc_playthread);
    950 	kthread_join(sc->sc_recthread);
    951 	mutex_enter(sc->sc_lock);
    952 	cv_destroy(&sc->sc_condvar);
    953 	cv_destroy(&sc->sc_rcondvar);
    954 	mutex_exit(sc->sc_lock);
    955 
    956 	/* delete sysctl nodes */
    957 	sysctl_teardown(&sc->sc_log);
    958 
    959 	/* locate the major number */
    960 	maj = cdevsw_lookup_major(&audio_cdevsw);
    961 
    962 	/*
    963 	 * Nuke the vnodes for any open instances (calls close).
    964 	 * Will wait until any activity on the device nodes has ceased.
    965 	 *
    966 	 * XXXAD NOT YET.
    967 	 *
    968 	 * XXXAD NEED TO PREVENT NEW REFERENCES THROUGH AUDIO_ENTER().
    969 	 */
    970 	mn = device_unit(self);
    971 	vdevgone(maj, mn | SOUND_DEVICE,    mn | SOUND_DEVICE, VCHR);
    972 	vdevgone(maj, mn | AUDIO_DEVICE,    mn | AUDIO_DEVICE, VCHR);
    973 	vdevgone(maj, mn | AUDIOCTL_DEVICE, mn | AUDIOCTL_DEVICE, VCHR);
    974 	vdevgone(maj, mn | MIXER_DEVICE,    mn | MIXER_DEVICE, VCHR);
    975 
    976 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN,
    977 	    audio_volume_down, true);
    978 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP,
    979 	    audio_volume_up, true);
    980 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE,
    981 	    audio_volume_toggle, true);
    982 
    983 #ifdef AUDIO_PM_IDLE
    984 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
    985 
    986 	device_active_deregister(self, audio_activity);
    987 #endif
    988 
    989 	pmf_device_deregister(self);
    990 
    991 	/* free resources */
    992 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
    993 		if (chan == NULL)
    994 			break;
    995 
    996 		if (chan->chan == MIXER_INUSE)
    997 			continue;
    998 		audio_free_ring(sc, &chan->vc->sc_mpr);
    999 		audio_free_ring(sc, &chan->vc->sc_mrr);
   1000 	}
   1001 	audio_free_ring(sc, &sc->sc_pr);
   1002 	audio_free_ring(sc, &sc->sc_rr);
   1003 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
   1004 		if (chan == NULL)
   1005 			break;
   1006 
   1007 		if (chan->chan == MIXER_INUSE)
   1008 			continue;
   1009 		for (i = 0; i < chan->vc->sc_npfilters; i++) {
   1010 			chan->vc->sc_pfilters[i]->dtor
   1011 			    (chan->vc->sc_pfilters[i]);
   1012 			chan->vc->sc_pfilters[i] = NULL;
   1013 			audio_stream_dtor(&chan->vc->sc_pstreams[i]);
   1014 		}
   1015 		chan->vc->sc_npfilters = 0;
   1016 
   1017 		for (i = 0; i < chan->vc->sc_nrfilters; i++) {
   1018 			chan->vc->sc_rfilters[i]->dtor
   1019 			    (chan->vc->sc_rfilters[i]);
   1020 			chan->vc->sc_rfilters[i] = NULL;
   1021 			audio_stream_dtor(&chan->vc->sc_rstreams[i]);
   1022 		}
   1023 		chan->vc->sc_nrfilters = 0;
   1024 	}
   1025 
   1026 	auconv_delete_encodings(sc->sc_encodings);
   1027 
   1028 	if (sc->sc_sih_rd) {
   1029 		softint_disestablish(sc->sc_sih_rd);
   1030 		sc->sc_sih_rd = NULL;
   1031 	}
   1032 	if (sc->sc_sih_wr) {
   1033 		softint_disestablish(sc->sc_sih_wr);
   1034 		sc->sc_sih_wr = NULL;
   1035 	}
   1036 
   1037 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   1038 	kmem_free(chan->vc, sizeof(struct virtual_channel));
   1039 	SIMPLEQ_REMOVE(&sc->sc_audiochan, chan, audio_chan, entries);
   1040 	kmem_free(chan, sizeof(struct audio_chan));
   1041 	kmem_free(sc->sc_mixer_state, sizeof(mixer_ctrl_t) *
   1042 	    (sc->sc_nmixer_states + 1));
   1043 
   1044 #ifdef AUDIO_PM_IDLE
   1045 	callout_destroy(&sc->sc_idle_counter);
   1046 #endif
   1047 	seldestroy(&sc->sc_rsel);
   1048 	seldestroy(&sc->sc_wsel);
   1049 
   1050 	cv_destroy(&sc->sc_rchan);
   1051 	cv_destroy(&sc->sc_wchan);
   1052 	cv_destroy(&sc->sc_lchan);
   1053 
   1054 	return 0;
   1055 }
   1056 
   1057 void
   1058 audiochilddet(device_t self, device_t child)
   1059 {
   1060 
   1061 	/* we hold no child references, so do nothing */
   1062 }
   1063 
   1064 static int
   1065 audiosearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
   1066 {
   1067 
   1068 	if (config_match(parent, cf, aux))
   1069 		config_attach_loc(parent, cf, locs, aux, NULL);
   1070 
   1071 	return 0;
   1072 }
   1073 
   1074 int
   1075 audiorescan(device_t self, const char *ifattr, const int *flags)
   1076 {
   1077 	struct audio_softc *sc = device_private(self);
   1078 
   1079 	if (!ifattr_match(ifattr, "audio"))
   1080 		return 0;
   1081 
   1082 	config_search_loc(audiosearch, sc->dev, "audio", NULL, NULL);
   1083 
   1084 	return 0;
   1085 }
   1086 
   1087 
   1088 int
   1089 au_portof(struct audio_softc *sc, char *name, int class)
   1090 {
   1091 	mixer_devinfo_t mi;
   1092 
   1093 	for (mi.index = 0; audio_query_devinfo(sc, &mi) == 0; mi.index++) {
   1094 		if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
   1095 			return mi.index;
   1096 	}
   1097 	return -1;
   1098 }
   1099 
   1100 void
   1101 au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
   1102 	       mixer_devinfo_t *mi, const struct portname *tbl)
   1103 {
   1104 	int i, j;
   1105 
   1106 	ports->index = mi->index;
   1107 	if (mi->type == AUDIO_MIXER_ENUM) {
   1108 		ports->isenum = true;
   1109 		for(i = 0; tbl[i].name; i++)
   1110 		    for(j = 0; j < mi->un.e.num_mem; j++)
   1111 			if (strcmp(mi->un.e.member[j].label.name,
   1112 						    tbl[i].name) == 0) {
   1113 				ports->allports |= tbl[i].mask;
   1114 				ports->aumask[ports->nports] = tbl[i].mask;
   1115 				ports->misel[ports->nports] =
   1116 				    mi->un.e.member[j].ord;
   1117 				ports->miport[ports->nports] =
   1118 				    au_portof(sc, mi->un.e.member[j].label.name,
   1119 				    mi->mixer_class);
   1120 				if (ports->mixerout != -1 &&
   1121 				    ports->miport[ports->nports] != -1)
   1122 					ports->isdual = true;
   1123 				++ports->nports;
   1124 			}
   1125 	} else if (mi->type == AUDIO_MIXER_SET) {
   1126 		for(i = 0; tbl[i].name; i++)
   1127 		    for(j = 0; j < mi->un.s.num_mem; j++)
   1128 			if (strcmp(mi->un.s.member[j].label.name,
   1129 						tbl[i].name) == 0) {
   1130 				ports->allports |= tbl[i].mask;
   1131 				ports->aumask[ports->nports] = tbl[i].mask;
   1132 				ports->misel[ports->nports] =
   1133 				    mi->un.s.member[j].mask;
   1134 				ports->miport[ports->nports] =
   1135 				    au_portof(sc, mi->un.s.member[j].label.name,
   1136 				    mi->mixer_class);
   1137 				++ports->nports;
   1138 			}
   1139 	}
   1140 }
   1141 
   1142 /*
   1143  * Called from hardware driver.  This is where the MI audio driver gets
   1144  * probed/attached to the hardware driver.
   1145  */
   1146 device_t
   1147 audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev)
   1148 {
   1149 	struct audio_attach_args arg;
   1150 
   1151 #ifdef DIAGNOSTIC
   1152 	if (ahwp == NULL) {
   1153 		aprint_error("audio_attach_mi: NULL\n");
   1154 		return 0;
   1155 	}
   1156 #endif
   1157 	arg.type = AUDIODEV_TYPE_AUDIO;
   1158 	arg.hwif = ahwp;
   1159 	arg.hdl = hdlp;
   1160 	return config_found(dev, &arg, audioprint);
   1161 }
   1162 
   1163 #ifdef AUDIO_DEBUG
   1164 void	audio_printsc(struct audio_softc *);
   1165 void	audio_print_params(const char *, struct audio_params *);
   1166 
   1167 void
   1168 audio_printsc(struct audio_softc *sc)
   1169 {
   1170 	struct audio_chan *chan;
   1171 
   1172 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   1173 
   1174 	if (chan == NULL)
   1175 		return;
   1176 
   1177 	printf("hwhandle %p hw_if %p ", sc->hw_hdl, sc->hw_if);
   1178 	printf("open 0x%x mode 0x%x\n", chan->vc->sc_open,
   1179 	    chan->vc->sc_mode);
   1180 	printf("rchan 0x%x wchan 0x%x ", cv_has_waiters(&sc->sc_rchan),
   1181 	    cv_has_waiters(&sc->sc_wchan));
   1182 	printf("rring used 0x%x pring used=%d\n",
   1183 	       audio_stream_get_used(&chan->vc->sc_mrr.s),
   1184 	       audio_stream_get_used(&chan->vc->sc_mpr.s));
   1185 	printf("rbus 0x%x pbus 0x%x ", chan->vc->sc_rbus,
   1186 	    chan->vc->sc_pbus);
   1187 	printf("blksize %d", chan->vc->sc_mpr.blksize);
   1188 	printf("hiwat %d lowat %d\n", chan->vc->sc_mpr.usedhigh,
   1189 	    chan->vc->sc_mpr.usedlow);
   1190 }
   1191 
   1192 void
   1193 audio_print_params(const char *s, struct audio_params *p)
   1194 {
   1195 	printf("%s enc=%u %uch %u/%ubit %uHz\n", s, p->encoding, p->channels,
   1196 	       p->validbits, p->precision, p->sample_rate);
   1197 }
   1198 #endif
   1199 
   1200 int
   1201 audio_alloc_ring(struct audio_softc *sc, struct audio_ringbuffer *r,
   1202 		 int direction, size_t bufsize)
   1203 {
   1204 	const struct audio_hw_if *hw;
   1205 	struct audio_chan *chan;
   1206 	void *hdl;
   1207 
   1208 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   1209 	hw = sc->hw_if;
   1210 	hdl = sc->hw_hdl;
   1211 	/*
   1212 	 * Alloc DMA play and record buffers
   1213 	 */
   1214 	if (bufsize < AUMINBUF)
   1215 		bufsize = AUMINBUF;
   1216 	ROUNDSIZE(bufsize);
   1217 	if (hw->round_buffersize) {
   1218 		bufsize = hw->round_buffersize(hdl, direction, bufsize);
   1219 	}
   1220 	if (hw->allocm && (r == &chan->vc->sc_mpr || r == &chan->vc->sc_mrr))
   1221 		r->s.start = hw->allocm(hdl, direction, bufsize);
   1222 	else
   1223 		r->s.start = kmem_zalloc(bufsize, KM_SLEEP);
   1224 	if (r->s.start == NULL)
   1225 		return ENOMEM;
   1226 	r->s.bufsize = bufsize;
   1227 
   1228 	return 0;
   1229 }
   1230 
   1231 void
   1232 audio_free_ring(struct audio_softc *sc, struct audio_ringbuffer *r)
   1233 {
   1234 	struct audio_chan *chan;
   1235 
   1236 	if (r->s.start == NULL)
   1237 		return;
   1238 
   1239 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   1240 
   1241 	if (sc->hw_if->freem && (r == &chan->vc->sc_mpr ||
   1242 						r == &chan->vc->sc_mrr))
   1243 		sc->hw_if->freem(sc->hw_hdl, r->s.start, r->s.bufsize);
   1244 	else
   1245 		kmem_free(r->s.start, r->s.bufsize);
   1246 	r->s.start = NULL;
   1247 }
   1248 
   1249 static int
   1250 audio_setup_pfilters(struct audio_softc *sc, const audio_params_t *pp,
   1251 		     stream_filter_list_t *pfilters, struct virtual_channel *vc)
   1252 {
   1253 	stream_filter_t *pf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS];
   1254 	audio_stream_t ps[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS];
   1255 	const audio_params_t *from_param;
   1256 	audio_params_t *to_param;
   1257 	int i, n, onfilters;
   1258 
   1259 	KASSERT(mutex_owned(sc->sc_lock));
   1260 
   1261 	/* Construct new filters. */
   1262 	memset(pf, 0, sizeof(pf));
   1263 	memset(ps, 0, sizeof(ps));
   1264 	from_param = pp;
   1265 	for (i = 0; i < pfilters->req_size; i++) {
   1266 		n = pfilters->req_size - i - 1;
   1267 		to_param = &pfilters->filters[n].param;
   1268 		audio_check_params(to_param);
   1269 		pf[i] = pfilters->filters[n].factory(sc, from_param, to_param);
   1270 		if (pf[i] == NULL)
   1271 			break;
   1272 		if (audio_stream_ctor(&ps[i], from_param, AU_RING_SIZE))
   1273 			break;
   1274 		if (i > 0)
   1275 			pf[i]->set_fetcher(pf[i], &pf[i - 1]->base);
   1276 		from_param = to_param;
   1277 	}
   1278 	if (i < pfilters->req_size) { /* failure */
   1279 		DPRINTF(("%s: pfilters failure\n", __func__));
   1280 		for (; i >= 0; i--) {
   1281 			if (pf[i] != NULL)
   1282 				pf[i]->dtor(pf[i]);
   1283 			audio_stream_dtor(&ps[i]);
   1284 		}
   1285 		return EINVAL;
   1286 	}
   1287 
   1288 	/* Swap in new filters. */
   1289 	HW_LOCK(vc);
   1290 	memcpy(of, vc->sc_pfilters, sizeof(of));
   1291 	memcpy(os, vc->sc_pstreams, sizeof(os));
   1292 	onfilters = vc->sc_npfilters;
   1293 	memcpy(vc->sc_pfilters, pf, sizeof(pf));
   1294 	memcpy(vc->sc_pstreams, ps, sizeof(ps));
   1295 	vc->sc_npfilters = pfilters->req_size;
   1296 	for (i = 0; i < pfilters->req_size; i++)
   1297 		pf[i]->set_inputbuffer(pf[i], &vc->sc_pstreams[i]);
   1298 
   1299 	/* hardware format and the buffer near to userland */
   1300 	if (pfilters->req_size <= 0) {
   1301 		vc->sc_mpr.s.param = *pp;
   1302 		vc->sc_pustream = &vc->sc_mpr.s;
   1303 	} else {
   1304 		vc->sc_mpr.s.param = pfilters->filters[0].param;
   1305 		vc->sc_pustream = &vc->sc_pstreams[0];
   1306 	}
   1307 	HW_UNLOCK(vc);
   1308 
   1309 	/* Destroy old filters. */
   1310 	for (i = 0; i < onfilters; i++) {
   1311 		of[i]->dtor(of[i]);
   1312 		audio_stream_dtor(&os[i]);
   1313 	}
   1314 
   1315 #ifdef AUDIO_DEBUG
   1316 	printf("%s: HW-buffer=%p pustream=%p\n",
   1317 	       __func__, &vc->sc_mpr.s, vc->sc_pustream);
   1318 	for (i = 0; i < pfilters->req_size; i++) {
   1319 		char num[100];
   1320 		snprintf(num, 100, "[%d]", i);
   1321 		audio_print_params(num, &vc->sc_pstreams[i].param);
   1322 	}
   1323 	audio_print_params("[HW]", &vc->sc_mpr.s.param);
   1324 #endif /* AUDIO_DEBUG */
   1325 
   1326 	return 0;
   1327 }
   1328 
   1329 static int
   1330 audio_setup_rfilters(struct audio_softc *sc, const audio_params_t *rp,
   1331 		     stream_filter_list_t *rfilters, struct virtual_channel *vc)
   1332 {
   1333 	stream_filter_t *rf[AUDIO_MAX_FILTERS], *of[AUDIO_MAX_FILTERS];
   1334 	audio_stream_t rs[AUDIO_MAX_FILTERS], os[AUDIO_MAX_FILTERS];
   1335 	const audio_params_t *to_param;
   1336 	audio_params_t *from_param;
   1337 	int i, onfilters;
   1338 
   1339 	KASSERT(mutex_owned(sc->sc_lock));
   1340 
   1341 	/* Construct new filters. */
   1342 	memset(rf, 0, sizeof(rf));
   1343 	memset(rs, 0, sizeof(rs));
   1344 	for (i = 0; i < rfilters->req_size; i++) {
   1345 		from_param = &rfilters->filters[i].param;
   1346 		audio_check_params(from_param);
   1347 		to_param = i + 1 < rfilters->req_size
   1348 			? &rfilters->filters[i + 1].param : rp;
   1349 		rf[i] = rfilters->filters[i].factory(sc, from_param, to_param);
   1350 		if (rf[i] == NULL)
   1351 			break;
   1352 		if (audio_stream_ctor(&rs[i], to_param, AU_RING_SIZE))
   1353 			break;
   1354 		if (i > 0) {
   1355 			rf[i]->set_fetcher(rf[i], &rf[i - 1]->base);
   1356 		} else {
   1357 			/* rf[0] has no previous fetcher because
   1358 			 * the audio hardware fills data to the
   1359 			 * input buffer. */
   1360 			rf[0]->set_inputbuffer(rf[0], &vc->sc_mrr.s);
   1361 		}
   1362 	}
   1363 	if (i < rfilters->req_size) { /* failure */
   1364 		DPRINTF(("%s: rfilters failure\n", __func__));
   1365 		for (; i >= 0; i--) {
   1366 			if (rf[i] != NULL)
   1367 				rf[i]->dtor(rf[i]);
   1368 			audio_stream_dtor(&rs[i]);
   1369 		}
   1370 		return EINVAL;
   1371 	}
   1372 
   1373 	/* Swap in new filters. */
   1374 	HW_LOCK(vc);
   1375 	memcpy(of, vc->sc_rfilters, sizeof(of));
   1376 	memcpy(os, vc->sc_rstreams, sizeof(os));
   1377 	onfilters = vc->sc_nrfilters;
   1378 	memcpy(vc->sc_rfilters, rf, sizeof(rf));
   1379 	memcpy(vc->sc_rstreams, rs, sizeof(rs));
   1380 	vc->sc_nrfilters = rfilters->req_size;
   1381 	for (i = 1; i < rfilters->req_size; i++)
   1382 		rf[i]->set_inputbuffer(rf[i], &vc->sc_rstreams[i - 1]);
   1383 
   1384 	/* hardware format and the buffer near to userland */
   1385 	if (rfilters->req_size <= 0) {
   1386 		vc->sc_mrr.s.param = *rp;
   1387 		vc->sc_rustream = &vc->sc_mrr.s;
   1388 	} else {
   1389 		vc->sc_mrr.s.param = rfilters->filters[0].param;
   1390 		vc->sc_rustream = &vc->sc_rstreams[rfilters->req_size - 1];
   1391 	}
   1392 	HW_UNLOCK(vc);
   1393 
   1394 #ifdef AUDIO_DEBUG
   1395 	printf("%s: HW-buffer=%p pustream=%p\n",
   1396 	       __func__, &vc->sc_mrr.s, vc->sc_rustream);
   1397 	audio_print_params("[HW]", &vc->sc_mrr.s.param);
   1398 	for (i = 0; i < rfilters->req_size; i++) {
   1399 		char num[100];
   1400 		snprintf(num, 100, "[%d]", i);
   1401 		audio_print_params(num, &vc->sc_rstreams[i].param);
   1402 	}
   1403 #endif /* AUDIO_DEBUG */
   1404 
   1405 	/* Destroy old filters. */
   1406 	for (i = 0; i < onfilters; i++) {
   1407 		of[i]->dtor(of[i]);
   1408 		audio_stream_dtor(&os[i]);
   1409 	}
   1410 
   1411 	return 0;
   1412 }
   1413 
   1414 static void
   1415 audio_stream_dtor(audio_stream_t *stream)
   1416 {
   1417 
   1418 	if (stream->start != NULL)
   1419 		kmem_free(stream->start, stream->bufsize);
   1420 	memset(stream, 0, sizeof(audio_stream_t));
   1421 }
   1422 
   1423 static int
   1424 audio_stream_ctor(audio_stream_t *stream, const audio_params_t *param, int size)
   1425 {
   1426 	int frame_size;
   1427 
   1428 	size = min(size, AU_RING_SIZE);
   1429 	stream->bufsize = size;
   1430 	stream->start = kmem_zalloc(size, KM_SLEEP);
   1431 	frame_size = (param->precision + 7) / 8 * param->channels;
   1432 	size = (size / frame_size) * frame_size;
   1433 	stream->end = stream->start + size;
   1434 	stream->inp = stream->start;
   1435 	stream->outp = stream->start;
   1436 	stream->used = 0;
   1437 	stream->param = *param;
   1438 	stream->loop = false;
   1439 	return 0;
   1440 }
   1441 
   1442 static void
   1443 stream_filter_list_append(stream_filter_list_t *list,
   1444 			  stream_filter_factory_t factory,
   1445 			  const audio_params_t *param)
   1446 {
   1447 
   1448 	if (list->req_size >= AUDIO_MAX_FILTERS) {
   1449 		printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n",
   1450 		       __func__);
   1451 		return;
   1452 	}
   1453 	list->filters[list->req_size].factory = factory;
   1454 	list->filters[list->req_size].param = *param;
   1455 	list->req_size++;
   1456 }
   1457 
   1458 static void
   1459 stream_filter_list_set(stream_filter_list_t *list, int i,
   1460 		       stream_filter_factory_t factory,
   1461 		       const audio_params_t *param)
   1462 {
   1463 
   1464 	if (i < 0 || i >= AUDIO_MAX_FILTERS) {
   1465 		printf("%s: invalid index: %d\n", __func__, i);
   1466 		return;
   1467 	}
   1468 
   1469 	list->filters[i].factory = factory;
   1470 	list->filters[i].param = *param;
   1471 	if (list->req_size <= i)
   1472 		list->req_size = i + 1;
   1473 }
   1474 
   1475 static void
   1476 stream_filter_list_prepend(stream_filter_list_t *list,
   1477 			   stream_filter_factory_t factory,
   1478 			   const audio_params_t *param)
   1479 {
   1480 
   1481 	if (list->req_size >= AUDIO_MAX_FILTERS) {
   1482 		printf("%s: increase AUDIO_MAX_FILTERS in sys/dev/audio_if.h\n",
   1483 		       __func__);
   1484 		return;
   1485 	}
   1486 	memmove(&list->filters[1], &list->filters[0],
   1487 		sizeof(struct stream_filter_req) * list->req_size);
   1488 	list->filters[0].factory = factory;
   1489 	list->filters[0].param = *param;
   1490 	list->req_size++;
   1491 }
   1492 
   1493 /*
   1494  * Look up audio device and acquire locks for device access.
   1495  */
   1496 static int
   1497 audio_enter(dev_t dev, krw_t rw, struct audio_softc **scp)
   1498 {
   1499 
   1500 	struct audio_softc *sc;
   1501 
   1502 	/* First, find the device and take sc_lock. */
   1503 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1504 	if (sc == NULL || sc->hw_if == NULL)
   1505 		return ENXIO;
   1506 	mutex_enter(sc->sc_lock);
   1507 	if (sc->sc_dying) {
   1508 		mutex_exit(sc->sc_lock);
   1509 		return EIO;
   1510 	}
   1511 
   1512 	*scp = sc;
   1513 	return 0;
   1514 }
   1515 
   1516 /*
   1517  * Release reference to device acquired with audio_enter().
   1518  */
   1519 static void
   1520 audio_exit(struct audio_softc *sc)
   1521 {
   1522 	cv_broadcast(&sc->sc_lchan);
   1523 	mutex_exit(sc->sc_lock);
   1524 }
   1525 
   1526 /*
   1527  * Wait for I/O to complete, releasing device lock.
   1528  */
   1529 static int
   1530 audio_waitio(struct audio_softc *sc, kcondvar_t *chan, struct virtual_channel *vc)
   1531 {
   1532 	struct audio_chan *vchan;
   1533 	bool found = false;
   1534 	int error;
   1535 
   1536 	KASSERT(mutex_owned(sc->sc_lock));
   1537 	cv_broadcast(&sc->sc_lchan);
   1538 
   1539 	/* Wait for pending I/O to complete. */
   1540 	error = cv_wait_sig(chan, sc->sc_lock);
   1541 
   1542 	found = false;
   1543 	SIMPLEQ_FOREACH(vchan, &sc->sc_audiochan, entries) {
   1544 		if (vchan->vc == vc) {
   1545 			found = true;
   1546 			break;
   1547 		}
   1548 	}
   1549 	if (found == false)
   1550 		error = EIO;
   1551 
   1552 	return error;
   1553 }
   1554 
   1555 int
   1556 audiobellopen(dev_t dev, int flags, int ifmt, struct lwp *l,
   1557 	      struct file **fp)
   1558 {
   1559 	struct audio_softc *sc;
   1560 	int error;
   1561 
   1562 	if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0)
   1563 		return error;
   1564 	device_active(sc->dev, DVA_SYSTEM);
   1565 	switch (AUDIODEV(dev)) {
   1566 	case AUDIO_DEVICE:
   1567 		error = audio_open(dev, sc, flags, ifmt, l, fp);
   1568 		break;
   1569 	default:
   1570 		error = EINVAL;
   1571 		break;
   1572 	}
   1573 	audio_exit(sc);
   1574 
   1575 	return error;
   1576 }
   1577 int
   1578 audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1579 {
   1580 	struct audio_softc *sc;
   1581 	struct file *fp;
   1582 	int error;
   1583 
   1584 	if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0)
   1585 		return error;
   1586 	device_active(sc->dev, DVA_SYSTEM);
   1587 	switch (AUDIODEV(dev)) {
   1588 	case SOUND_DEVICE:
   1589 	case AUDIO_DEVICE:
   1590 	case AUDIOCTL_DEVICE:
   1591 		error = audio_open(dev, sc, flags, ifmt, l, &fp);
   1592 		break;
   1593 	case MIXER_DEVICE:
   1594 		error = mixer_open(dev, sc, flags, ifmt, l, &fp);
   1595 		break;
   1596 	default:
   1597 		error = ENXIO;
   1598 		break;
   1599 	}
   1600 	audio_exit(sc);
   1601 
   1602 	return error;
   1603 }
   1604 
   1605 int
   1606 audioclose(struct file *fp)
   1607 {
   1608 	struct audio_softc *sc;
   1609 	struct audio_chan *chan;
   1610 	int error;
   1611 	dev_t dev;
   1612 
   1613 	chan = fp->f_audioctx;
   1614 	if (chan == NULL)	/* XXX:NS Why is this needed. */
   1615 		return EIO;
   1616 
   1617 	dev = chan->dev;
   1618 
   1619 	if ((error = audio_enter(dev, RW_WRITER, &sc)) != 0)
   1620 		return error;
   1621 
   1622 	device_active(sc->dev, DVA_SYSTEM);
   1623 	switch (AUDIODEV(dev)) {
   1624 	case SOUND_DEVICE:
   1625 	case AUDIO_DEVICE:
   1626 	case AUDIOCTL_DEVICE:
   1627 		error = audio_close(sc, fp->f_flag, chan);
   1628 		break;
   1629 	case MIXER_DEVICE:
   1630 		error = mixer_close(sc, fp->f_flag, chan);
   1631 		break;
   1632 	default:
   1633 		error = ENXIO;
   1634 		break;
   1635 	}
   1636 	if (error == 0) {
   1637 		kmem_free(fp->f_audioctx, sizeof(struct audio_chan));
   1638 		fp->f_audioctx = NULL;
   1639 	}
   1640 
   1641 	audio_exit(sc);
   1642 
   1643 	return error;
   1644 }
   1645 
   1646 int
   1647 audioread(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1648 	  int ioflag)
   1649 {
   1650 	struct audio_softc *sc;
   1651 	struct virtual_channel *vc;
   1652 	int error;
   1653 	dev_t dev;
   1654 
   1655 	dev = fp->f_audioctx->dev;
   1656 
   1657 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1658 		return error;
   1659 
   1660 	switch (AUDIODEV(dev)) {
   1661 	case SOUND_DEVICE:
   1662 	case AUDIO_DEVICE:
   1663 		vc = fp->f_audioctx->vc;
   1664 		error = audio_read(sc, uio, ioflag, vc);
   1665 		break;
   1666 	case AUDIOCTL_DEVICE:
   1667 	case MIXER_DEVICE:
   1668 		error = ENODEV;
   1669 		break;
   1670 	default:
   1671 		error = ENXIO;
   1672 		break;
   1673 	}
   1674 	audio_exit(sc);
   1675 
   1676 	return error;
   1677 }
   1678 
   1679 int
   1680 audiowrite(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1681 	   int ioflag)
   1682 {
   1683 	struct audio_softc *sc;
   1684 	struct virtual_channel *vc;
   1685 	int error;
   1686 	dev_t dev;
   1687 
   1688 	dev = fp->f_audioctx->dev;
   1689 
   1690 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1691 		return error;
   1692 
   1693 	switch (AUDIODEV(dev)) {
   1694 	case SOUND_DEVICE:
   1695 	case AUDIO_DEVICE:
   1696 		vc = fp->f_audioctx->vc;
   1697 		error = audio_write(sc, uio, ioflag, vc);
   1698 		break;
   1699 	case AUDIOCTL_DEVICE:
   1700 	case MIXER_DEVICE:
   1701 		error = ENODEV;
   1702 		break;
   1703 	default:
   1704 		error = ENXIO;
   1705 		break;
   1706 	}
   1707 	audio_exit(sc);
   1708 
   1709 	return error;
   1710 }
   1711 
   1712 int
   1713 audioioctl(struct file *fp, u_long cmd, void *addr)
   1714 {
   1715 	struct audio_softc *sc;
   1716 	struct audio_chan *chan;
   1717 	struct lwp *l = curlwp;
   1718 	int error;
   1719 	krw_t rw;
   1720 	dev_t dev;
   1721 
   1722 	chan = fp->f_audioctx;
   1723 	dev = chan->dev;
   1724 
   1725 	/* Figure out which lock type we need. */
   1726 	switch (cmd) {
   1727 	case AUDIO_FLUSH:
   1728 	case AUDIO_SETINFO:
   1729 	case AUDIO_DRAIN:
   1730 	case AUDIO_SETFD:
   1731 		rw = RW_WRITER;
   1732 		break;
   1733 	default:
   1734 		rw = RW_READER;
   1735 		break;
   1736 	}
   1737 
   1738 	if ((error = audio_enter(dev, rw, &sc)) != 0)
   1739 		return error;
   1740 	chan = fp->f_audioctx;
   1741 
   1742 	switch (AUDIODEV(dev)) {
   1743 	case SOUND_DEVICE:
   1744 	case AUDIO_DEVICE:
   1745 	case AUDIOCTL_DEVICE:
   1746 		device_active(sc->dev, DVA_SYSTEM);
   1747 		if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ))
   1748 			error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   1749 		else
   1750 			error = audio_ioctl(dev, sc, cmd, addr, fp->f_flag, l,
   1751 			    chan);
   1752 		break;
   1753 	case MIXER_DEVICE:
   1754 		error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   1755 		break;
   1756 	default:
   1757 		error = ENXIO;
   1758 		break;
   1759 	}
   1760 	audio_exit(sc);
   1761 
   1762 	return error;
   1763 }
   1764 
   1765 int
   1766 audiostat(struct file *fp, struct stat *st)
   1767 {
   1768 	memset(st, 0, sizeof(*st));
   1769 
   1770 	st->st_dev = fp->f_audioctx->dev;
   1771 
   1772 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   1773 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   1774 	st->st_mode = S_IFCHR;
   1775 	return 0;
   1776 }
   1777 
   1778 int
   1779 audiopoll(struct file *fp, int events)
   1780 {
   1781 	struct audio_softc *sc;
   1782 	struct virtual_channel *vc;
   1783 	struct lwp *l = curlwp;
   1784 	int revents;
   1785 	dev_t dev;
   1786 
   1787 	dev = fp->f_audioctx->dev;
   1788 
   1789 	/* Don't bother with device level lock here. */
   1790 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1791 	if (sc == NULL)
   1792 		return ENXIO;
   1793 	mutex_enter(sc->sc_lock);
   1794 	if (sc->sc_dying) {
   1795 		mutex_exit(sc->sc_lock);
   1796 		return EIO;
   1797 	}
   1798 
   1799 	switch (AUDIODEV(dev)) {
   1800 	case SOUND_DEVICE:
   1801 	case AUDIO_DEVICE:
   1802 		vc = fp->f_audioctx->vc;
   1803 		revents = audio_poll(sc, events, l, vc);
   1804 		break;
   1805 	case AUDIOCTL_DEVICE:
   1806 	case MIXER_DEVICE:
   1807 		revents = 0;
   1808 		break;
   1809 	default:
   1810 		revents = POLLERR;
   1811 		break;
   1812 	}
   1813 	mutex_exit(sc->sc_lock);
   1814 
   1815 	return revents;
   1816 }
   1817 
   1818 int
   1819 audiokqfilter(struct file *fp, struct knote *kn)
   1820 {
   1821 	struct audio_softc *sc;
   1822 	int rv;
   1823 	struct audio_chan *chan;
   1824 	dev_t dev;
   1825 
   1826 	chan = fp->f_audioctx;
   1827 	dev = chan->dev;
   1828 
   1829 	/* Don't bother with device level lock here. */
   1830 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1831 	if (sc == NULL)
   1832 		return ENXIO;
   1833 	mutex_enter(sc->sc_lock);
   1834 	if (sc->sc_dying) {
   1835 		mutex_exit(sc->sc_lock);
   1836 		return EIO;
   1837 	}
   1838 	switch (AUDIODEV(dev)) {
   1839 	case SOUND_DEVICE:
   1840 	case AUDIO_DEVICE:
   1841 		rv = audio_kqfilter(chan, kn);
   1842 		break;
   1843 	case AUDIOCTL_DEVICE:
   1844 	case MIXER_DEVICE:
   1845 		rv = 1;
   1846 		break;
   1847 	default:
   1848 		rv = 1;
   1849 	}
   1850 	mutex_exit(sc->sc_lock);
   1851 
   1852 	return rv;
   1853 }
   1854 
   1855 /* XXX:NS mmap is disabled. */
   1856 int
   1857 audio_fop_mmap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
   1858 	     int *advicep, struct uvm_object **uobjp, int *maxprotp)
   1859 {
   1860 	struct audio_chan *chan;
   1861 	struct virtual_channel *vc;
   1862 	dev_t dev;
   1863 
   1864 	return -1;
   1865 
   1866 	chan = fp->f_audioctx;
   1867 	dev = chan->dev;
   1868 	vc = chan->vc;
   1869 
   1870 	*offp = audiommap(dev, *offp, prot, vc);
   1871 	*maxprotp = prot;
   1872 	*advicep = UVM_ADV_RANDOM;
   1873 	return -1;
   1874 }
   1875 
   1876 paddr_t
   1877 audiommap(dev_t dev, off_t off, int prot, struct virtual_channel *vc)
   1878 {
   1879 	struct audio_softc *sc;
   1880 	paddr_t error;
   1881 
   1882 	return -1;
   1883 
   1884 	/*
   1885 	 * Acquire a reader lock.  audio_mmap() will drop sc_lock
   1886 	 * in order to allow the device's mmap routine to sleep.
   1887 	 * Although not yet possible, we want to prevent memory
   1888 	 * from being allocated or freed out from under us.
   1889 	 */
   1890 	if ((error = audio_enter(dev, RW_READER, &sc)) != 0)
   1891 		return 1;
   1892 	device_active(sc->dev, DVA_SYSTEM); /* XXXJDM */
   1893 
   1894 	switch (AUDIODEV(dev)) {
   1895 	case SOUND_DEVICE:
   1896 	case AUDIO_DEVICE:
   1897 		error = audio_mmap(sc, off, prot, vc);
   1898 		break;
   1899 	case AUDIOCTL_DEVICE:
   1900 	case MIXER_DEVICE:
   1901 		error = -1;
   1902 		break;
   1903 	default:
   1904 		error = -1;
   1905 		break;
   1906 	}
   1907 	audio_exit(sc);
   1908 	return error;
   1909 }
   1910 
   1911 /*
   1912  * Audio driver
   1913  */
   1914 void
   1915 audio_init_ringbuffer(struct audio_softc *sc, struct audio_ringbuffer *rp,
   1916 		      int mode)
   1917 {
   1918 	int nblks;
   1919 	int blksize;
   1920 
   1921 	blksize = rp->blksize;
   1922 	if (blksize < AUMINBLK)
   1923 		blksize = AUMINBLK;
   1924 	if (blksize > rp->s.bufsize / AUMINNOBLK)
   1925 		blksize = rp->s.bufsize / AUMINNOBLK;
   1926 	ROUNDSIZE(blksize);
   1927 	DPRINTF(("audio_init_ringbuffer: MI blksize=%d\n", blksize));
   1928 	if (sc->hw_if->round_blocksize)
   1929 		blksize = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
   1930 						     mode, &rp->s.param);
   1931 	if (blksize <= 0)
   1932 		panic("audio_init_ringbuffer: blksize=%d", blksize);
   1933 	nblks = rp->s.bufsize / blksize;
   1934 
   1935 	DPRINTF(("audio_init_ringbuffer: final blksize=%d\n", blksize));
   1936 	rp->blksize = blksize;
   1937 	rp->maxblks = nblks;
   1938 	rp->s.end = rp->s.start + nblks * blksize;
   1939 	rp->s.outp = rp->s.inp = rp->s.start;
   1940 	rp->s.used = 0;
   1941 	rp->stamp = 0;
   1942 	rp->stamp_last = 0;
   1943 	rp->fstamp = 0;
   1944 	rp->drops = 0;
   1945 	rp->copying = false;
   1946 	rp->needfill = false;
   1947 	rp->mmapped = false;
   1948 	memset(rp->s.start, 0, blksize * 2);
   1949 }
   1950 
   1951 int
   1952 audio_initbufs(struct audio_softc *sc, struct virtual_channel *vc)
   1953 {
   1954 	const struct audio_hw_if *hw;
   1955 	struct audio_chan *chan;
   1956 	int error;
   1957 
   1958 	if (vc == NULL) {
   1959 		chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   1960 		vc = chan->vc;
   1961 	}
   1962 
   1963 	DPRINTF(("audio_initbufs: mode=0x%x\n", vc->sc_mode));
   1964 	hw = sc->hw_if;
   1965 	if (audio_can_capture(sc) || (vc->sc_open & AUOPEN_READ)) {
   1966 		audio_init_ringbuffer(sc, &vc->sc_mrr,
   1967 		    AUMODE_RECORD);
   1968 		if (sc->sc_opens == 0 && hw->init_input &&
   1969 		    (vc->sc_mode & AUMODE_RECORD)) {
   1970 			error = hw->init_input(sc->hw_hdl, vc->sc_mrr.s.start,
   1971 				       vc->sc_mrr.s.end - vc->sc_mrr.s.start);
   1972 			if (error)
   1973 				return error;
   1974 		}
   1975 	}
   1976 
   1977 	if (audio_can_playback(sc) || (vc->sc_open & AUOPEN_WRITE)) {
   1978 		audio_init_ringbuffer(sc, &vc->sc_mpr,
   1979 		    AUMODE_PLAY);
   1980 		vc->sc_sil_count = 0;
   1981 		if (sc->sc_opens == 0 && hw->init_output &&
   1982 		    (vc->sc_mode & AUMODE_PLAY)) {
   1983 			error = hw->init_output(sc->hw_hdl, vc->sc_mpr.s.start,
   1984 					vc->sc_mpr.s.end - vc->sc_mpr.s.start);
   1985 			if (error)
   1986 				return error;
   1987 		}
   1988 	}
   1989 
   1990 #ifdef AUDIO_INTR_TIME
   1991 #define double u_long
   1992 	if (audio_can_playback(sc)) {
   1993 		sc->sc_pnintr = 0;
   1994 		sc->sc_pblktime = (u_long)(
   1995 		    (double)vc->sc_mpr.blksize * 100000 /
   1996 		    (double)(vc->sc_pparams.precision / NBBY *
   1997 			     vc->sc_pparams.channels *
   1998 			     vc->sc_pparams.sample_rate)) * 10;
   1999 		DPRINTF(("audio: play blktime = %lu for %d\n",
   2000 			 sc->sc_pblktime, vc->sc_mpr.blksize));
   2001 	}
   2002 	if (audio_can_capture(sc)) {
   2003 		sc->sc_rnintr = 0;
   2004 		sc->sc_rblktime = (u_long)(
   2005 		    (double)vc->sc_mrr.blksize * 100000 /
   2006 		    (double)(vc->sc_rparams.precision / NBBY *
   2007 			     vc->sc_rparams.channels *
   2008 			     vc->sc_rparams.sample_rate)) * 10;
   2009 		DPRINTF(("audio: record blktime = %lu for %d\n",
   2010 			 sc->sc_rblktime, vc->sc_mrr.blksize));
   2011 	}
   2012 #undef double
   2013 #endif
   2014 
   2015 	return 0;
   2016 }
   2017 
   2018 void
   2019 audio_calcwater(struct audio_softc *sc, struct virtual_channel *vc)
   2020 {
   2021 	/* set high at 100% */
   2022 	if (audio_can_playback(sc) && vc && vc->sc_pustream) {
   2023 		vc->sc_mpr.usedhigh =
   2024 		    vc->sc_pustream->end - vc->sc_pustream->start;
   2025 		/* set low at 75% of usedhigh */
   2026 		vc->sc_mpr.usedlow = vc->sc_mpr.usedhigh * 3 / 4;
   2027 		if (vc->sc_mpr.usedlow == vc->sc_mpr.usedhigh)
   2028 			vc->sc_mpr.usedlow -= vc->sc_mpr.blksize;
   2029 	}
   2030 
   2031 	if (audio_can_capture(sc) && vc && vc->sc_rustream) {
   2032 		vc->sc_mrr.usedhigh =
   2033 		    vc->sc_rustream->end - vc->sc_rustream->start -
   2034 		    vc->sc_mrr.blksize;
   2035 		vc->sc_mrr.usedlow = 0;
   2036 		DPRINTF(("%s: plow=%d phigh=%d rlow=%d rhigh=%d\n", __func__,
   2037 			 vc->sc_mpr.usedlow, vc->sc_mpr.usedhigh,
   2038 			 vc->sc_mrr.usedlow, vc->sc_mrr.usedhigh));
   2039 	}
   2040 }
   2041 
   2042 int
   2043 audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   2044     struct lwp *l, struct file **nfp)
   2045 {
   2046 	struct file *fp;
   2047 	int error, fd, i, n;
   2048 	u_int mode;
   2049 	const struct audio_hw_if *hw;
   2050 	struct virtual_channel *vc;
   2051 	struct audio_chan *chan;
   2052 
   2053 	KASSERT(mutex_owned(sc->sc_lock));
   2054 
   2055 	if (sc->sc_ready == false)
   2056 		return ENXIO;
   2057 
   2058 	hw = sc->hw_if;
   2059 	if (hw == NULL)
   2060 		return ENXIO;
   2061 	n = 0;
   2062 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries)
   2063 		n++;
   2064 
   2065 	chan = kmem_zalloc(sizeof(struct audio_chan), KM_SLEEP);
   2066 	vc = kmem_zalloc(sizeof(struct virtual_channel), KM_SLEEP);
   2067 	chan->vc = vc;
   2068 
   2069 	vc->sc_open = 0;
   2070 	vc->sc_mode = 0;
   2071 	vc->sc_sil_count = 0;
   2072 	vc->sc_nrfilters = 0;
   2073 	memset(vc->sc_rfilters, 0,
   2074 	    sizeof(vc->sc_rfilters));
   2075 	vc->sc_rbus = false;
   2076 	vc->sc_npfilters = 0;
   2077 	memset(vc->sc_pfilters, 0,
   2078 	    sizeof(vc->sc_pfilters));
   2079 	vc->sc_draining = false;
   2080 	vc->sc_pbus = false;
   2081 	vc->sc_blkset = false;
   2082 	vc->sc_lastinfovalid = false;
   2083 	vc->sc_swvol = 255;
   2084 	vc->sc_recswvol = 255;
   2085 
   2086 	DPRINTF(("audio_open: flags=0x%x sc=%p hdl=%p\n",
   2087 		 flags, sc, sc->hw_hdl));
   2088 
   2089 	if (((flags & FREAD) && (vc->sc_open & AUOPEN_READ)) ||
   2090 	    ((flags & FWRITE) && (vc->sc_open & AUOPEN_WRITE))) {
   2091 		kmem_free(vc, sizeof(struct virtual_channel));
   2092 		kmem_free(chan, sizeof(struct audio_chan));
   2093 		return EBUSY;
   2094 	}
   2095 
   2096 	error = audio_alloc_ring(sc, &vc->sc_mpr,
   2097 	    	    AUMODE_PLAY, AU_RING_SIZE);
   2098 	if (!error) {
   2099 		error = audio_alloc_ring(sc, &vc->sc_mrr,
   2100 	    	    AUMODE_RECORD, AU_RING_SIZE);
   2101 	}
   2102 	if (error) {
   2103 		kmem_free(vc, sizeof(struct virtual_channel));
   2104 		kmem_free(chan, sizeof(struct audio_chan));
   2105 		return error;
   2106 	}
   2107 
   2108 	if (sc->sc_opens == 0) {
   2109 		sc->sc_credentials = kauth_cred_get();
   2110 		kauth_cred_hold(sc->sc_credentials);
   2111 		if (hw->open != NULL) {
   2112 			mutex_enter(sc->sc_intr_lock);
   2113 			error = hw->open(sc->hw_hdl, flags);
   2114 			mutex_exit(sc->sc_intr_lock);
   2115 			if (error) {
   2116 				kmem_free(vc,
   2117 				    sizeof(struct virtual_channel));
   2118 				kmem_free(chan,
   2119 				    sizeof(struct audio_chan));
   2120 				return error;
   2121 			}
   2122 		}
   2123 		audio_init_ringbuffer(sc, &sc->sc_pr, AUMODE_PLAY);
   2124 		audio_init_ringbuffer(sc, &sc->sc_rr, AUMODE_RECORD);
   2125 		audio_initbufs(sc, NULL);
   2126 		sc->schedule_wih = false;
   2127 		sc->schedule_rih = false;
   2128 		sc->sc_eof = 0;
   2129 		vc->sc_rbus = false;
   2130 		sc->sc_async_audio = 0;
   2131 	} else if (sc->sc_multiuser == false) {
   2132 		/* XXX:NS Should be handled correctly. */
   2133 		/* Do we allow multi user access */
   2134 		if (kauth_cred_geteuid(sc->sc_credentials) !=
   2135 		    kauth_cred_geteuid(kauth_cred_get()) &&
   2136 		    kauth_cred_geteuid(kauth_cred_get()) != 0) {
   2137 			error = EPERM;
   2138 			goto bad;
   2139 		}
   2140 	}
   2141 
   2142 	mutex_enter(sc->sc_intr_lock);
   2143 	vc->sc_full_duplex =
   2144 		(flags & (FWRITE|FREAD)) == (FWRITE|FREAD) &&
   2145 		(audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX);
   2146 	mutex_exit(sc->sc_intr_lock);
   2147 
   2148 	mode = 0;
   2149 	if (flags & FREAD) {
   2150 		vc->sc_open |= AUOPEN_READ;
   2151 		mode |= AUMODE_RECORD;
   2152 	}
   2153 	if (flags & FWRITE) {
   2154 		vc->sc_open |= AUOPEN_WRITE;
   2155 		mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
   2156 	}
   2157 
   2158 	vc->sc_mrr.blksize = sc->sc_rr.blksize;
   2159 	vc->sc_mpr.blksize = sc->sc_pr.blksize;
   2160 
   2161 	/*
   2162 	 * Multiplex device: /dev/audio (MU-Law) and /dev/sound (linear)
   2163 	 * The /dev/audio is always (re)set to 8-bit MU-Law mono
   2164 	 * For the other devices, you get what they were last set to.
   2165 	 */
   2166 	error = audio_set_defaults(sc, mode, vc);
   2167 	if (!error && ISDEVSOUND(dev) && sc->sc_aivalid == true) {
   2168 		sc->sc_ai.mode = mode;
   2169 		error = audiosetinfo(sc, &sc->sc_ai, true, vc);
   2170 	}
   2171 	if (error)
   2172 		goto bad;
   2173 
   2174 #ifdef DIAGNOSTIC
   2175 	/*
   2176 	 * Sample rate and precision are supposed to be set to proper
   2177 	 * default values by the hardware driver, so that it may give
   2178 	 * us these values.
   2179 	 */
   2180 	if (vc->sc_rparams.precision == 0 || vc->sc_pparams.precision == 0) {
   2181 		printf("audio_open: 0 precision\n");
   2182 		goto bad;
   2183 	}
   2184 #endif
   2185 
   2186 	/* audio_close() decreases sc_mpr[n].usedlow, recalculate here */
   2187 	audio_calcwater(sc, vc);
   2188 
   2189 	error = fd_allocfile(&fp, &fd);
   2190 	if (error)
   2191 		return error;
   2192 
   2193 	DPRINTF(("audio_open: done sc_mode = 0x%x\n", vc->sc_mode));
   2194 
   2195 	grow_mixer_states(sc, 2);
   2196 	if (flags & FREAD)
   2197 		sc->sc_recopens++;
   2198 	sc->sc_opens++;
   2199 	chan->dev = dev;
   2200 	chan->chan = n;
   2201 	chan->deschan = n;
   2202 	SIMPLEQ_INSERT_TAIL(&sc->sc_audiochan, chan, entries);
   2203 
   2204 	error = fd_clone(fp, fd, flags, &audio_fileops, chan);
   2205 	KASSERT(error == EMOVEFD);
   2206 
   2207 	*nfp = fp;
   2208 	return error;
   2209 
   2210 bad:
   2211 	for (i = 0; i < vc->sc_npfilters; i++) {
   2212 		vc->sc_pfilters[i]->dtor(vc->sc_pfilters[i]);
   2213 		vc->sc_pfilters[i] = NULL;
   2214 		audio_stream_dtor(&vc->sc_pstreams[i]);
   2215 	}
   2216 	vc->sc_npfilters = 0;
   2217 	for (i = 0; i < vc->sc_nrfilters; i++) {
   2218 		vc->sc_rfilters[i]->dtor(vc->sc_rfilters[i]);
   2219 		vc->sc_rfilters[i] = NULL;
   2220 		audio_stream_dtor(&vc->sc_rstreams[i]);
   2221 	}
   2222 	vc->sc_nrfilters = 0;
   2223 	if (hw->close != NULL && sc->sc_opens == 0)
   2224 		hw->close(sc->hw_hdl);
   2225 	mutex_exit(sc->sc_lock);
   2226 	audio_free_ring(sc, &vc->sc_mpr);
   2227 	audio_free_ring(sc, &vc->sc_mrr);
   2228 	mutex_enter(sc->sc_lock);
   2229 	kmem_free(vc, sizeof(struct virtual_channel));
   2230 	kmem_free(chan, sizeof(struct audio_chan));
   2231 	return error;
   2232 }
   2233 
   2234 /*
   2235  * Must be called from task context.
   2236  */
   2237 void
   2238 audio_init_record(struct audio_softc *sc, struct virtual_channel *vc)
   2239 {
   2240 
   2241 	KASSERT(mutex_owned(sc->sc_lock));
   2242 
   2243 	if (sc->sc_opens != 0)
   2244 		return;
   2245 
   2246 	mutex_enter(sc->sc_intr_lock);
   2247 	if (sc->hw_if->speaker_ctl &&
   2248 	    (!vc->sc_full_duplex || (vc->sc_mode & AUMODE_PLAY) == 0))
   2249 		sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_OFF);
   2250 	mutex_exit(sc->sc_intr_lock);
   2251 }
   2252 
   2253 /*
   2254  * Must be called from task context.
   2255  */
   2256 void
   2257 audio_init_play(struct audio_softc *sc, struct virtual_channel *vc)
   2258 {
   2259 
   2260 	KASSERT(mutex_owned(sc->sc_lock));
   2261 
   2262 	if (sc->sc_opens != 0)
   2263 		return;
   2264 
   2265 	mutex_enter(sc->sc_intr_lock);
   2266 	vc->sc_wstamp = vc->sc_mpr.stamp;
   2267 	if (sc->hw_if->speaker_ctl)
   2268 		sc->hw_if->speaker_ctl(sc->hw_hdl, SPKR_ON);
   2269 	mutex_exit(sc->sc_intr_lock);
   2270 }
   2271 
   2272 int
   2273 audio_drain(struct audio_softc *sc, struct audio_chan *chan)
   2274 {
   2275 	struct audio_ringbuffer *cb;
   2276 	struct virtual_channel *vc;
   2277 	int error, drops;
   2278 	int cc, i, used;
   2279 	bool hw = false;
   2280 
   2281 	KASSERT(mutex_owned(sc->sc_lock));
   2282 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2283 
   2284 	error = 0;
   2285 	vc = chan->vc;
   2286 	DPRINTF(("audio_drain: enter busy=%d\n", vc->sc_pbus));
   2287 	cb = &chan->vc->sc_mpr;
   2288 	if (cb->mmapped)
   2289 		return 0;
   2290 
   2291 	used = audio_stream_get_used(&cb->s);
   2292 	if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan)) {
   2293 		hw = true;
   2294 		used += audio_stream_get_used(&sc->sc_pr.s);
   2295 	}
   2296 	for (i = 0; i < vc->sc_npfilters; i++)
   2297 		used += audio_stream_get_used(&vc->sc_pstreams[i]);
   2298 	if (used <= 0 || (hw == true && sc->hw_if->trigger_output == NULL))
   2299 		return 0;
   2300 
   2301 	if (hw == false && !vc->sc_pbus) {
   2302 		/* We've never started playing, probably because the
   2303 		 * block was too short.  Pad it and start now.
   2304 		 */
   2305 		uint8_t *inp = cb->s.inp;
   2306 
   2307 		cc = cb->blksize - (inp - cb->s.start) % cb->blksize;
   2308 		audio_fill_silence(&cb->s.param, inp, cc);
   2309 		cb->s.inp = audio_stream_add_inp(&cb->s, inp, cc);
   2310 		mutex_exit(sc->sc_intr_lock);
   2311 		error = audiostartp(sc, vc);
   2312 		mutex_enter(sc->sc_intr_lock);
   2313 		if (error)
   2314 			return error;
   2315 	} else if (hw == true) {
   2316 		used = cb->blksize - (sc->sc_pr.s.inp - sc->sc_pr.s.start)
   2317 		    % cb->blksize;
   2318 		while (used > 0) {
   2319 			cc = sc->sc_pr.s.end - sc->sc_pr.s.inp;
   2320 			if (cc > used)
   2321 				cc = used;
   2322 			audio_fill_silence(&cb->s.param, sc->sc_pr.s.inp, cc);
   2323 			sc->sc_pr.s.inp = audio_stream_add_inp(&sc->sc_pr.s,
   2324 			    sc->sc_pr.s.inp, cc);
   2325 			used -= cc;
   2326 		}
   2327 		mix_write(sc);
   2328 	}
   2329 	/*
   2330 	 * Play until a silence block has been played, then we
   2331 	 * know all has been drained.
   2332 	 * XXX This should be done some other way to avoid
   2333 	 * playing silence.
   2334 	 */
   2335 #ifdef DIAGNOSTIC
   2336 	if (cb->copying) {
   2337 		DPRINTF(("audio_drain: copying in progress!?!\n"));
   2338 		cb->copying = false;
   2339 	}
   2340 #endif
   2341 	vc->sc_draining = true;
   2342 
   2343 	drops = cb->drops;
   2344 	error = 0;
   2345 	while (cb->drops == drops && !error) {
   2346 		DPRINTF(("audio_drain: chan=%d used=%d, drops=%ld\n",
   2347 			chan->chan,
   2348 			audio_stream_get_used(&vc->sc_mpr.s),
   2349 			cb->drops));
   2350 		mutex_exit(sc->sc_intr_lock);
   2351 		error = audio_waitio(sc, &sc->sc_wchan, vc);
   2352 		mutex_enter(sc->sc_intr_lock);
   2353 		if (sc->sc_dying)
   2354 			error = EIO;
   2355 	}
   2356 	vc->sc_draining = false;
   2357 
   2358 	return error;
   2359 }
   2360 
   2361 /*
   2362  * Close an audio chip.
   2363  */
   2364 /* ARGSUSED */
   2365 int
   2366 audio_close(struct audio_softc *sc, int flags, struct audio_chan *chan)
   2367 {
   2368 	struct virtual_channel *vc;
   2369 	const struct audio_hw_if *hw;
   2370 	int o;
   2371 
   2372 	KASSERT(mutex_owned(sc->sc_lock));
   2373 
   2374 	if (sc->sc_opens == 0)
   2375 		return ENXIO;
   2376 
   2377 	vc = chan->vc;
   2378 
   2379 	hw = sc->hw_if;
   2380 	if (hw == NULL)
   2381 		return ENXIO;
   2382 	mutex_enter(sc->sc_intr_lock);
   2383 	DPRINTF(("audio_close: sc=%p\n", sc));
   2384 	/* Stop recording. */
   2385 	if (sc->sc_recopens == 1 && (flags & FREAD) && vc->sc_rbus) {
   2386 		/*
   2387 		 * XXX Some drivers (e.g. SB) use the same routine
   2388 		 * to halt input and output so don't halt input if
   2389 		 * in full duplex mode.  These drivers should be fixed.
   2390 		 */
   2391 		if (!vc->sc_full_duplex || hw->halt_input != hw->halt_output)
   2392 			hw->halt_input(sc->hw_hdl);
   2393 		vc->sc_rbus = false;
   2394 	}
   2395 	/*
   2396 	 * Block until output drains, but allow ^C interrupt.
   2397 	 */
   2398 	vc->sc_mpr.usedlow = vc->sc_mpr.blksize;  /* avoid excessive wakeups */
   2399 	/*
   2400 	 * If there is pending output, let it drain (unless
   2401 	 * the output is paused).
   2402 	 */
   2403 	if ((flags & FWRITE) && vc->sc_pbus) {
   2404 		if (!vc->sc_mpr.pause)
   2405 			audio_drain(sc, chan);
   2406 		vc->sc_pbus = false;
   2407 	}
   2408 	if (sc->sc_opens == 1) {
   2409 		audio_drain(sc, SIMPLEQ_FIRST(&sc->sc_audiochan));
   2410 		if (hw->drain)
   2411 			(void)hw->drain(sc->hw_hdl);
   2412 		hw->halt_output(sc->hw_hdl);
   2413 		sc->sc_trigger_started = false;
   2414 	}
   2415 	if ((flags & FREAD) && (sc->sc_recopens == 1))
   2416 		sc->sc_rec_started = false;
   2417 
   2418 	if (sc->sc_opens == 1 && hw->close != NULL)
   2419 		hw->close(sc->hw_hdl);
   2420 	mutex_exit(sc->sc_intr_lock);
   2421 
   2422 	if (sc->sc_opens == 1) {
   2423 		sc->sc_async_audio = 0;
   2424 		kauth_cred_free(sc->sc_credentials);
   2425 	}
   2426 
   2427 	vc->sc_open = 0;
   2428 	vc->sc_mode = 0;
   2429 	vc->sc_full_duplex = 0;
   2430 
   2431 	for (o = 0; o < vc->sc_npfilters; o++) {
   2432 		vc->sc_pfilters[o]->dtor(vc->sc_pfilters[o]);
   2433 		vc->sc_pfilters[o] = NULL;
   2434 		audio_stream_dtor(&vc->sc_pstreams[o]);
   2435 	}
   2436 	vc->sc_npfilters = 0;
   2437 	for (o = 0; o < vc->sc_nrfilters; o++) {
   2438 		vc->sc_rfilters[o]->dtor(vc->sc_rfilters[o]);
   2439 		vc->sc_rfilters[o] = NULL;
   2440 		audio_stream_dtor(&vc->sc_rstreams[o]);
   2441 	}
   2442 	vc->sc_nrfilters = 0;
   2443 
   2444 	if (flags & FREAD)
   2445 		sc->sc_recopens--;
   2446 	sc->sc_opens--;
   2447 	shrink_mixer_states(sc, 2);
   2448 	SIMPLEQ_REMOVE(&sc->sc_audiochan, chan, audio_chan, entries);
   2449 	mutex_exit(sc->sc_lock);
   2450 	audio_free_ring(sc, &vc->sc_mpr);
   2451 	audio_free_ring(sc, &vc->sc_mrr);
   2452 	mutex_enter(sc->sc_lock);
   2453 	kmem_free(vc, sizeof(struct virtual_channel));
   2454 
   2455 	return 0;
   2456 }
   2457 
   2458 int
   2459 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag,
   2460 	   struct virtual_channel *vc)
   2461 {
   2462 	struct audio_ringbuffer *cb;
   2463 	const uint8_t *outp;
   2464 	uint8_t *inp;
   2465 	int error, used, cc, n;
   2466 
   2467 	KASSERT(mutex_owned(sc->sc_lock));
   2468 
   2469 	if (sc->hw_if == NULL)
   2470 		return ENXIO;
   2471 
   2472 	cb = &vc->sc_mrr;
   2473 	if (cb->mmapped)
   2474 		return EINVAL;
   2475 
   2476 	DPRINTFN(1,("audio_read: cc=%zu mode=%d\n",
   2477 		    uio->uio_resid, vc->sc_mode));
   2478 
   2479 #ifdef AUDIO_PM_IDLE
   2480 	if (device_is_active(&sc->dev) || sc->sc_idle)
   2481 		device_active(&sc->dev, DVA_SYSTEM);
   2482 #endif
   2483 
   2484 	error = 0;
   2485 	/*
   2486 	 * If hardware is half-duplex and currently playing, return
   2487 	 * silence blocks based on the number of blocks we have output.
   2488 	 */
   2489 	if (!vc->sc_full_duplex && (vc->sc_mode & AUMODE_PLAY)) {
   2490 		while (uio->uio_resid > 0 && !error) {
   2491 			for(;;) {
   2492 				/*
   2493 				 * No need to lock, as any wakeup will be
   2494 				 * held for us while holding sc_lock.
   2495 				 */
   2496 				cc = vc->sc_mpr.stamp - vc->sc_wstamp;
   2497 				if (cc > 0)
   2498 					break;
   2499 				DPRINTF(("audio_read: stamp=%lu, wstamp=%lu\n",
   2500 					 vc->sc_mpr.stamp, vc->sc_wstamp));
   2501 				if (ioflag & IO_NDELAY)
   2502 					return EWOULDBLOCK;
   2503 				error = audio_waitio(sc, &sc->sc_rchan, vc);
   2504 				if (sc->sc_dying)
   2505 					error = EIO;
   2506 				if (error)
   2507 					return error;
   2508 			}
   2509 
   2510 			if (uio->uio_resid < cc)
   2511 				cc = uio->uio_resid;
   2512 			DPRINTFN(1,("audio_read: reading in write mode, "
   2513 				    "cc=%d\n", cc));
   2514 			error = audio_silence_copyout(sc, cc, uio);
   2515 			vc->sc_wstamp += cc;
   2516 		}
   2517 		return error;
   2518 	}
   2519 
   2520 	while (uio->uio_resid > 0 && !error) {
   2521 		while ((used = audio_stream_get_used(vc->sc_rustream)) <= 0) {
   2522 			if (!vc->sc_rbus && !vc->sc_mrr.pause)
   2523 				error = audiostartr(sc, vc);
   2524 			if (error)
   2525 				return error;
   2526 			if (ioflag & IO_NDELAY)
   2527 				return EWOULDBLOCK;
   2528 			DPRINTFN(2, ("audio_read: sleep used=%d\n", used));
   2529 			error = audio_waitio(sc, &sc->sc_rchan, vc);
   2530 			if (sc->sc_dying)
   2531 				error = EIO;
   2532 			if (error)
   2533 				return error;
   2534 		}
   2535 
   2536 		outp = vc->sc_rustream->outp;
   2537 		inp = vc->sc_rustream->inp;
   2538 		cb->copying = true;
   2539 
   2540 		/*
   2541 		 * cc is the amount of data in the sc_rustream excluding
   2542 		 * wrapped data.  Note the tricky case of inp == outp, which
   2543 		 * must mean the buffer is full, not empty, because used > 0.
   2544 		 */
   2545 		cc = outp < inp ? inp - outp :vc->sc_rustream->end - outp;
   2546 		DPRINTFN(1,("audio_read: outp=%p, cc=%d\n", outp, cc));
   2547 
   2548 		n = uio->uio_resid;
   2549 		mutex_exit(sc->sc_lock);
   2550 		error = uiomove(__UNCONST(outp), cc, uio);
   2551 		mutex_enter(sc->sc_lock);
   2552 		n -= uio->uio_resid; /* number of bytes actually moved */
   2553 
   2554 		vc->sc_rustream->outp = audio_stream_add_outp
   2555 			(vc->sc_rustream, outp, n);
   2556 		cb->copying = false;
   2557 	}
   2558 	return error;
   2559 }
   2560 
   2561 void
   2562 audio_clear(struct audio_softc *sc, struct virtual_channel *vc)
   2563 {
   2564 
   2565 	KASSERT(mutex_owned(sc->sc_intr_lock));
   2566 
   2567 	if (vc->sc_rbus) {
   2568 		cv_broadcast(&sc->sc_rchan);
   2569 		if (sc->sc_recopens == 1) {
   2570 			sc->hw_if->halt_input(sc->hw_hdl);
   2571 			sc->sc_rec_started = false;
   2572 		}
   2573 		vc->sc_rbus = false;
   2574 		vc->sc_mrr.pause = false;
   2575 	}
   2576 	if (vc->sc_pbus) {
   2577 		cv_broadcast(&sc->sc_wchan);
   2578 		vc->sc_pbus = false;
   2579 		vc->sc_mpr.pause = false;
   2580 	}
   2581 }
   2582 
   2583 void
   2584 audio_clear_intr_unlocked(struct audio_softc *sc, struct virtual_channel *vc)
   2585 {
   2586 
   2587 	mutex_enter(sc->sc_intr_lock);
   2588 	audio_clear(sc, vc);
   2589 	mutex_exit(sc->sc_intr_lock);
   2590 }
   2591 
   2592 void
   2593 audio_calc_blksize(struct audio_softc *sc, int mode,
   2594 		   struct virtual_channel *vc)
   2595 {
   2596 	const audio_params_t *parm;
   2597 	struct audio_ringbuffer *rb;
   2598 
   2599 	if (vc->sc_blkset)
   2600 		return;
   2601 
   2602 	if (mode == AUMODE_PLAY) {
   2603 		rb = &vc->sc_mpr;
   2604 		parm = &rb->s.param;
   2605 	} else {
   2606 		rb = &vc->sc_mrr;
   2607 		parm = &rb->s.param;
   2608 	}
   2609 
   2610 	rb->blksize = parm->sample_rate * audio_blk_ms / 1000 *
   2611 	     parm->channels * parm->precision / NBBY;
   2612 
   2613 	DPRINTF(("audio_calc_blksize: %s blksize=%d\n",
   2614 		 mode == AUMODE_PLAY ? "play" : "record", rb->blksize));
   2615 }
   2616 
   2617 void
   2618 audio_fill_silence(struct audio_params *params, uint8_t *p, int n)
   2619 {
   2620 	uint8_t auzero0, auzero1;
   2621 	int nfill;
   2622 
   2623 	auzero1 = 0;		/* initialize to please gcc */
   2624 	nfill = 1;
   2625 	switch (params->encoding) {
   2626 	case AUDIO_ENCODING_ULAW:
   2627 		auzero0 = 0x7f;
   2628 		break;
   2629 	case AUDIO_ENCODING_ALAW:
   2630 		auzero0 = 0x55;
   2631 		break;
   2632 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   2633 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   2634 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   2635 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   2636 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   2637 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   2638 	case AUDIO_ENCODING_AC3:
   2639 	case AUDIO_ENCODING_ADPCM: /* is this right XXX */
   2640 	case AUDIO_ENCODING_SLINEAR_LE:
   2641 	case AUDIO_ENCODING_SLINEAR_BE:
   2642 		auzero0 = 0;/* fortunately this works for any number of bits */
   2643 		break;
   2644 	case AUDIO_ENCODING_ULINEAR_LE:
   2645 	case AUDIO_ENCODING_ULINEAR_BE:
   2646 		if (params->precision > 8) {
   2647 			nfill = (params->precision + NBBY - 1)/ NBBY;
   2648 			auzero0 = 0x80;
   2649 			auzero1 = 0;
   2650 		} else
   2651 			auzero0 = 0x80;
   2652 		break;
   2653 	default:
   2654 		DPRINTF(("audio: bad encoding %d\n", params->encoding));
   2655 		auzero0 = 0;
   2656 		break;
   2657 	}
   2658 	if (nfill == 1) {
   2659 		while (--n >= 0)
   2660 			*p++ = auzero0; /* XXX memset */
   2661 	} else /* nfill must no longer be 2 */ {
   2662 		if (params->encoding == AUDIO_ENCODING_ULINEAR_LE) {
   2663 			int k = nfill;
   2664 			while (--k > 0)
   2665 				*p++ = auzero1;
   2666 			n -= nfill - 1;
   2667 		}
   2668 		while (n >= nfill) {
   2669 			int k = nfill;
   2670 			*p++ = auzero0;
   2671 			while (--k > 0)
   2672 				*p++ = auzero1;
   2673 
   2674 			n -= nfill;
   2675 		}
   2676 		if (n-- > 0)	/* XXX must be 1 - DIAGNOSTIC check? */
   2677 			*p++ = auzero0;
   2678 	}
   2679 }
   2680 
   2681 int
   2682 audio_silence_copyout(struct audio_softc *sc, int n, struct uio *uio)
   2683 {
   2684 	struct audio_chan *chan;
   2685 	struct virtual_channel *vc;
   2686 	uint8_t zerobuf[128];
   2687 	int error;
   2688 	int k;
   2689 
   2690 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   2691 	vc = chan->vc;
   2692 	audio_fill_silence(&vc->sc_rparams, zerobuf, sizeof zerobuf);
   2693 
   2694 	error = 0;
   2695 	while (n > 0 && uio->uio_resid > 0 && !error) {
   2696 		k = min(n, min(uio->uio_resid, sizeof zerobuf));
   2697 		mutex_exit(sc->sc_lock);
   2698 		error = uiomove(zerobuf, k, uio);
   2699 		mutex_enter(sc->sc_lock);
   2700 		n -= k;
   2701 	}
   2702 
   2703 	return error;
   2704 }
   2705 
   2706 static int
   2707 uio_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
   2708     audio_stream_t *p, int max_used)
   2709 {
   2710 	uio_fetcher_t *this;
   2711 	int size;
   2712 	int stream_space;
   2713 	int error;
   2714 
   2715 	KASSERT(mutex_owned(sc->sc_lock));
   2716 	KASSERT(!cpu_intr_p());
   2717 	KASSERT(!cpu_softintr_p());
   2718 
   2719 	this = (uio_fetcher_t *)self;
   2720 	this->last_used = audio_stream_get_used(p);
   2721 	if (this->last_used >= this->usedhigh)
   2722 		return 0;
   2723 	/*
   2724 	 * uio_fetcher ignores max_used and move the data as
   2725 	 * much as possible in order to return the correct value
   2726 	 * for audio_prinfo::seek and kfilters.
   2727 	 */
   2728 	stream_space = audio_stream_get_space(p);
   2729 	size = min(this->uio->uio_resid, stream_space);
   2730 
   2731 	/* the first fragment of the space */
   2732 	stream_space = p->end - p->inp;
   2733 	if (stream_space >= size) {
   2734 		mutex_exit(sc->sc_lock);
   2735 		error = uiomove(p->inp, size, this->uio);
   2736 		mutex_enter(sc->sc_lock);
   2737 		if (error)
   2738 			return error;
   2739 		p->inp = audio_stream_add_inp(p, p->inp, size);
   2740 	} else {
   2741 		mutex_exit(sc->sc_lock);
   2742 		error = uiomove(p->inp, stream_space, this->uio);
   2743 		mutex_enter(sc->sc_lock);
   2744 		if (error)
   2745 			return error;
   2746 		p->inp = audio_stream_add_inp(p, p->inp, stream_space);
   2747 		mutex_exit(sc->sc_lock);
   2748 		error = uiomove(p->start, size - stream_space, this->uio);
   2749 		mutex_enter(sc->sc_lock);
   2750 		if (error)
   2751 			return error;
   2752 		p->inp = audio_stream_add_inp(p, p->inp, size - stream_space);
   2753 	}
   2754 	this->last_used = audio_stream_get_used(p);
   2755 	return 0;
   2756 }
   2757 
   2758 static int
   2759 null_fetcher_fetch_to(struct audio_softc *sc, stream_fetcher_t *self,
   2760     audio_stream_t *p, int max_used)
   2761 {
   2762 
   2763 	return 0;
   2764 }
   2765 
   2766 static void
   2767 uio_fetcher_ctor(uio_fetcher_t *this, struct uio *u, int h)
   2768 {
   2769 
   2770 	this->base.fetch_to = uio_fetcher_fetch_to;
   2771 	this->uio = u;
   2772 	this->usedhigh = h;
   2773 }
   2774 
   2775 int
   2776 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag,
   2777 	    struct virtual_channel *vc)
   2778 {
   2779 	uio_fetcher_t ufetcher;
   2780 	audio_stream_t stream;
   2781 	struct audio_ringbuffer *cb;
   2782 	stream_fetcher_t *fetcher;
   2783 	stream_filter_t *filter;
   2784 	uint8_t *inp, *einp;
   2785 	int saveerror, error, m, cc, used;
   2786 
   2787 	KASSERT(mutex_owned(sc->sc_lock));
   2788 
   2789 	if (sc->hw_if == NULL)
   2790 		return ENXIO;
   2791 
   2792 	cb = &vc->sc_mpr;
   2793 
   2794 	DPRINTFN(2,("audio_write: sc=%p count=%zu used=%d(hi=%d)\n",
   2795 		    sc, uio->uio_resid, audio_stream_get_used(vc->sc_pustream),
   2796 		    vc->sc_mpr.usedhigh));
   2797 	if (vc->sc_mpr.mmapped)
   2798 		return EINVAL;
   2799 
   2800 	if (uio->uio_resid == 0) {
   2801 		sc->sc_eof++;
   2802 		return 0;
   2803 	}
   2804 
   2805 #ifdef AUDIO_PM_IDLE
   2806 	if (device_is_active(&sc->dev) || sc->sc_idle)
   2807 		device_active(&sc->dev, DVA_SYSTEM);
   2808 #endif
   2809 
   2810 	/*
   2811 	 * If half-duplex and currently recording, throw away data.
   2812 	 */
   2813 	if (!vc->sc_full_duplex &&
   2814 	    (vc->sc_mode & AUMODE_RECORD)) {
   2815 		uio->uio_offset += uio->uio_resid;
   2816 		uio->uio_resid = 0;
   2817 		DPRINTF(("audio_write: half-dpx read busy\n"));
   2818 		return 0;
   2819 	}
   2820 
   2821 	if (!(vc->sc_mode & AUMODE_PLAY_ALL) && vc->sc_playdrop > 0) {
   2822 		m = min(vc->sc_playdrop, uio->uio_resid);
   2823 		DPRINTF(("audio_write: playdrop %d\n", m));
   2824 		uio->uio_offset += m;
   2825 		uio->uio_resid -= m;
   2826 		vc->sc_playdrop -= m;
   2827 		if (uio->uio_resid == 0)
   2828 			return 0;
   2829 	}
   2830 
   2831 	/**
   2832 	 * setup filter pipeline
   2833 	 */
   2834 	uio_fetcher_ctor(&ufetcher, uio, vc->sc_mpr.usedhigh);
   2835 	if (vc->sc_npfilters > 0) {
   2836 		fetcher = &vc->sc_pfilters[vc->sc_npfilters - 1]->base;
   2837 	} else {
   2838 		fetcher = &ufetcher.base;
   2839 	}
   2840 
   2841 	error = 0;
   2842 	while (uio->uio_resid > 0 && !error) {
   2843 		/* wait if the first buffer is occupied */
   2844 		while ((used = audio_stream_get_used(vc->sc_pustream)) >=
   2845 							 cb->usedhigh) {
   2846 			DPRINTFN(2, ("audio_write: sleep used=%d lowat=%d "
   2847 				     "hiwat=%d\n", used,
   2848 				     cb->usedlow, cb->usedhigh));
   2849 			if (ioflag & IO_NDELAY)
   2850 				return EWOULDBLOCK;
   2851 			error = audio_waitio(sc, &sc->sc_wchan, vc);
   2852 			if (sc->sc_dying)
   2853 				error = EIO;
   2854 			if (error)
   2855 				return error;
   2856 		}
   2857 		inp = cb->s.inp;
   2858 		cb->copying = true;
   2859 		stream = cb->s;
   2860 		used = stream.used;
   2861 
   2862 		/* Write to the sc_pustream as much as possible. */
   2863 		if (vc->sc_npfilters > 0) {
   2864 			filter = vc->sc_pfilters[0];
   2865 			filter->set_fetcher(filter, &ufetcher.base);
   2866 			fetcher = &vc->sc_pfilters[vc->sc_npfilters - 1]->base;
   2867 			cc = cb->blksize * 2;
   2868 			error = fetcher->fetch_to(sc, fetcher, &stream, cc);
   2869 			if (error != 0) {
   2870 				fetcher = &ufetcher.base;
   2871 				cc = vc->sc_pustream->end -
   2872 				    vc->sc_pustream->start;
   2873 				error = fetcher->fetch_to(sc, fetcher,
   2874 				    vc->sc_pustream, cc);
   2875 			}
   2876 		} else {
   2877 			fetcher = &ufetcher.base;
   2878 			cc = stream.end - stream.start;
   2879 			error = fetcher->fetch_to(sc, fetcher, &stream, cc);
   2880 		}
   2881 		if (vc->sc_npfilters > 0) {
   2882 			cb->fstamp += ufetcher.last_used
   2883 			    - audio_stream_get_used(vc->sc_pustream);
   2884 		}
   2885 		cb->s.used += stream.used - used;
   2886 		cb->s.inp = stream.inp;
   2887 		einp = cb->s.inp;
   2888 
   2889 		/*
   2890 		 * This is a very suboptimal way of keeping track of
   2891 		 * silence in the buffer, but it is simple.
   2892 		 */
   2893 		vc->sc_sil_count = 0;
   2894 
   2895 		/*
   2896 		 * If the interrupt routine wants the last block filled AND
   2897 		 * the copy did not fill the last block completely it needs to
   2898 		 * be padded.
   2899 		 */
   2900 		if (cb->needfill && inp < einp &&
   2901 		    (inp  - cb->s.start) / cb->blksize ==
   2902 		    (einp - cb->s.start) / cb->blksize) {
   2903 			/* Figure out how many bytes to a block boundary. */
   2904 			cc = cb->blksize - (einp - cb->s.start) % cb->blksize;
   2905 			DPRINTF(("audio_write: partial fill %d\n", cc));
   2906 		} else
   2907 			cc = 0;
   2908 		cb->needfill = false;
   2909 		cb->copying = false;
   2910 		if (!vc->sc_pbus && !cb->pause) {
   2911 			saveerror = error;
   2912 			error = audiostartp(sc, vc);
   2913 			if (saveerror != 0) {
   2914 				/* Report the first error that occurred. */
   2915 				error = saveerror;
   2916 			}
   2917 		}
   2918 		if (cc != 0) {
   2919 			DPRINTFN(1, ("audio_write: fill %d\n", cc));
   2920 			audio_fill_silence(&cb->s.param, einp, cc);
   2921 		}
   2922 	}
   2923 
   2924 	return error;
   2925 }
   2926 
   2927 int
   2928 audio_ioctl(dev_t dev, struct audio_softc *sc, u_long cmd, void *addr, int flag,
   2929 	    struct lwp *l, struct audio_chan *chan)
   2930 {
   2931 	const struct audio_hw_if *hw;
   2932 	struct audio_chan *pchan;
   2933 	struct virtual_channel *vc;
   2934 	struct audio_offset *ao;
   2935 	u_long stamp;
   2936 	int error, offs, fd;
   2937 	bool rbus, pbus;
   2938 
   2939 	KASSERT(mutex_owned(sc->sc_lock));
   2940 
   2941 	SIMPLEQ_FOREACH(pchan, &sc->sc_audiochan, entries) {
   2942 		if (pchan->chan == chan->deschan)
   2943 			break;
   2944 	}
   2945 	if (pchan == NULL)
   2946 		return ENXIO;
   2947 
   2948 	vc = pchan->vc;
   2949 
   2950 	DPRINTF(("audio_ioctl(%lu,'%c',%lu)\n",
   2951 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
   2952 	hw = sc->hw_if;
   2953 	if (hw == NULL)
   2954 		return ENXIO;
   2955 	error = 0;
   2956 	switch (cmd) {
   2957 	case AUDIO_SETPROC:
   2958 		if ((int *)addr != NULL && *(int*)addr > 0)
   2959 			chan->deschan = *(int*)addr;
   2960 		break;
   2961 	case FIONBIO:
   2962 		/* All handled in the upper FS layer. */
   2963 		break;
   2964 
   2965 	case FIONREAD:
   2966 		*(int *)addr = audio_stream_get_used(vc->sc_rustream);
   2967 		break;
   2968 
   2969 	case FIOASYNC:
   2970 		if (*(int *)addr) {
   2971 			if (sc->sc_async_audio != 0)
   2972 				error = EBUSY;
   2973 			else
   2974 				sc->sc_async_audio = pchan->chan;
   2975 			DPRINTF(("audio_ioctl: FIOASYNC chan %d\n",
   2976 			    pchan->chan));
   2977 		} else
   2978 			sc->sc_async_audio = 0;
   2979 		break;
   2980 
   2981 	case AUDIO_FLUSH:
   2982 		DPRINTF(("AUDIO_FLUSH\n"));
   2983 		rbus = vc->sc_rbus;
   2984 		pbus = vc->sc_pbus;
   2985 		mutex_enter(sc->sc_intr_lock);
   2986 		audio_clear(sc, vc);
   2987 		error = audio_initbufs(sc, vc);
   2988 		if (error) {
   2989 			mutex_exit(sc->sc_intr_lock);
   2990 			return error;
   2991 		}
   2992 		mutex_exit(sc->sc_intr_lock);
   2993 		if ((vc->sc_mode & AUMODE_PLAY) && !vc->sc_pbus && pbus)
   2994 			error = audiostartp(sc, vc);
   2995 		if (!error &&
   2996 		    (vc->sc_mode & AUMODE_RECORD) && !vc->sc_rbus && rbus)
   2997 			error = audiostartr(sc, vc);
   2998 		break;
   2999 
   3000 	/*
   3001 	 * Number of read (write) samples dropped.  We don't know where or
   3002 	 * when they were dropped.
   3003 	 */
   3004 	case AUDIO_RERROR:
   3005 		*(int *)addr = vc->sc_mrr.drops;
   3006 		break;
   3007 
   3008 	case AUDIO_PERROR:
   3009 		*(int *)addr = vc->sc_mpr.drops;
   3010 		break;
   3011 
   3012 	/*
   3013 	 * Offsets into buffer.
   3014 	 */
   3015 	case AUDIO_GETIOFFS:
   3016 		ao = (struct audio_offset *)addr;
   3017 		HW_LOCK(vc);
   3018 		/* figure out where next DMA will start */
   3019 		stamp = vc->sc_rustream == &vc->sc_mrr.s
   3020 			? vc->sc_mrr.stamp : vc->sc_mrr.fstamp;
   3021 		offs = vc->sc_rustream->inp - vc->sc_rustream->start;
   3022 		HW_UNLOCK(vc);
   3023 		ao->samples = stamp;
   3024 		ao->deltablks =
   3025 		  (stamp / vc->sc_mrr.blksize) -
   3026 		  (vc->sc_mrr.stamp_last / vc->sc_mrr.blksize);
   3027 		vc->sc_mrr.stamp_last = stamp;
   3028 		ao->offset = offs;
   3029 		break;
   3030 
   3031 	case AUDIO_GETOOFFS:
   3032 		ao = (struct audio_offset *)addr;
   3033 		HW_LOCK(vc);
   3034 		/* figure out where next DMA will start */
   3035 		stamp = vc->sc_pustream == &vc->sc_mpr.s
   3036 			? vc->sc_mpr.stamp : vc->sc_mpr.fstamp;
   3037 		offs = vc->sc_pustream->outp - vc->sc_pustream->start
   3038 			+ vc->sc_mpr.blksize;
   3039 		HW_UNLOCK(vc);
   3040 		ao->samples = stamp;
   3041 		ao->deltablks =
   3042 		  (stamp / vc->sc_mpr.blksize) -
   3043 		  (vc->sc_mpr.stamp_last / vc->sc_mpr.blksize);
   3044 		vc->sc_mpr.stamp_last = stamp;
   3045 		if (vc->sc_pustream->start + offs >= vc->sc_pustream->end)
   3046 			offs = 0;
   3047 		ao->offset = offs;
   3048 		break;
   3049 
   3050 	/*
   3051 	 * How many bytes will elapse until mike hears the first
   3052 	 * sample of what we write next?
   3053 	 */
   3054 	case AUDIO_WSEEK:
   3055 		*(u_long *)addr = audio_stream_get_used(vc->sc_pustream);
   3056 		break;
   3057 
   3058 	case AUDIO_SETINFO:
   3059 		DPRINTF(("AUDIO_SETINFO mode=0x%x\n", vc->sc_mode));
   3060 		error = audiosetinfo(sc, (struct audio_info *)addr, false, vc);
   3061 		if (!error && ISDEVSOUND(dev)) {
   3062 			error = audiogetinfo(sc, &sc->sc_ai, 0, vc);
   3063 			sc->sc_aivalid = true;
   3064 		}
   3065 		break;
   3066 
   3067 	case AUDIO_GETINFO:
   3068 		DPRINTF(("AUDIO_GETINFO\n"));
   3069 		error = audiogetinfo(sc, (struct audio_info *)addr, 0, vc);
   3070 		break;
   3071 
   3072 	case AUDIO_GETBUFINFO:
   3073 		DPRINTF(("AUDIO_GETBUFINFO\n"));
   3074 		error = audiogetinfo(sc, (struct audio_info *)addr, 1, vc);
   3075 		break;
   3076 
   3077 	case AUDIO_DRAIN:
   3078 		DPRINTF(("AUDIO_DRAIN\n"));
   3079 		mutex_enter(sc->sc_intr_lock);
   3080 		error = audio_drain(sc, pchan);
   3081 		if (!error && sc->sc_opens == 1 && hw->drain)
   3082 		    error = hw->drain(sc->hw_hdl);
   3083 		mutex_exit(sc->sc_intr_lock);
   3084 		break;
   3085 
   3086 	case AUDIO_GETDEV:
   3087 		DPRINTF(("AUDIO_GETDEV\n"));
   3088 		error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
   3089 		break;
   3090 
   3091 	case AUDIO_GETENC:
   3092 		DPRINTF(("AUDIO_GETENC\n"));
   3093 		error = audio_query_encoding(sc,
   3094 		    (struct audio_encoding *)addr);
   3095 		break;
   3096 
   3097 	case AUDIO_GETFD:
   3098 		DPRINTF(("AUDIO_GETFD\n"));
   3099 		*(int *)addr = vc->sc_full_duplex;
   3100 		break;
   3101 
   3102 	case AUDIO_SETFD:
   3103 		DPRINTF(("AUDIO_SETFD\n"));
   3104 		fd = *(int *)addr;
   3105 		if (audio_get_props(sc) & AUDIO_PROP_FULLDUPLEX) {
   3106 			if (hw->setfd)
   3107 				error = hw->setfd(sc->hw_hdl, fd);
   3108 			else
   3109 				error = 0;
   3110 			if (!error)
   3111 				vc->sc_full_duplex = fd;
   3112 		} else {
   3113 			if (fd)
   3114 				error = ENOTTY;
   3115 			else
   3116 				error = 0;
   3117 		}
   3118 		break;
   3119 
   3120 	case AUDIO_GETPROPS:
   3121 		DPRINTF(("AUDIO_GETPROPS\n"));
   3122 		*(int *)addr = audio_get_props(sc);
   3123 		break;
   3124 
   3125 	default:
   3126 		if (hw->dev_ioctl) {
   3127 			error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l);
   3128 		} else {
   3129 			DPRINTF(("audio_ioctl: unknown ioctl\n"));
   3130 			error = EINVAL;
   3131 		}
   3132 		break;
   3133 	}
   3134 	DPRINTF(("audio_ioctl(%lu,'%c',%lu) result %d\n",
   3135 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
   3136 	return error;
   3137 }
   3138 
   3139 int
   3140 audio_poll(struct audio_softc *sc, int events, struct lwp *l,
   3141 	   struct virtual_channel *vc)
   3142 {
   3143 	int revents;
   3144 	int used;
   3145 
   3146 	KASSERT(mutex_owned(sc->sc_lock));
   3147 
   3148 	DPRINTF(("audio_poll: events=0x%x mode=%d\n", events, vc->sc_mode));
   3149 
   3150 	revents = 0;
   3151 	HW_LOCK(vc);
   3152 	if (events & (POLLIN | POLLRDNORM)) {
   3153 		used = audio_stream_get_used(vc->sc_rustream);
   3154 		/*
   3155 		 * If half duplex and playing, audio_read() will generate
   3156 		 * silence at the play rate; poll for silence being
   3157 		 * available.  Otherwise, poll for recorded sound.
   3158 		 */
   3159 		if ((!vc->sc_full_duplex && (vc->sc_mode & AUMODE_PLAY))
   3160 		     ? vc->sc_mpr.stamp > vc->sc_wstamp :
   3161 		    used > vc->sc_mrr.usedlow)
   3162 			revents |= events & (POLLIN | POLLRDNORM);
   3163 	}
   3164 
   3165 	if (events & (POLLOUT | POLLWRNORM)) {
   3166 		used = audio_stream_get_used(vc->sc_pustream);
   3167 		/*
   3168 		 * If half duplex and recording, audio_write() will throw
   3169 		 * away play data, which means we are always ready to write.
   3170 		 * Otherwise, poll for play buffer being below its low water
   3171 		 * mark.
   3172 		 */
   3173 		if ((!vc->sc_full_duplex && (vc->sc_mode & AUMODE_RECORD)) ||
   3174 		    (!(vc->sc_mode & AUMODE_PLAY_ALL) && vc->sc_playdrop > 0) ||
   3175 		    (used <= vc->sc_mpr.usedlow))
   3176 			revents |= events & (POLLOUT | POLLWRNORM);
   3177 	}
   3178 	HW_UNLOCK(vc);
   3179 
   3180 	if (revents == 0) {
   3181 		if (events & (POLLIN | POLLRDNORM))
   3182 			selrecord(l, &sc->sc_rsel);
   3183 
   3184 		if (events & (POLLOUT | POLLWRNORM))
   3185 			selrecord(l, &sc->sc_wsel);
   3186 	}
   3187 
   3188 	return revents;
   3189 }
   3190 
   3191 static void
   3192 filt_audiordetach(struct knote *kn)
   3193 {
   3194 	struct audio_softc *sc;
   3195 	struct audio_chan *chan;
   3196 	dev_t dev;
   3197 
   3198 	chan = kn->kn_hook;
   3199 	dev = chan->dev;
   3200 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   3201 	if (sc == NULL)
   3202 		return;
   3203 
   3204 
   3205 	mutex_enter(sc->sc_intr_lock);
   3206 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
   3207 	mutex_exit(sc->sc_intr_lock);
   3208 }
   3209 
   3210 static int
   3211 filt_audioread(struct knote *kn, long hint)
   3212 {
   3213 	struct audio_softc *sc;
   3214 	struct audio_chan *chan;
   3215 	struct virtual_channel *vc;
   3216 	dev_t dev;
   3217 
   3218 	chan = kn->kn_hook;
   3219 	dev = chan->dev;
   3220 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   3221 	if (sc == NULL)
   3222 		return ENXIO;
   3223 
   3224 	vc = chan->vc;
   3225 	mutex_enter(sc->sc_intr_lock);
   3226 	if (!vc->sc_full_duplex && (vc->sc_mode & AUMODE_PLAY))
   3227 		kn->kn_data = vc->sc_mpr.stamp - vc->sc_wstamp;
   3228 	else
   3229 		kn->kn_data = audio_stream_get_used(vc->sc_rustream)
   3230 			- vc->sc_mrr.usedlow;
   3231 	mutex_exit(sc->sc_intr_lock);
   3232 
   3233 	return kn->kn_data > 0;
   3234 }
   3235 
   3236 static const struct filterops audioread_filtops =
   3237 	{ 1, NULL, filt_audiordetach, filt_audioread };
   3238 
   3239 static void
   3240 filt_audiowdetach(struct knote *kn)
   3241 {
   3242 	struct audio_softc *sc;
   3243 	struct audio_chan *chan;
   3244 	dev_t dev;
   3245 
   3246 	chan = kn->kn_hook;
   3247 	dev = chan->dev;
   3248 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   3249 	if (sc == NULL)
   3250 		return;
   3251 
   3252 	mutex_enter(sc->sc_intr_lock);
   3253 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
   3254 	mutex_exit(sc->sc_intr_lock);
   3255 }
   3256 
   3257 static int
   3258 filt_audiowrite(struct knote *kn, long hint)
   3259 {
   3260 	struct audio_softc *sc;
   3261 	struct audio_chan *chan;
   3262 	audio_stream_t *stream;
   3263 	dev_t dev;
   3264 
   3265 	chan = kn->kn_hook;
   3266 	dev = chan->dev;
   3267 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   3268 	if (sc == NULL)
   3269 		return ENXIO;
   3270 
   3271 	mutex_enter(sc->sc_intr_lock);
   3272 
   3273 	stream = chan->vc->sc_pustream;
   3274 	kn->kn_data = (stream->end - stream->start)
   3275 		- audio_stream_get_used(stream);
   3276 	mutex_exit(sc->sc_intr_lock);
   3277 
   3278 	return kn->kn_data > 0;
   3279 }
   3280 
   3281 static const struct filterops audiowrite_filtops =
   3282 	{ 1, NULL, filt_audiowdetach, filt_audiowrite };
   3283 
   3284 int
   3285 audio_kqfilter(struct audio_chan *chan, struct knote *kn)
   3286 {
   3287 	struct audio_softc *sc;
   3288 	struct klist *klist;
   3289 	dev_t dev;
   3290 
   3291 	dev = chan->dev;
   3292 
   3293 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   3294 	if (sc == NULL)
   3295 		return ENXIO;
   3296 
   3297 	switch (kn->kn_filter) {
   3298 	case EVFILT_READ:
   3299 		klist = &sc->sc_rsel.sel_klist;
   3300 		kn->kn_fop = &audioread_filtops;
   3301 		break;
   3302 
   3303 	case EVFILT_WRITE:
   3304 		klist = &sc->sc_wsel.sel_klist;
   3305 		kn->kn_fop = &audiowrite_filtops;
   3306 		break;
   3307 
   3308 	default:
   3309 		return EINVAL;
   3310 	}
   3311 
   3312 	kn->kn_hook = chan;
   3313 
   3314 	mutex_enter(sc->sc_intr_lock);
   3315 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   3316 	mutex_exit(sc->sc_intr_lock);
   3317 
   3318 	return 0;
   3319 }
   3320 
   3321 /* XXX:NS mmap to be fixed. */
   3322 paddr_t
   3323 audio_mmap(struct audio_softc *sc, off_t off, int prot,
   3324 	   struct virtual_channel *vc)
   3325 {
   3326 	struct audio_ringbuffer *cb;
   3327 	paddr_t rv;
   3328 
   3329 	KASSERT(mutex_owned(sc->sc_lock));
   3330 
   3331 	if (sc->hw_if == NULL)
   3332 		return ENXIO;
   3333 
   3334 	DPRINTF(("audio_mmap: off=%lld, prot=%d\n", (long long)off, prot));
   3335 	if (!(audio_get_props(sc) & AUDIO_PROP_MMAP))
   3336 		return -1;
   3337 #if 0
   3338 /* XXX
   3339  * The idea here was to use the protection to determine if
   3340  * we are mapping the read or write buffer, but it fails.
   3341  * The VM system is broken in (at least) two ways.
   3342  * 1) If you map memory VM_PROT_WRITE you SIGSEGV
   3343  *    when writing to it, so VM_PROT_READ|VM_PROT_WRITE
   3344  *    has to be used for mmapping the play buffer.
   3345  * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
   3346  *    audio_mmap will get called at some point with VM_PROT_READ
   3347  *    only.
   3348  * So, alas, we always map the play buffer for now.
   3349  */
   3350 	if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
   3351 	    prot == VM_PROT_WRITE)
   3352 		cb = &vc->sc_mpr;
   3353 	else if (prot == VM_PROT_READ)
   3354 		cb = &vc->sc_mrr;
   3355 	else
   3356 		return -1;
   3357 #else
   3358 	cb = &vc->sc_mpr;
   3359 #endif
   3360 
   3361 	if ((u_int)off >= cb->s.bufsize)
   3362 		return -1;
   3363 	if (!cb->mmapped) {
   3364 		cb->mmapped = true;
   3365 		if (cb != &sc->sc_rr) {
   3366 			audio_fill_silence(&cb->s.param, cb->s.start,
   3367 					   cb->s.bufsize);
   3368 			vc->sc_pustream = &cb->s;
   3369 			if (!vc->sc_pbus && !vc->sc_mpr.pause)
   3370 				(void)audiostartp(sc, vc);
   3371 		} else {
   3372 			vc->sc_rustream = &cb->s;
   3373 			if (!vc->sc_rbus && !sc->sc_rr.pause)
   3374 				(void)audiostartr(sc, vc);
   3375 		}
   3376 	}
   3377 
   3378 	rv = (paddr_t)(uintptr_t)(cb->s.start + off);
   3379 
   3380 	return rv;
   3381 }
   3382 
   3383 int
   3384 audiostartr(struct audio_softc *sc, struct virtual_channel *vc)
   3385 {
   3386 
   3387 	KASSERT(mutex_owned(sc->sc_lock));
   3388 
   3389 	DPRINTF(("audiostartr: start=%p used=%d(hi=%d) mmapped=%d\n",
   3390 		 vc->sc_mrr.s.start, audio_stream_get_used(&vc->sc_mrr.s),
   3391 		 vc->sc_mrr.usedhigh, vc->sc_mrr.mmapped));
   3392 
   3393 	if (!audio_can_capture(sc))
   3394 		return EINVAL;
   3395 
   3396 	if (sc->sc_rec_started == false) {
   3397 		mutex_enter(sc->sc_intr_lock);
   3398 		mix_read(sc);
   3399 		cv_broadcast(&sc->sc_rcondvar);
   3400 		mutex_exit(sc->sc_intr_lock);
   3401 	}
   3402 	vc->sc_rbus = true;
   3403 
   3404 	return 0;
   3405 }
   3406 
   3407 int
   3408 audiostartp(struct audio_softc *sc, struct virtual_channel *vc)
   3409 {
   3410 	struct audio_chan *chan;
   3411 	int error, used;
   3412 
   3413 	KASSERT(mutex_owned(sc->sc_lock));
   3414 
   3415 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   3416 	error = 0;
   3417 	used = audio_stream_get_used(&vc->sc_mpr.s);
   3418 	DPRINTF(("audiostartp: start=%p used=%d(hi=%d blk=%d) mmapped=%d\n",
   3419 		 vc->sc_mpr.s.start, used, vc->sc_mpr.usedhigh,
   3420 		 vc->sc_mpr.blksize, vc->sc_mpr.mmapped));
   3421 
   3422 	if (!audio_can_playback(sc))
   3423 		return EINVAL;
   3424 
   3425 	if (!vc->sc_mpr.mmapped && used < vc->sc_mpr.blksize) {
   3426 		cv_broadcast(&sc->sc_wchan);
   3427 		DPRINTF(("%s: wakeup and return\n", __func__));
   3428 		return 0;
   3429 	}
   3430 
   3431 	vc->sc_pbus = true;
   3432 	if (sc->sc_trigger_started == false) {
   3433 		audio_mix(sc);
   3434 
   3435 		vc = chan->vc;
   3436 		vc->sc_mpr.s.outp =
   3437 		    audio_stream_add_outp(&vc->sc_mpr.s,
   3438 		      vc->sc_mpr.s.outp, vc->sc_mpr.blksize);
   3439 		audio_mix(sc);
   3440 		mutex_enter(sc->sc_intr_lock);
   3441 		mix_write(sc);
   3442 		mix_write(sc);
   3443 		cv_broadcast(&sc->sc_condvar);
   3444 		mutex_exit(sc->sc_intr_lock);
   3445 	}
   3446 
   3447 	return error;
   3448 }
   3449 
   3450 /*
   3451  * When the play interrupt routine finds that the write isn't keeping
   3452  * the buffer filled it will insert silence in the buffer to make up
   3453  * for this.  The part of the buffer that is filled with silence
   3454  * is kept track of in a very approximate way: it starts at sc_sil_start
   3455  * and extends sc_sil_count bytes.  If there is already silence in
   3456  * the requested area nothing is done; so when the whole buffer is
   3457  * silent nothing happens.  When the writer starts again sc_sil_count
   3458  * is set to 0.
   3459  *
   3460  * XXX
   3461  * Putting silence into the output buffer should not really be done
   3462  * from the device interrupt handler.  Consider deferring to the soft
   3463  * interrupt.
   3464  */
   3465 static inline void
   3466 audio_pint_silence(struct audio_softc *sc, struct audio_ringbuffer *cb,
   3467 		   uint8_t *inp, int cc, struct virtual_channel *vc)
   3468 {
   3469 	uint8_t *s, *e, *p, *q;
   3470 
   3471 	KASSERT(mutex_owned(sc->sc_lock));
   3472 
   3473 	if (vc->sc_sil_count > 0) {
   3474 		s = vc->sc_sil_start; /* start of silence */
   3475 		e = s + vc->sc_sil_count; /* end of sil., may be beyond end */
   3476 		p = inp;	/* adjusted pointer to area to fill */
   3477 		if (p < s)
   3478 			p += cb->s.end - cb->s.start;
   3479 		q = p + cc;
   3480 		/* Check if there is already silence. */
   3481 		if (!(s <= p && p <  e &&
   3482 		      s <= q && q <= e)) {
   3483 			if (s <= p)
   3484 				vc->sc_sil_count = max(vc->sc_sil_count, q-s);
   3485 			DPRINTFN(5,("audio_pint_silence: fill cc=%d inp=%p, "
   3486 				    "count=%d size=%d\n",
   3487 				    cc, inp, vc->sc_sil_count,
   3488 				    (int)(cb->s.end - cb->s.start)));
   3489 			audio_fill_silence(&cb->s.param, inp, cc);
   3490 		} else {
   3491 			DPRINTFN(5,("audio_pint_silence: already silent "
   3492 				    "cc=%d inp=%p\n", cc, inp));
   3493 
   3494 		}
   3495 	} else {
   3496 		vc->sc_sil_start = inp;
   3497 		vc->sc_sil_count = cc;
   3498 		DPRINTFN(5, ("audio_pint_silence: start fill %p %d\n",
   3499 			     inp, cc));
   3500 		audio_fill_silence(&cb->s.param, inp, cc);
   3501 	}
   3502 }
   3503 
   3504 static void
   3505 audio_softintr_rd(void *cookie)
   3506 {
   3507 	struct audio_softc *sc = cookie;
   3508 	proc_t *p;
   3509 	pid_t pid;
   3510 
   3511 	mutex_enter(sc->sc_lock);
   3512 	cv_broadcast(&sc->sc_rchan);
   3513 	selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
   3514 	if ((pid = sc->sc_async_audio) != 0) {
   3515 		DPRINTFN(3, ("audio_softintr_rd: sending SIGIO %d\n", pid));
   3516 		mutex_enter(proc_lock);
   3517 		if ((p = proc_find(pid)) != NULL)
   3518 			psignal(p, SIGIO);
   3519 		mutex_exit(proc_lock);
   3520 	}
   3521 	mutex_exit(sc->sc_lock);
   3522 }
   3523 
   3524 static void
   3525 audio_softintr_wr(void *cookie)
   3526 {
   3527 	struct audio_softc *sc = cookie;
   3528 	proc_t *p;
   3529 	pid_t pid;
   3530 
   3531 	mutex_enter(sc->sc_lock);
   3532 	cv_broadcast(&sc->sc_wchan);
   3533 	selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT);
   3534 	if ((pid = sc->sc_async_audio) != 0) {
   3535 		DPRINTFN(3, ("audio_softintr_wr: sending SIGIO %d\n", pid));
   3536 		mutex_enter(proc_lock);
   3537 		if ((p = proc_find(pid)) != NULL)
   3538 			psignal(p, SIGIO);
   3539 		mutex_exit(proc_lock);
   3540 	}
   3541 	mutex_exit(sc->sc_lock);
   3542 }
   3543 
   3544 /*
   3545  * Called from HW driver module on completion of DMA output.
   3546  * Start output of new block, wrap in ring buffer if needed.
   3547  * If no more buffers to play, output zero instead.
   3548  * Do a wakeup if necessary.
   3549  */
   3550 void
   3551 audio_pint(void *v)
   3552 {
   3553 	struct audio_softc *sc;
   3554 	struct audio_chan *chan;
   3555 	struct virtual_channel *vc;
   3556 	int blksize;
   3557 
   3558 	sc = v;
   3559 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   3560 	vc = chan->vc;
   3561 	blksize = vc->sc_mpr.blksize;
   3562 
   3563 	if (sc->sc_dying == true)
   3564 		return;
   3565 
   3566 	if (vc->sc_draining == true) {
   3567 		vc->sc_mpr.drops += blksize;
   3568 		cv_broadcast(&sc->sc_wchan);
   3569 	}
   3570 
   3571 	if (audio_stream_get_used(&sc->sc_pr.s) < blksize)
   3572 		goto wake_mix;
   3573 
   3574 	vc->sc_mpr.s.outp = audio_stream_add_outp(&vc->sc_mpr.s,
   3575 	    vc->sc_mpr.s.outp, blksize);
   3576 
   3577 	mix_write(sc);
   3578 
   3579 wake_mix:
   3580 	cv_broadcast(&sc->sc_condvar);
   3581 }
   3582 
   3583 void
   3584 audio_mix(void *v)
   3585 {
   3586 	stream_fetcher_t null_fetcher;
   3587 	struct audio_softc *sc;
   3588 	struct audio_chan *chan;
   3589 	struct virtual_channel *vc;
   3590 	struct audio_ringbuffer *cb;
   3591 	stream_fetcher_t *fetcher;
   3592 	uint8_t *inp;
   3593 	int cc, used, blksize;
   3594 
   3595 	sc = v;
   3596 
   3597 	DPRINTF(("PINT MIX\n"));
   3598 	sc->schedule_rih = false;
   3599 	sc->schedule_wih = false;
   3600 	sc->sc_writeme = false;
   3601 
   3602 	if (sc->sc_dying == true)
   3603 		return;
   3604 
   3605 	blksize = sc->sc_pr.blksize;
   3606 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
   3607 		if (!sc->sc_opens)
   3608 			break;		/* ignore interrupt if not open */
   3609 
   3610 		if (chan == NULL)
   3611 			break;
   3612 
   3613 		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan))
   3614 			continue;
   3615 
   3616 		if (chan->chan == MIXER_INUSE)
   3617 			continue;
   3618 
   3619 		vc = chan->vc;
   3620 
   3621 		if (!vc->sc_open)
   3622 			continue;
   3623 		if (!vc->sc_pbus)
   3624 			continue;
   3625 
   3626 		cb = &vc->sc_mpr;
   3627 
   3628 		sc->sc_writeme = true;
   3629 
   3630 		inp = cb->s.inp;
   3631 		cb->stamp += blksize;
   3632 		if (cb->mmapped) {
   3633 			DPRINTF(("audio_pint: mmapped outp=%p cc=%d inp=%p\n",
   3634 				     cb->s.outp, blksize, cb->s.inp));
   3635 			mutex_enter(sc->sc_intr_lock);
   3636 			mix_func(sc, cb, vc);
   3637 			mutex_exit(sc->sc_intr_lock);
   3638 			continue;
   3639 		}
   3640 
   3641 #ifdef AUDIO_INTR_TIME
   3642 		{
   3643 			struct timeval tv;
   3644 			u_long t;
   3645 			microtime(&tv);
   3646 			t = tv.tv_usec + 1000000 * tv.tv_sec;
   3647 			if (sc->sc_pnintr) {
   3648 				long lastdelta, totdelta;
   3649 				lastdelta = t - sc->sc_plastintr -
   3650 				    sc->sc_pblktime;
   3651 				if (lastdelta > sc->sc_pblktime / 3) {
   3652 					printf("audio: play interrupt(%d) off "
   3653 				       "relative by %ld us (%lu)\n",
   3654 					       sc->sc_pnintr, lastdelta,
   3655 					       sc->sc_pblktime);
   3656 				}
   3657 				totdelta = t - sc->sc_pfirstintr -
   3658 					sc->sc_pblktime * sc->sc_pnintr;
   3659 				if (totdelta > sc->sc_pblktime) {
   3660 					printf("audio: play interrupt(%d) "
   3661 					       "off absolute by %ld us (%lu) "
   3662 					       "(LOST)\n", sc->sc_pnintr,
   3663 					       totdelta, sc->sc_pblktime);
   3664 					sc->sc_pnintr++;
   3665 					/* avoid repeated messages */
   3666 				}
   3667 			} else
   3668 				sc->sc_pfirstintr = t;
   3669 			sc->sc_plastintr = t;
   3670 			sc->sc_pnintr++;
   3671 		}
   3672 #endif
   3673 
   3674 		used = audio_stream_get_used(&cb->s);
   3675 		/*
   3676 		 * "used <= cb->usedlow" should be "used < blksize" ideally.
   3677 		 * Some HW drivers such as uaudio(4) does not call audio_pint()
   3678 		 * at accurate timing.  If used < blksize, uaudio(4) already
   3679 		 * request transfer of garbage data.
   3680 		 */
   3681 		if (used <= cb->usedlow && !cb->copying &&
   3682 		    vc->sc_npfilters > 0) {
   3683 			/* we might have data in filter pipeline */
   3684 			null_fetcher.fetch_to = null_fetcher_fetch_to;
   3685 			fetcher = &vc->sc_pfilters[vc->sc_npfilters - 1]->base;
   3686 			vc->sc_pfilters[0]->set_fetcher(vc->sc_pfilters[0],
   3687 							&null_fetcher);
   3688 			used = audio_stream_get_used(vc->sc_pustream);
   3689 			cc = cb->s.end - cb->s.start;
   3690 			if (blksize * 2 < cc)
   3691 				cc = blksize * 2;
   3692 			fetcher->fetch_to(sc, fetcher, &cb->s, cc);
   3693 			cb->fstamp += used -
   3694 			    audio_stream_get_used(vc->sc_pustream);
   3695 			used = audio_stream_get_used(&cb->s);
   3696 		}
   3697 		if (used < blksize) {
   3698 			/* we don't have a full block to use */
   3699 			if (cb->copying) {
   3700 				/* writer is in progress, don't disturb */
   3701 				cb->needfill = true;
   3702 				DPRINTFN(1, ("audio_pint: copying in "
   3703 					 "progress\n"));
   3704 			} else {
   3705 				inp = cb->s.inp;
   3706 				cc = blksize - (inp - cb->s.start) % blksize;
   3707 				if (cb->pause)
   3708 					cb->pdrops += cc;
   3709 				else {
   3710 					cb->drops += cc;
   3711 					vc->sc_playdrop += cc;
   3712 				}
   3713 
   3714 				audio_pint_silence(sc, cb, inp, cc, vc);
   3715 				cb->s.inp = audio_stream_add_inp(&cb->s, inp,
   3716 				    cc);
   3717 
   3718 				/* Clear next block to keep ahead of the DMA. */
   3719 				used = audio_stream_get_used(&cb->s);
   3720 				if (used + blksize < cb->s.end - cb->s.start) {
   3721 					audio_pint_silence(sc, cb, cb->s.inp,
   3722 					    blksize, vc);
   3723 				}
   3724 			}
   3725 		}
   3726 
   3727 		DPRINTFN(5, ("audio_pint: outp=%p cc=%d\n", cb->s.outp,
   3728 			 blksize));
   3729 		mutex_enter(sc->sc_intr_lock);
   3730 		mix_func(sc, cb, vc);
   3731 		mutex_exit(sc->sc_intr_lock);
   3732 		cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp, blksize);
   3733 
   3734 		DPRINTFN(2, ("audio_pint: mode=%d pause=%d used=%d lowat=%d\n",
   3735 			     vc->sc_mode, cb->pause,
   3736 			     audio_stream_get_used(vc->sc_pustream),
   3737 			     cb->usedlow));
   3738 
   3739 		if ((vc->sc_mode & AUMODE_PLAY) && !cb->pause) {
   3740 			if (audio_stream_get_used(&cb->s) <= cb->usedlow)
   3741 				sc->schedule_wih = true;
   3742 		}
   3743 		/* Possible to return one or more "phantom blocks" now. */
   3744 		if (!vc->sc_full_duplex && vc->sc_mode & AUMODE_RECORD)
   3745 				sc->schedule_rih = true;
   3746 	}
   3747 	mutex_enter(sc->sc_intr_lock);
   3748 	if (sc->sc_saturate == true && sc->sc_opens > 1)
   3749 		saturate_func(sc);
   3750 
   3751 	cb = &sc->sc_pr;
   3752 	if (sc->sc_writeme == true)
   3753 		cb->s.inp = audio_stream_add_inp(&cb->s, cb->s.inp, blksize);
   3754 	mutex_exit(sc->sc_intr_lock);
   3755 
   3756 	kpreempt_disable();
   3757 	if (sc->schedule_wih == true)
   3758 		softint_schedule(sc->sc_sih_wr);
   3759 
   3760 	if (sc->schedule_rih == true)
   3761 		softint_schedule(sc->sc_sih_rd);
   3762 	kpreempt_enable();
   3763 }
   3764 
   3765 /*
   3766  * Called from HW driver module on completion of DMA input.
   3767  * Mark it as input in the ring buffer (fiddle pointers).
   3768  * Do a wakeup if necessary.
   3769  */
   3770 void
   3771 audio_rint(void *v)
   3772 {
   3773 	struct audio_softc *sc;
   3774 	int blksize;
   3775 
   3776 	sc = v;
   3777 
   3778 	KASSERT(mutex_owned(sc->sc_intr_lock));
   3779 
   3780 	if (sc->sc_dying == true)
   3781 		return;
   3782 
   3783 	blksize = audio_stream_get_used(&sc->sc_rr.s);
   3784 	sc->sc_rr.s.outp = audio_stream_add_outp(&sc->sc_rr.s,
   3785 	    sc->sc_rr.s.outp, blksize);
   3786 	mix_read(sc);
   3787 
   3788 	cv_broadcast(&sc->sc_rcondvar);
   3789 }
   3790 
   3791 void
   3792 audio_upmix(void *v)
   3793 {
   3794 	stream_fetcher_t null_fetcher;
   3795 	struct audio_softc *sc;
   3796 	struct audio_chan *chan;
   3797 	struct audio_ringbuffer *cb;
   3798 	stream_fetcher_t *last_fetcher;
   3799 	struct virtual_channel *vc;
   3800 	int cc, used, blksize, cc1;
   3801 
   3802 	sc = v;
   3803 	blksize = sc->sc_rr.blksize;
   3804 
   3805 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
   3806 		if (!sc->sc_opens)
   3807 			break;		/* ignore interrupt if not open */
   3808 
   3809 		if (chan == NULL)
   3810 			break;
   3811 
   3812 		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan))
   3813 			continue;
   3814 
   3815 		if (chan->chan == MIXER_INUSE)
   3816 			continue;
   3817 
   3818 		vc = chan->vc;
   3819 
   3820 		if (!(vc->sc_open & AUOPEN_READ))
   3821 			continue;
   3822 		if (!vc->sc_rbus)
   3823 			continue;
   3824 
   3825 		cb = &vc->sc_mrr;
   3826 
   3827 		blksize = audio_stream_get_used(&sc->sc_rr.s);
   3828 		if (audio_stream_get_space(&cb->s) < blksize) {
   3829 			cb->drops += blksize;
   3830 			cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp,
   3831 			    sc->sc_rr.blksize);
   3832 			continue;
   3833 		}
   3834 
   3835 		cc = blksize;
   3836 		if (cb->s.inp + blksize > cb->s.end)
   3837 			cc = cb->s.end - cb->s.inp;
   3838 		mutex_enter(sc->sc_intr_lock);
   3839 		memcpy(cb->s.inp, sc->sc_rr.s.start, cc);
   3840 		if (cc < blksize && cc != 0) {
   3841 			cc1 = cc;
   3842 			cc = blksize - cc;
   3843 			memcpy(cb->s.start, sc->sc_rr.s.start + cc1, cc);
   3844 		}
   3845 		mutex_exit(sc->sc_intr_lock);
   3846 
   3847 		cc = blksize;
   3848 		recswvol_func(sc, cb, blksize, vc);
   3849 
   3850 		cb->s.inp = audio_stream_add_inp(&cb->s, cb->s.inp, blksize);
   3851 		cb->stamp += blksize;
   3852 		if (cb->mmapped) {
   3853 			DPRINTFN(2, ("audio_rint: mmapped inp=%p cc=%d\n",
   3854 			     	cb->s.inp, blksize));
   3855 			continue;
   3856 		}
   3857 
   3858 #ifdef AUDIO_INTR_TIME
   3859 		{
   3860 			struct timeval tv;
   3861 			u_long t;
   3862 			microtime(&tv);
   3863 			t = tv.tv_usec + 1000000 * tv.tv_sec;
   3864 			if (sc->sc_rnintr) {
   3865 				long lastdelta, totdelta;
   3866 				lastdelta = t - sc->sc_rlastintr -
   3867 				    sc->sc_rblktime;
   3868 				if (lastdelta > sc->sc_rblktime / 5) {
   3869 					printf("audio: record interrupt(%d) "
   3870 					       "off relative by %ld us (%lu)\n",
   3871 					       sc->sc_rnintr, lastdelta,
   3872 					       sc->sc_rblktime);
   3873 				}
   3874 				totdelta = t - sc->sc_rfirstintr -
   3875 					sc->sc_rblktime * sc->sc_rnintr;
   3876 				if (totdelta > sc->sc_rblktime / 2) {
   3877 					sc->sc_rnintr++;
   3878 					printf("audio: record interrupt(%d) "
   3879 					       "off absolute by %ld us (%lu)\n",
   3880 					       sc->sc_rnintr, totdelta,
   3881 					       sc->sc_rblktime);
   3882 					sc->sc_rnintr++;
   3883 					/* avoid repeated messages */
   3884 				}
   3885 			} else
   3886 				sc->sc_rfirstintr = t;
   3887 			sc->sc_rlastintr = t;
   3888 			sc->sc_rnintr++;
   3889 		}
   3890 #endif
   3891 
   3892 		if (!cb->pause && vc->sc_nrfilters > 0) {
   3893 			null_fetcher.fetch_to = null_fetcher_fetch_to;
   3894 			last_fetcher =
   3895 			    &vc->sc_rfilters[vc->sc_nrfilters - 1]->base;
   3896 			vc->sc_rfilters[0]->set_fetcher(vc->sc_rfilters[0],
   3897 							&null_fetcher);
   3898 			used = audio_stream_get_used(vc->sc_rustream);
   3899 			cc = vc->sc_rustream->end - vc->sc_rustream->start;
   3900 			last_fetcher->fetch_to
   3901 				(sc, last_fetcher, vc->sc_rustream, cc);
   3902 			cb->fstamp += audio_stream_get_used(vc->sc_rustream) -
   3903 			    used;
   3904 			/* XXX what should do for error? */
   3905 		}
   3906 		used = audio_stream_get_used(&vc->sc_mrr.s);
   3907 		if (cb->pause) {
   3908 			DPRINTFN(1, ("audio_rint: pdrops %lu\n", cb->pdrops));
   3909 			cb->pdrops += blksize;
   3910 			cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp,
   3911 			    blksize);
   3912 		} else if (used + blksize > cb->s.end - cb->s.start &&
   3913 								!cb->copying) {
   3914 			DPRINTFN(1, ("audio_rint: drops %lu\n", cb->drops));
   3915 			cb->drops += blksize;
   3916 			cb->s.outp = audio_stream_add_outp(&cb->s, cb->s.outp,
   3917 			    blksize);
   3918 		}
   3919 	}
   3920 	kpreempt_disable();
   3921 	softint_schedule(sc->sc_sih_rd);
   3922 	kpreempt_enable();
   3923 }
   3924 
   3925 int
   3926 audio_check_params(struct audio_params *p)
   3927 {
   3928 
   3929 	if (p->encoding == AUDIO_ENCODING_PCM16) {
   3930 		if (p->precision == 8)
   3931 			p->encoding = AUDIO_ENCODING_ULINEAR;
   3932 		else
   3933 			p->encoding = AUDIO_ENCODING_SLINEAR;
   3934 	} else if (p->encoding == AUDIO_ENCODING_PCM8) {
   3935 		if (p->precision == 8)
   3936 			p->encoding = AUDIO_ENCODING_ULINEAR;
   3937 		else
   3938 			return EINVAL;
   3939 	}
   3940 
   3941 	if (p->encoding == AUDIO_ENCODING_SLINEAR)
   3942 #if BYTE_ORDER == LITTLE_ENDIAN
   3943 		p->encoding = AUDIO_ENCODING_SLINEAR_LE;
   3944 #else
   3945 		p->encoding = AUDIO_ENCODING_SLINEAR_BE;
   3946 #endif
   3947 	if (p->encoding == AUDIO_ENCODING_ULINEAR)
   3948 #if BYTE_ORDER == LITTLE_ENDIAN
   3949 		p->encoding = AUDIO_ENCODING_ULINEAR_LE;
   3950 #else
   3951 		p->encoding = AUDIO_ENCODING_ULINEAR_BE;
   3952 #endif
   3953 
   3954 	switch (p->encoding) {
   3955 	case AUDIO_ENCODING_ULAW:
   3956 	case AUDIO_ENCODING_ALAW:
   3957 		if (p->precision != 8)
   3958 			return EINVAL;
   3959 		break;
   3960 	case AUDIO_ENCODING_ADPCM:
   3961 		if (p->precision != 4 && p->precision != 8)
   3962 			return EINVAL;
   3963 		break;
   3964 	case AUDIO_ENCODING_SLINEAR_LE:
   3965 	case AUDIO_ENCODING_SLINEAR_BE:
   3966 	case AUDIO_ENCODING_ULINEAR_LE:
   3967 	case AUDIO_ENCODING_ULINEAR_BE:
   3968 		/* XXX is: our zero-fill can handle any multiple of 8 */
   3969 		if (p->precision !=  8 && p->precision != 16 &&
   3970 		    p->precision != 24 && p->precision != 32)
   3971 			return EINVAL;
   3972 		if (p->precision == 8 && p->encoding ==
   3973 		    AUDIO_ENCODING_SLINEAR_BE)
   3974 			p->encoding = AUDIO_ENCODING_SLINEAR_LE;
   3975 		if (p->precision == 8 && p->encoding ==
   3976 		    AUDIO_ENCODING_ULINEAR_BE)
   3977 			p->encoding = AUDIO_ENCODING_ULINEAR_LE;
   3978 		if (p->validbits > p->precision)
   3979 			return EINVAL;
   3980 		break;
   3981 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   3982 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   3983 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   3984 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   3985 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   3986 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   3987 	case AUDIO_ENCODING_AC3:
   3988 		break;
   3989 	default:
   3990 		return EINVAL;
   3991 	}
   3992 
   3993 	/* sanity check # of channels*/
   3994 	if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS)
   3995 		return EINVAL;
   3996 
   3997 	return 0;
   3998 }
   3999 
   4000 static int
   4001 audio_set_vchan_defaults(struct audio_softc *sc, u_int mode,
   4002      const struct audio_format *format)
   4003 {
   4004 	struct audio_chan *chan;
   4005 	struct virtual_channel *vc;
   4006 	struct audio_info ai;
   4007 	int error;
   4008 
   4009 	KASSERT(mutex_owned(sc->sc_lock));
   4010 
   4011 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   4012 	if (chan == NULL)
   4013 		return EINVAL;
   4014 	vc = chan->vc;
   4015 
   4016 	sc->sc_vchan_params.sample_rate = sc->sc_iffreq;
   4017 #if BYTE_ORDER == LITTLE_ENDIAN
   4018 	sc->sc_vchan_params.encoding = AUDIO_ENCODING_SLINEAR_LE;
   4019 #else
   4020 	sc->sc_vchan_params.encoding = AUDIO_ENCODING_SLINEAR_BE;
   4021 #endif
   4022 	sc->sc_vchan_params.precision = sc->sc_precision;
   4023 	sc->sc_vchan_params.validbits = sc->sc_precision;
   4024 	sc->sc_vchan_params.channels = sc->sc_channels;
   4025 
   4026 	/* default parameters */
   4027 	vc->sc_rparams = sc->sc_vchan_params;
   4028 	vc->sc_pparams = sc->sc_vchan_params;
   4029 	vc->sc_blkset = false;
   4030 
   4031 	AUDIO_INITINFO(&ai);
   4032 	ai.record.sample_rate = sc->sc_iffreq;
   4033 	ai.record.encoding    = format->encoding;
   4034 	ai.record.channels    = sc->sc_channels;
   4035 	ai.record.precision   = sc->sc_precision;
   4036 	ai.record.pause	      = false;
   4037 	ai.play.sample_rate   = sc->sc_iffreq;
   4038 	ai.play.encoding      = format->encoding;
   4039 	ai.play.channels      = sc->sc_channels;
   4040 	ai.play.precision     = sc->sc_precision;
   4041 	ai.play.pause         = false;
   4042 	ai.mode		      = mode;
   4043 
   4044 	sc->sc_format->channels = sc->sc_channels;
   4045 	sc->sc_format->precision = sc->sc_precision;
   4046 	sc->sc_format->validbits = sc->sc_precision;
   4047 	sc->sc_format->frequency[0] = sc->sc_iffreq;
   4048 
   4049 	auconv_delete_encodings(sc->sc_encodings);
   4050 	error = auconv_create_encodings(sc->sc_format, VAUDIO_NFORMATS,
   4051 	    &sc->sc_encodings);
   4052 
   4053 	if (error == 0)
   4054 		error = audiosetinfo(sc, &ai, true, vc);
   4055 
   4056 	sc->sc_pr.blksize = vc->sc_mpr.blksize;
   4057 	sc->sc_rr.blksize = vc->sc_mrr.blksize;
   4058 
   4059 	return error;
   4060 }
   4061 
   4062 int
   4063 audio_set_defaults(struct audio_softc *sc, u_int mode,
   4064 		   struct virtual_channel *vc)
   4065 {
   4066 	struct audio_info ai;
   4067 
   4068 	KASSERT(mutex_owned(sc->sc_lock));
   4069 
   4070 	/* default parameters */
   4071 	vc->sc_rparams = audio_default;
   4072 	vc->sc_pparams = audio_default;
   4073 	vc->sc_blkset = false;
   4074 
   4075 	AUDIO_INITINFO(&ai);
   4076 	ai.record.sample_rate = vc->sc_rparams.sample_rate;
   4077 	ai.record.encoding    = vc->sc_rparams.encoding;
   4078 	ai.record.channels    = vc->sc_rparams.channels;
   4079 	ai.record.precision   = vc->sc_rparams.precision;
   4080 	ai.record.pause	      = false;
   4081 	ai.play.sample_rate   = vc->sc_pparams.sample_rate;
   4082 	ai.play.encoding      = vc->sc_pparams.encoding;
   4083 	ai.play.channels      = vc->sc_pparams.channels;
   4084 	ai.play.precision     = vc->sc_pparams.precision;
   4085 	ai.play.pause         = false;
   4086 	ai.mode		      = mode;
   4087 
   4088 	return audiosetinfo(sc, &ai, true, vc);
   4089 }
   4090 
   4091 int
   4092 au_set_lr_value(struct	audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
   4093 {
   4094 
   4095 	KASSERT(mutex_owned(sc->sc_lock));
   4096 
   4097 	ct->type = AUDIO_MIXER_VALUE;
   4098 	ct->un.value.num_channels = 2;
   4099 	ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   4100 	ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   4101 	if (audio_set_port(sc, ct) == 0)
   4102 		return 0;
   4103 	ct->un.value.num_channels = 1;
   4104 	ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
   4105 	return audio_set_port(sc, ct);
   4106 }
   4107 
   4108 int
   4109 au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   4110 	    int gain, int balance)
   4111 {
   4112 	mixer_ctrl_t ct;
   4113 	int i, error;
   4114 	int l, r;
   4115 	u_int mask;
   4116 	int nset;
   4117 
   4118 	KASSERT(mutex_owned(sc->sc_lock));
   4119 
   4120 	if (balance == AUDIO_MID_BALANCE) {
   4121 		l = r = gain;
   4122 	} else if (balance < AUDIO_MID_BALANCE) {
   4123 		l = gain;
   4124 		r = (balance * gain) / AUDIO_MID_BALANCE;
   4125 	} else {
   4126 		r = gain;
   4127 		l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
   4128 		    / AUDIO_MID_BALANCE;
   4129 	}
   4130 	DPRINTF(("au_set_gain: gain=%d balance=%d, l=%d r=%d\n",
   4131 		 gain, balance, l, r));
   4132 
   4133 	if (ports->index == -1) {
   4134 	usemaster:
   4135 		if (ports->master == -1)
   4136 			return 0; /* just ignore it silently */
   4137 		ct.dev = ports->master;
   4138 		error = au_set_lr_value(sc, &ct, l, r);
   4139 	} else {
   4140 		ct.dev = ports->index;
   4141 		if (ports->isenum) {
   4142 			ct.type = AUDIO_MIXER_ENUM;
   4143 			error = audio_get_port(sc, &ct);
   4144 			if (error)
   4145 				return error;
   4146 			if (ports->isdual) {
   4147 				if (ports->cur_port == -1)
   4148 					ct.dev = ports->master;
   4149 				else
   4150 					ct.dev = ports->miport[ports->cur_port];
   4151 				error = au_set_lr_value(sc, &ct, l, r);
   4152 			} else {
   4153 				for(i = 0; i < ports->nports; i++)
   4154 				    if (ports->misel[i] == ct.un.ord) {
   4155 					    ct.dev = ports->miport[i];
   4156 					    if (ct.dev == -1 ||
   4157 						au_set_lr_value(sc, &ct, l, r))
   4158 						    goto usemaster;
   4159 					    else
   4160 						    break;
   4161 				    }
   4162 			}
   4163 		} else {
   4164 			ct.type = AUDIO_MIXER_SET;
   4165 			error = audio_get_port(sc, &ct);
   4166 			if (error)
   4167 				return error;
   4168 			mask = ct.un.mask;
   4169 			nset = 0;
   4170 			for(i = 0; i < ports->nports; i++) {
   4171 				if (ports->misel[i] & mask) {
   4172 				    ct.dev = ports->miport[i];
   4173 				    if (ct.dev != -1 &&
   4174 					au_set_lr_value(sc, &ct, l, r) == 0)
   4175 					    nset++;
   4176 				}
   4177 			}
   4178 			if (nset == 0)
   4179 				goto usemaster;
   4180 		}
   4181 	}
   4182 	if (!error)
   4183 		mixer_signal(sc);
   4184 	return error;
   4185 }
   4186 
   4187 int
   4188 au_get_lr_value(struct	audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
   4189 {
   4190 	int error;
   4191 
   4192 	KASSERT(mutex_owned(sc->sc_lock));
   4193 
   4194 	ct->un.value.num_channels = 2;
   4195 	if (audio_get_port(sc, ct) == 0) {
   4196 		*l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   4197 		*r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   4198 	} else {
   4199 		ct->un.value.num_channels = 1;
   4200 		error = audio_get_port(sc, ct);
   4201 		if (error)
   4202 			return error;
   4203 		*r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   4204 	}
   4205 	return 0;
   4206 }
   4207 
   4208 void
   4209 au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   4210 	    u_int *pgain, u_char *pbalance)
   4211 {
   4212 	mixer_ctrl_t ct;
   4213 	int i, l, r, n;
   4214 	int lgain, rgain;
   4215 
   4216 	KASSERT(mutex_owned(sc->sc_lock));
   4217 
   4218 	lgain = AUDIO_MAX_GAIN / 2;
   4219 	rgain = AUDIO_MAX_GAIN / 2;
   4220 	if (ports->index == -1) {
   4221 	usemaster:
   4222 		if (ports->master == -1)
   4223 			goto bad;
   4224 		ct.dev = ports->master;
   4225 		ct.type = AUDIO_MIXER_VALUE;
   4226 		if (au_get_lr_value(sc, &ct, &lgain, &rgain))
   4227 			goto bad;
   4228 	} else {
   4229 		ct.dev = ports->index;
   4230 		if (ports->isenum) {
   4231 			ct.type = AUDIO_MIXER_ENUM;
   4232 			if (audio_get_port(sc, &ct))
   4233 				goto bad;
   4234 			ct.type = AUDIO_MIXER_VALUE;
   4235 			if (ports->isdual) {
   4236 				if (ports->cur_port == -1)
   4237 					ct.dev = ports->master;
   4238 				else
   4239 					ct.dev = ports->miport[ports->cur_port];
   4240 				au_get_lr_value(sc, &ct, &lgain, &rgain);
   4241 			} else {
   4242 				for(i = 0; i < ports->nports; i++)
   4243 				    if (ports->misel[i] == ct.un.ord) {
   4244 					    ct.dev = ports->miport[i];
   4245 					    if (ct.dev == -1 ||
   4246 						au_get_lr_value(sc, &ct,
   4247 								&lgain, &rgain))
   4248 						    goto usemaster;
   4249 					    else
   4250 						    break;
   4251 				    }
   4252 			}
   4253 		} else {
   4254 			ct.type = AUDIO_MIXER_SET;
   4255 			if (audio_get_port(sc, &ct))
   4256 				goto bad;
   4257 			ct.type = AUDIO_MIXER_VALUE;
   4258 			lgain = rgain = n = 0;
   4259 			for(i = 0; i < ports->nports; i++) {
   4260 				if (ports->misel[i] & ct.un.mask) {
   4261 					ct.dev = ports->miport[i];
   4262 					if (ct.dev == -1 ||
   4263 					    au_get_lr_value(sc, &ct, &l, &r))
   4264 						goto usemaster;
   4265 					else {
   4266 						lgain += l;
   4267 						rgain += r;
   4268 						n++;
   4269 					}
   4270 				}
   4271 			}
   4272 			if (n != 0) {
   4273 				lgain /= n;
   4274 				rgain /= n;
   4275 			}
   4276 		}
   4277 	}
   4278 bad:
   4279 	if (lgain == rgain) {	/* handles lgain==rgain==0 */
   4280 		*pgain = lgain;
   4281 		*pbalance = AUDIO_MID_BALANCE;
   4282 	} else if (lgain < rgain) {
   4283 		*pgain = rgain;
   4284 		/* balance should be > AUDIO_MID_BALANCE */
   4285 		*pbalance = AUDIO_RIGHT_BALANCE -
   4286 			(AUDIO_MID_BALANCE * lgain) / rgain;
   4287 	} else /* lgain > rgain */ {
   4288 		*pgain = lgain;
   4289 		/* balance should be < AUDIO_MID_BALANCE */
   4290 		*pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
   4291 	}
   4292 }
   4293 
   4294 int
   4295 au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
   4296 {
   4297 	mixer_ctrl_t ct;
   4298 	int i, error, use_mixerout;
   4299 
   4300 	KASSERT(mutex_owned(sc->sc_lock));
   4301 
   4302 	use_mixerout = 1;
   4303 	if (port == 0) {
   4304 		if (ports->allports == 0)
   4305 			return 0;		/* Allow this special case. */
   4306 		else if (ports->isdual) {
   4307 			if (ports->cur_port == -1) {
   4308 				return 0;
   4309 			} else {
   4310 				port = ports->aumask[ports->cur_port];
   4311 				ports->cur_port = -1;
   4312 				use_mixerout = 0;
   4313 			}
   4314 		}
   4315 	}
   4316 	if (ports->index == -1)
   4317 		return EINVAL;
   4318 	ct.dev = ports->index;
   4319 	if (ports->isenum) {
   4320 		if (port & (port-1))
   4321 			return EINVAL; /* Only one port allowed */
   4322 		ct.type = AUDIO_MIXER_ENUM;
   4323 		error = EINVAL;
   4324 		for(i = 0; i < ports->nports; i++)
   4325 			if (ports->aumask[i] == port) {
   4326 				if (ports->isdual && use_mixerout) {
   4327 					ct.un.ord = ports->mixerout;
   4328 					ports->cur_port = i;
   4329 				} else {
   4330 					ct.un.ord = ports->misel[i];
   4331 				}
   4332 				error = audio_set_port(sc, &ct);
   4333 				break;
   4334 			}
   4335 	} else {
   4336 		ct.type = AUDIO_MIXER_SET;
   4337 		ct.un.mask = 0;
   4338 		for(i = 0; i < ports->nports; i++)
   4339 			if (ports->aumask[i] & port)
   4340 				ct.un.mask |= ports->misel[i];
   4341 		if (port != 0 && ct.un.mask == 0)
   4342 			error = EINVAL;
   4343 		else
   4344 			error = audio_set_port(sc, &ct);
   4345 	}
   4346 	if (!error)
   4347 		mixer_signal(sc);
   4348 	return error;
   4349 }
   4350 
   4351 int
   4352 au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
   4353 {
   4354 	mixer_ctrl_t ct;
   4355 	int i, aumask;
   4356 
   4357 	KASSERT(mutex_owned(sc->sc_lock));
   4358 
   4359 	if (ports->index == -1)
   4360 		return 0;
   4361 	ct.dev = ports->index;
   4362 	ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
   4363 	if (audio_get_port(sc, &ct))
   4364 		return 0;
   4365 	aumask = 0;
   4366 	if (ports->isenum) {
   4367 		if (ports->isdual && ports->cur_port != -1) {
   4368 			if (ports->mixerout == ct.un.ord)
   4369 				aumask = ports->aumask[ports->cur_port];
   4370 			else
   4371 				ports->cur_port = -1;
   4372 		}
   4373 		if (aumask == 0)
   4374 			for(i = 0; i < ports->nports; i++)
   4375 				if (ports->misel[i] == ct.un.ord)
   4376 					aumask = ports->aumask[i];
   4377 	} else {
   4378 		for(i = 0; i < ports->nports; i++)
   4379 			if (ct.un.mask & ports->misel[i])
   4380 				aumask |= ports->aumask[i];
   4381 	}
   4382 	return aumask;
   4383 }
   4384 
   4385 int
   4386 audiosetinfo(struct audio_softc *sc, struct audio_info *ai, bool reset,
   4387 	     struct virtual_channel *vc)
   4388 {
   4389 	stream_filter_list_t pfilters, rfilters;
   4390 	audio_params_t pp, rp;
   4391 	struct audio_prinfo *r, *p;
   4392 	const struct audio_hw_if *hw;
   4393 	audio_stream_t *oldpus, *oldrus;
   4394 	int setmode;
   4395 	int error;
   4396 	int np, nr;
   4397 	unsigned int blks;
   4398 	int oldpblksize, oldrblksize;
   4399 	u_int gain;
   4400 	bool rbus, pbus;
   4401 	bool cleared, modechange, pausechange;
   4402 	u_char balance;
   4403 
   4404 	KASSERT(mutex_owned(sc->sc_lock));
   4405 
   4406 	hw = sc->hw_if;
   4407 	if (hw == NULL)		/* HW has not attached */
   4408 		return ENXIO;
   4409 
   4410 	DPRINTF(("%s sc=%p ai=%p\n", __func__, sc, ai));
   4411 	r = &ai->record;
   4412 	p = &ai->play;
   4413 	rbus = vc->sc_rbus;
   4414 	pbus = vc->sc_pbus;
   4415 	error = 0;
   4416 	cleared = false;
   4417 	modechange = false;
   4418 	pausechange = false;
   4419 
   4420 	pp = vc->sc_pparams;	/* Temporary encoding storage in */
   4421 	rp = vc->sc_rparams;	/* case setting the modes fails. */
   4422 	nr = np = 0;
   4423 
   4424 	if (SPECIFIED(p->sample_rate)) {
   4425 		pp.sample_rate = p->sample_rate;
   4426 		np++;
   4427 	}
   4428 	if (SPECIFIED(r->sample_rate)) {
   4429 		rp.sample_rate = r->sample_rate;
   4430 		nr++;
   4431 	}
   4432 	if (SPECIFIED(p->encoding)) {
   4433 		pp.encoding = p->encoding;
   4434 		np++;
   4435 	}
   4436 	if (SPECIFIED(r->encoding)) {
   4437 		rp.encoding = r->encoding;
   4438 		nr++;
   4439 	}
   4440 	if (SPECIFIED(p->precision)) {
   4441 		pp.precision = p->precision;
   4442 		/* we don't have API to specify validbits */
   4443 		pp.validbits = p->precision;
   4444 		np++;
   4445 	}
   4446 	if (SPECIFIED(r->precision)) {
   4447 		rp.precision = r->precision;
   4448 		/* we don't have API to specify validbits */
   4449 		rp.validbits = r->precision;
   4450 		nr++;
   4451 	}
   4452 	if (SPECIFIED(p->channels)) {
   4453 		pp.channels = p->channels;
   4454 		np++;
   4455 	}
   4456 	if (SPECIFIED(r->channels)) {
   4457 		rp.channels = r->channels;
   4458 		nr++;
   4459 	}
   4460 
   4461 	if (!audio_can_capture(sc))
   4462 		nr = 0;
   4463 	if (!audio_can_playback(sc))
   4464 		np = 0;
   4465 
   4466 #ifdef AUDIO_DEBUG
   4467 	if (audiodebug && nr > 0)
   4468 	    audio_print_params("audiosetinfo() Setting record params:", &rp);
   4469 	if (audiodebug && np > 0)
   4470 	    audio_print_params("audiosetinfo() Setting play params:", &pp);
   4471 #endif
   4472 	if (nr > 0 && (error = audio_check_params(&rp)))
   4473 		return error;
   4474 	if (np > 0 && (error = audio_check_params(&pp)))
   4475 		return error;
   4476 
   4477 	oldpblksize = vc->sc_mpr.blksize;
   4478 	oldrblksize = vc->sc_mrr.blksize;
   4479 
   4480 	setmode = 0;
   4481 	if (nr > 0) {
   4482 		if (!cleared) {
   4483 			audio_clear_intr_unlocked(sc, vc);
   4484 			cleared = true;
   4485 		}
   4486 		modechange = true;
   4487 		setmode |= AUMODE_RECORD;
   4488 	}
   4489 	if (np > 0) {
   4490 		if (!cleared) {
   4491 			audio_clear_intr_unlocked(sc, vc);
   4492 			cleared = true;
   4493 		}
   4494 		modechange = true;
   4495 		setmode |= AUMODE_PLAY;
   4496 	}
   4497 
   4498 	if (SPECIFIED(ai->mode)) {
   4499 		if (!cleared) {
   4500 			audio_clear_intr_unlocked(sc, vc);
   4501 			cleared = true;
   4502 		}
   4503 		modechange = true;
   4504 		vc->sc_mode = ai->mode;
   4505 		if (vc->sc_mode & AUMODE_PLAY_ALL)
   4506 			vc->sc_mode |= AUMODE_PLAY;
   4507 		if ((vc->sc_mode & AUMODE_PLAY) && !vc->sc_full_duplex)
   4508 			/* Play takes precedence */
   4509 			vc->sc_mode &= ~AUMODE_RECORD;
   4510 	}
   4511 
   4512 	oldpus = vc->sc_pustream;
   4513 	oldrus = vc->sc_rustream;
   4514 	if (modechange || reset) {
   4515 		int indep;
   4516 
   4517 		indep = audio_get_props(sc) & AUDIO_PROP_INDEPENDENT;
   4518 		if (!indep) {
   4519 			if (setmode == AUMODE_RECORD)
   4520 				pp = rp;
   4521 			else if (setmode == AUMODE_PLAY)
   4522 				rp = pp;
   4523 		}
   4524 		memset(&pfilters, 0, sizeof(pfilters));
   4525 		memset(&rfilters, 0, sizeof(rfilters));
   4526 		pfilters.append = stream_filter_list_append;
   4527 		pfilters.prepend = stream_filter_list_prepend;
   4528 		pfilters.set = stream_filter_list_set;
   4529 		rfilters.append = stream_filter_list_append;
   4530 		rfilters.prepend = stream_filter_list_prepend;
   4531 		rfilters.set = stream_filter_list_set;
   4532 		/* Some device drivers change channels/sample_rate and change
   4533 		 * no channels/sample_rate. */
   4534 		error = audio_set_params(sc, setmode,
   4535 		    vc->sc_mode & (AUMODE_PLAY | AUMODE_RECORD), &pp, &rp,
   4536 		    &pfilters, &rfilters, vc);
   4537 		if (error) {
   4538 			DPRINTF(("%s: audio_set_params() failed with %d\n",
   4539 			    __func__, error));
   4540 			goto cleanup;
   4541 		}
   4542 
   4543 		audio_check_params(&pp);
   4544 		audio_check_params(&rp);
   4545 		if (!indep) {
   4546 			/* XXX for !indep device, we have to use the same
   4547 			 * parameters for the hardware, not userland */
   4548 			if (setmode == AUMODE_RECORD) {
   4549 				pp = rp;
   4550 			} else if (setmode == AUMODE_PLAY) {
   4551 				rp = pp;
   4552 			}
   4553 		}
   4554 
   4555 		if (vc->sc_mpr.mmapped && pfilters.req_size > 0) {
   4556 			DPRINTF(("%s: mmapped, and filters are requested.\n",
   4557 				 __func__));
   4558 			error = EINVAL;
   4559 			goto cleanup;
   4560 		}
   4561 
   4562 		/* construct new filter chain */
   4563 		if (setmode & AUMODE_PLAY) {
   4564 			error = audio_setup_pfilters(sc, &pp, &pfilters, vc);
   4565 			if (error)
   4566 				goto cleanup;
   4567 		}
   4568 		if (setmode & AUMODE_RECORD) {
   4569 			error = audio_setup_rfilters(sc, &rp, &rfilters, vc);
   4570 			if (error)
   4571 				goto cleanup;
   4572 		}
   4573 		DPRINTF(("%s: filter setup is completed.\n", __func__));
   4574 
   4575 		/* userland formats */
   4576 		vc->sc_pparams = pp;
   4577 		vc->sc_rparams = rp;
   4578 	}
   4579 
   4580 	/* Play params can affect the record params, so recalculate blksize. */
   4581 	if (nr > 0 || np > 0 || reset) {
   4582 		vc->sc_blkset = false;
   4583 		audio_calc_blksize(sc, AUMODE_RECORD, vc);
   4584 		audio_calc_blksize(sc, AUMODE_PLAY, vc);
   4585 	}
   4586 #ifdef AUDIO_DEBUG
   4587 	if (audiodebug > 1 && nr > 0) {
   4588 	    audio_print_params("audiosetinfo() After setting record params:",
   4589 		&vc->sc_rparams);
   4590 	}
   4591 	if (audiodebug > 1 && np > 0) {
   4592 	    audio_print_params("audiosetinfo() After setting play params:",
   4593 		&vc->sc_pparams);
   4594 	}
   4595 #endif
   4596 
   4597 	if (SPECIFIED(p->port)) {
   4598 		if (!cleared) {
   4599 			audio_clear_intr_unlocked(sc, vc);
   4600 			cleared = true;
   4601 		}
   4602 		error = au_set_port(sc, &sc->sc_outports, p->port);
   4603 		if (error)
   4604 			goto cleanup;
   4605 	}
   4606 	if (SPECIFIED(r->port)) {
   4607 		if (!cleared) {
   4608 			audio_clear_intr_unlocked(sc, vc);
   4609 			cleared = true;
   4610 		}
   4611 		error = au_set_port(sc, &sc->sc_inports, r->port);
   4612 		if (error)
   4613 			goto cleanup;
   4614 	}
   4615 	if (SPECIFIED(p->gain)) {
   4616 		au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   4617 		error = au_set_gain(sc, &sc->sc_outports, p->gain, balance);
   4618 		if (error)
   4619 			goto cleanup;
   4620 	}
   4621 	if (SPECIFIED(r->gain)) {
   4622 		au_get_gain(sc, &sc->sc_inports, &gain, &balance);
   4623 		error = au_set_gain(sc, &sc->sc_inports, r->gain, balance);
   4624 		if (error)
   4625 			goto cleanup;
   4626 	}
   4627 
   4628 	if (SPECIFIED_CH(p->balance)) {
   4629 		au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   4630 		error = au_set_gain(sc, &sc->sc_outports, gain, p->balance);
   4631 		if (error)
   4632 			goto cleanup;
   4633 	}
   4634 	if (SPECIFIED_CH(r->balance)) {
   4635 		au_get_gain(sc, &sc->sc_inports, &gain, &balance);
   4636 		error = au_set_gain(sc, &sc->sc_inports, gain, r->balance);
   4637 		if (error)
   4638 			goto cleanup;
   4639 	}
   4640 
   4641 	if (SPECIFIED(ai->monitor_gain) && sc->sc_monitor_port != -1) {
   4642 		mixer_ctrl_t ct;
   4643 
   4644 		ct.dev = sc->sc_monitor_port;
   4645 		ct.type = AUDIO_MIXER_VALUE;
   4646 		ct.un.value.num_channels = 1;
   4647 		ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = ai->monitor_gain;
   4648 		error = audio_set_port(sc, &ct);
   4649 		if (error)
   4650 			goto cleanup;
   4651 	}
   4652 
   4653 	if (SPECIFIED_CH(p->pause)) {
   4654 		vc->sc_mpr.pause = p->pause;
   4655 		pbus = !p->pause;
   4656 		pausechange = true;
   4657 	}
   4658 	if (SPECIFIED_CH(r->pause)) {
   4659 		vc->sc_mrr.pause = r->pause;
   4660 		rbus = !r->pause;
   4661 		pausechange = true;
   4662 	}
   4663 
   4664 	if (SPECIFIED(ai->blocksize)) {
   4665 		int pblksize, rblksize;
   4666 
   4667 		/* Block size specified explicitly. */
   4668 		if (ai->blocksize == 0) {
   4669 			if (!cleared) {
   4670 				audio_clear_intr_unlocked(sc, vc);
   4671 				cleared = true;
   4672 			}
   4673 			vc->sc_blkset = false;
   4674 			audio_calc_blksize(sc, AUMODE_RECORD, vc);
   4675 			audio_calc_blksize(sc, AUMODE_PLAY, vc);
   4676 		} else {
   4677 			vc->sc_blkset = true;
   4678 			/* check whether new blocksize changes actually */
   4679 			if (hw->round_blocksize == NULL) {
   4680 				if (!cleared) {
   4681 					audio_clear_intr_unlocked(sc, vc);
   4682 					cleared = true;
   4683 				}
   4684 				vc->sc_mpr.blksize = ai->blocksize;
   4685 				vc->sc_mrr.blksize = ai->blocksize;
   4686 			} else {
   4687 				pblksize = hw->round_blocksize(sc->hw_hdl,
   4688 				    ai->blocksize, AUMODE_PLAY,
   4689 				    &vc->sc_mpr.s.param);
   4690 				rblksize = hw->round_blocksize(sc->hw_hdl,
   4691 				    ai->blocksize, AUMODE_RECORD,
   4692 				    &vc->sc_mrr.s.param);
   4693 				if ((pblksize != vc->sc_mpr.blksize &&
   4694 				    pblksize > sc->sc_pr.blksize)
   4695 				    || (rblksize != vc->sc_mrr.blksize &&
   4696 				    rblksize > sc->sc_rr.blksize)) {
   4697 					if (!cleared) {
   4698 					    audio_clear_intr_unlocked(sc, vc);
   4699 					    cleared = true;
   4700 					}
   4701 					vc->sc_mpr.blksize = pblksize;
   4702 					vc->sc_mrr.blksize = rblksize;
   4703 				}
   4704 			}
   4705 		}
   4706 	}
   4707 
   4708 	if (SPECIFIED(ai->mode)) {
   4709 		if (vc->sc_mode & AUMODE_PLAY)
   4710 			audio_init_play(sc, vc);
   4711 		if (vc->sc_mode & AUMODE_RECORD)
   4712 			audio_init_record(sc, vc);
   4713 	}
   4714 
   4715 	if (hw->commit_settings && sc->sc_opens == 0) {
   4716 		error = hw->commit_settings(sc->hw_hdl);
   4717 		if (error)
   4718 			goto cleanup;
   4719 	}
   4720 
   4721 	vc->sc_lastinfo = *ai;
   4722 	vc->sc_lastinfovalid = true;
   4723 
   4724 cleanup:
   4725 	if (error == 0 && (cleared || pausechange|| reset)) {
   4726 		int init_error;
   4727 
   4728 		init_error = (pausechange == 1 && reset == 0) ? 0 :
   4729 		    audio_initbufs(sc, vc);
   4730 		if (init_error) goto err;
   4731 		if (vc->sc_mpr.blksize != oldpblksize ||
   4732 		    vc->sc_mrr.blksize != oldrblksize ||
   4733 		    vc->sc_pustream != oldpus ||
   4734 		    vc->sc_rustream != oldrus)
   4735 			audio_calcwater(sc, vc);
   4736 		if ((vc->sc_mode & AUMODE_PLAY) &&
   4737 		    pbus && !vc->sc_pbus)
   4738 			init_error = audiostartp(sc, vc);
   4739 		if (!init_error &&
   4740 		    (vc->sc_mode & AUMODE_RECORD) &&
   4741 		    rbus && !vc->sc_rbus)
   4742 			init_error = audiostartr(sc, vc);
   4743 	err:
   4744 		if (init_error)
   4745 			return init_error;
   4746 	}
   4747 
   4748 	/* Change water marks after initializing the buffers. */
   4749 	if (SPECIFIED(ai->hiwat)) {
   4750 		blks = ai->hiwat;
   4751 		if (blks > vc->sc_mpr.maxblks)
   4752 			blks = vc->sc_mpr.maxblks;
   4753 		if (blks < 2)
   4754 			blks = 2;
   4755 		vc->sc_mpr.usedhigh = blks * vc->sc_mpr.blksize;
   4756 	}
   4757 	if (SPECIFIED(ai->lowat)) {
   4758 		blks = ai->lowat;
   4759 		if (blks > vc->sc_mpr.maxblks - 1)
   4760 			blks = vc->sc_mpr.maxblks - 1;
   4761 		vc->sc_mpr.usedlow = blks * vc->sc_mpr.blksize;
   4762 	}
   4763 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   4764 		if (vc->sc_mpr.usedlow > vc->sc_mpr.usedhigh -
   4765 		    vc->sc_mpr.blksize) {
   4766 			vc->sc_mpr.usedlow =
   4767 				vc->sc_mpr.usedhigh - vc->sc_mpr.blksize;
   4768 		}
   4769 	}
   4770 
   4771 	return error;
   4772 }
   4773 
   4774 int
   4775 audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int buf_only_mode,
   4776 	     struct virtual_channel *vc)
   4777 {
   4778 	struct audio_prinfo *r, *p;
   4779 	const struct audio_hw_if *hw;
   4780 
   4781 	KASSERT(mutex_owned(sc->sc_lock));
   4782 
   4783 	r = &ai->record;
   4784 	p = &ai->play;
   4785 	hw = sc->hw_if;
   4786 	if (hw == NULL)		/* HW has not attached */
   4787 		return ENXIO;
   4788 
   4789 	p->sample_rate = vc->sc_pparams.sample_rate;
   4790 	r->sample_rate = vc->sc_rparams.sample_rate;
   4791 	p->channels = vc->sc_pparams.channels;
   4792 	r->channels = vc->sc_rparams.channels;
   4793 	p->precision = vc->sc_pparams.precision;
   4794 	r->precision = vc->sc_rparams.precision;
   4795 	p->encoding = vc->sc_pparams.encoding;
   4796 	r->encoding = vc->sc_rparams.encoding;
   4797 
   4798 	if (buf_only_mode) {
   4799 		r->port = 0;
   4800 		p->port = 0;
   4801 
   4802 		r->avail_ports = 0;
   4803 		p->avail_ports = 0;
   4804 
   4805 		r->gain = 0;
   4806 		r->balance = 0;
   4807 
   4808 		p->gain = 0;
   4809 		p->balance = 0;
   4810 	} else {
   4811 		r->port = au_get_port(sc, &sc->sc_inports);
   4812 		p->port = au_get_port(sc, &sc->sc_outports);
   4813 
   4814 		r->avail_ports = sc->sc_inports.allports;
   4815 		p->avail_ports = sc->sc_outports.allports;
   4816 
   4817 		au_get_gain(sc, &sc->sc_inports, &r->gain, &r->balance);
   4818 		au_get_gain(sc, &sc->sc_outports, &p->gain, &p->balance);
   4819 	}
   4820 
   4821 	if (sc->sc_monitor_port != -1 && buf_only_mode == 0) {
   4822 		mixer_ctrl_t ct;
   4823 
   4824 		ct.dev = sc->sc_monitor_port;
   4825 		ct.type = AUDIO_MIXER_VALUE;
   4826 		ct.un.value.num_channels = 1;
   4827 		if (audio_get_port(sc, &ct))
   4828 			ai->monitor_gain = 0;
   4829 		else
   4830 			ai->monitor_gain =
   4831 				ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
   4832 	} else
   4833 		ai->monitor_gain = 0;
   4834 
   4835 	p->seek = audio_stream_get_used(vc->sc_pustream);
   4836 	r->seek = audio_stream_get_used(vc->sc_rustream);
   4837 
   4838 	/*
   4839 	 * XXX samples should be a value for userland data.
   4840 	 * But drops is a value for HW data.
   4841 	 */
   4842 	p->samples = (vc->sc_pustream == &vc->sc_mpr.s
   4843 	    ? vc->sc_mpr.stamp : vc->sc_mpr.fstamp) - vc->sc_mpr.drops;
   4844 	r->samples = (vc->sc_rustream == &vc->sc_mrr.s
   4845 	    ? vc->sc_mrr.stamp : vc->sc_mrr.fstamp) - vc->sc_mrr.drops;
   4846 
   4847 	p->eof = sc->sc_eof;
   4848 	r->eof = 0;
   4849 
   4850 	p->pause = vc->sc_mpr.pause;
   4851 	r->pause = vc->sc_mrr.pause;
   4852 
   4853 	p->error = vc->sc_mpr.drops != 0;
   4854 	r->error = vc->sc_mrr.drops != 0;
   4855 
   4856 	p->waiting = r->waiting = 0;		/* open never hangs */
   4857 
   4858 	p->open = (vc->sc_open & AUOPEN_WRITE) != 0;
   4859 	r->open = (vc->sc_open & AUOPEN_READ) != 0;
   4860 
   4861 	p->active = vc->sc_pbus;
   4862 	r->active = vc->sc_rbus;
   4863 
   4864 	p->buffer_size = vc->sc_pustream ? vc->sc_pustream->bufsize : 0;
   4865 	r->buffer_size = vc->sc_rustream ? vc->sc_rustream->bufsize : 0;
   4866 
   4867 	ai->blocksize = vc->sc_mpr.blksize;
   4868 	if (vc->sc_mpr.blksize > 0) {
   4869 		ai->hiwat = vc->sc_mpr.usedhigh / vc->sc_mpr.blksize;
   4870 		ai->lowat = vc->sc_mpr.usedlow / vc->sc_mpr.blksize;
   4871 	} else
   4872 		ai->hiwat = ai->lowat = 0;
   4873 	ai->mode = vc->sc_mode;
   4874 
   4875 	return 0;
   4876 }
   4877 
   4878 /*
   4879  * Mixer driver
   4880  */
   4881 int
   4882 mixer_open(dev_t dev, struct audio_softc *sc, int flags,
   4883     int ifmt, struct lwp *l, struct file **nfp)
   4884 {
   4885 	struct file *fp;
   4886 	struct audio_chan *chan;
   4887 	int error, fd;
   4888 
   4889 	KASSERT(mutex_owned(sc->sc_lock));
   4890 
   4891 	if (sc->hw_if == NULL)
   4892 		return  ENXIO;
   4893 
   4894 	DPRINTF(("mixer_open: flags=0x%x sc=%p\n", flags, sc));
   4895 
   4896 	chan = kmem_zalloc(sizeof(struct audio_chan), KM_SLEEP);
   4897 
   4898 	error = fd_allocfile(&fp, &fd);
   4899 	if (error)
   4900 		return error;
   4901 
   4902 	chan->dev = dev;
   4903 	chan->chan = MIXER_INUSE;
   4904 
   4905 	SIMPLEQ_INSERT_TAIL(&sc->sc_audiochan, chan, entries);
   4906 
   4907 	error = fd_clone(fp, fd, flags, &audio_fileops, chan);
   4908 	KASSERT(error == EMOVEFD);
   4909 
   4910 	*nfp = fp;
   4911 	return error;
   4912 }
   4913 
   4914 /*
   4915  * Remove a process from those to be signalled on mixer activity.
   4916  */
   4917 static void
   4918 mixer_remove(struct audio_softc *sc)
   4919 {
   4920 	struct mixer_asyncs **pm, *m;
   4921 	pid_t pid;
   4922 
   4923 	KASSERT(mutex_owned(sc->sc_lock));
   4924 
   4925 	pid = curproc->p_pid;
   4926 	for (pm = &sc->sc_async_mixer; *pm; pm = &(*pm)->next) {
   4927 		if ((*pm)->pid == pid) {
   4928 			m = *pm;
   4929 			*pm = m->next;
   4930 			kmem_free(m, sizeof(*m));
   4931 			return;
   4932 		}
   4933 	}
   4934 }
   4935 
   4936 /*
   4937  * Signal all processes waiting for the mixer.
   4938  */
   4939 static void
   4940 mixer_signal(struct audio_softc *sc)
   4941 {
   4942 	struct mixer_asyncs *m;
   4943 	proc_t *p;
   4944 
   4945 	for (m = sc->sc_async_mixer; m; m = m->next) {
   4946 		mutex_enter(proc_lock);
   4947 		if ((p = proc_find(m->pid)) != NULL)
   4948 			psignal(p, SIGIO);
   4949 		mutex_exit(proc_lock);
   4950 	}
   4951 }
   4952 
   4953 /*
   4954  * Close a mixer device
   4955  */
   4956 /* ARGSUSED */
   4957 int
   4958 mixer_close(struct audio_softc *sc, int flags, struct audio_chan *chan)
   4959 {
   4960 
   4961 	KASSERT(mutex_owned(sc->sc_lock));
   4962 	if (sc->hw_if == NULL)
   4963 		return ENXIO;
   4964 
   4965 	DPRINTF(("mixer_close: sc %p\n", sc));
   4966 	mixer_remove(sc);
   4967 	SIMPLEQ_REMOVE(&sc->sc_audiochan, chan, audio_chan, entries);
   4968 
   4969 	return 0;
   4970 }
   4971 
   4972 int
   4973 mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
   4974 	    struct lwp *l)
   4975 {
   4976 	const struct audio_hw_if *hw;
   4977 	struct mixer_asyncs *ma;
   4978 	mixer_ctrl_t *mc;
   4979 	int error;
   4980 
   4981 	DPRINTF(("mixer_ioctl(%lu,'%c',%lu)\n",
   4982 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff));
   4983 	hw = sc->hw_if;
   4984 	if (hw == NULL)
   4985 		return ENXIO;
   4986 	error = EINVAL;
   4987 
   4988 	/* we can return cached values if we are sleeping */
   4989 	if (cmd != AUDIO_MIXER_READ)
   4990 		device_active(sc->dev, DVA_SYSTEM);
   4991 
   4992 	switch (cmd) {
   4993 	case FIOASYNC:
   4994 		if (*(int *)addr) {
   4995 			ma = kmem_alloc(sizeof(struct mixer_asyncs), KM_SLEEP);
   4996 		} else {
   4997 			ma = NULL;
   4998 		}
   4999 		mixer_remove(sc);	/* remove old entry */
   5000 		if (ma != NULL) {
   5001 			ma->next = sc->sc_async_mixer;
   5002 			ma->pid = curproc->p_pid;
   5003 			sc->sc_async_mixer = ma;
   5004 		}
   5005 		error = 0;
   5006 		break;
   5007 
   5008 	case AUDIO_GETDEV:
   5009 		DPRINTF(("AUDIO_GETDEV\n"));
   5010 		error = hw->getdev(sc->hw_hdl, (audio_device_t *)addr);
   5011 		break;
   5012 
   5013 	case AUDIO_MIXER_DEVINFO:
   5014 		DPRINTF(("AUDIO_MIXER_DEVINFO\n"));
   5015 		((mixer_devinfo_t *)addr)->un.v.delta = 0; /* default */
   5016 		error = audio_query_devinfo(sc, (mixer_devinfo_t *)addr);
   5017 		break;
   5018 
   5019 	case AUDIO_MIXER_READ:
   5020 		DPRINTF(("AUDIO_MIXER_READ\n"));
   5021 		mc = (mixer_ctrl_t *)addr;
   5022 
   5023 		if (device_is_active(sc->sc_dev))
   5024 			error = audio_get_port(sc, mc);
   5025 		else if (mc->dev >= sc->sc_nmixer_states)
   5026 			error = ENXIO;
   5027 		else {
   5028 			int dev = mc->dev;
   5029 			memcpy(mc, &sc->sc_mixer_state[dev],
   5030 			    sizeof(mixer_ctrl_t));
   5031 			error = 0;
   5032 		}
   5033 		break;
   5034 
   5035 	case AUDIO_MIXER_WRITE:
   5036 		DPRINTF(("AUDIO_MIXER_WRITE\n"));
   5037 		error = audio_set_port(sc, (mixer_ctrl_t *)addr);
   5038 		if (!error && hw->commit_settings)
   5039 			error = hw->commit_settings(sc->hw_hdl);
   5040 		if (!error)
   5041 			mixer_signal(sc);
   5042 		break;
   5043 
   5044 	default:
   5045 		if (hw->dev_ioctl) {
   5046 			error = hw->dev_ioctl(sc->hw_hdl, cmd, addr, flag, l);
   5047 		} else
   5048 			error = EINVAL;
   5049 		break;
   5050 	}
   5051 	DPRINTF(("mixer_ioctl(%lu,'%c',%lu) result %d\n",
   5052 		 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, error));
   5053 	return error;
   5054 }
   5055 #endif /* NAUDIO > 0 */
   5056 
   5057 #include "midi.h"
   5058 
   5059 #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
   5060 #include <sys/param.h>
   5061 #include <sys/systm.h>
   5062 #include <sys/device.h>
   5063 #include <sys/audioio.h>
   5064 #include <dev/audio_if.h>
   5065 #endif
   5066 
   5067 #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
   5068 int
   5069 audioprint(void *aux, const char *pnp)
   5070 {
   5071 	struct audio_attach_args *arg;
   5072 	const char *type;
   5073 
   5074 	if (pnp != NULL) {
   5075 		arg = aux;
   5076 		switch (arg->type) {
   5077 		case AUDIODEV_TYPE_AUDIO:
   5078 			type = "audio";
   5079 			break;
   5080 		case AUDIODEV_TYPE_MIDI:
   5081 			type = "midi";
   5082 			break;
   5083 		case AUDIODEV_TYPE_OPL:
   5084 			type = "opl";
   5085 			break;
   5086 		case AUDIODEV_TYPE_MPU:
   5087 			type = "mpu";
   5088 			break;
   5089 		default:
   5090 			panic("audioprint: unknown type %d", arg->type);
   5091 		}
   5092 		aprint_normal("%s at %s", type, pnp);
   5093 	}
   5094 	return UNCONF;
   5095 }
   5096 
   5097 #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */
   5098 
   5099 #if NAUDIO > 0
   5100 device_t
   5101 audio_get_device(struct audio_softc *sc)
   5102 {
   5103 	return sc->sc_dev;
   5104 }
   5105 #endif
   5106 
   5107 #if NAUDIO > 0
   5108 static void
   5109 audio_mixer_capture(struct audio_softc *sc)
   5110 {
   5111 	mixer_devinfo_t mi;
   5112 	mixer_ctrl_t *mc;
   5113 
   5114 	KASSERT(mutex_owned(sc->sc_lock));
   5115 
   5116 	for (mi.index = 0;; mi.index++) {
   5117 		if (audio_query_devinfo(sc, &mi) != 0)
   5118 			break;
   5119 		KASSERT(mi.index < sc->sc_nmixer_states);
   5120 		if (mi.type == AUDIO_MIXER_CLASS)
   5121 			continue;
   5122 		mc = &sc->sc_mixer_state[mi.index];
   5123 		mc->dev = mi.index;
   5124 		mc->type = mi.type;
   5125 		mc->un.value.num_channels = mi.un.v.num_channels;
   5126 		(void)audio_get_port(sc, mc);
   5127 	}
   5128 
   5129 	return;
   5130 }
   5131 
   5132 static void
   5133 audio_mixer_restore(struct audio_softc *sc)
   5134 {
   5135 	mixer_devinfo_t mi;
   5136 	mixer_ctrl_t *mc;
   5137 
   5138 	KASSERT(mutex_owned(sc->sc_lock));
   5139 
   5140 	for (mi.index = 0; ; mi.index++) {
   5141 		if (audio_query_devinfo(sc, &mi) != 0)
   5142 			break;
   5143 		if (mi.type == AUDIO_MIXER_CLASS)
   5144 			continue;
   5145 		mc = &sc->sc_mixer_state[mi.index];
   5146 		(void)audio_set_port(sc, mc);
   5147 	}
   5148 	if (sc->hw_if->commit_settings)
   5149 		sc->hw_if->commit_settings(sc->hw_hdl);
   5150 
   5151 	return;
   5152 }
   5153 
   5154 #ifdef AUDIO_PM_IDLE
   5155 static void
   5156 audio_idle(void *arg)
   5157 {
   5158 	device_t dv = arg;
   5159 	struct audio_softc *sc = device_private(dv);
   5160 
   5161 #ifdef PNP_DEBUG
   5162 	extern int pnp_debug_idle;
   5163 	if (pnp_debug_idle)
   5164 		printf("%s: idle handler called\n", device_xname(dv));
   5165 #endif
   5166 
   5167 	sc->sc_idle = true;
   5168 
   5169 	/* XXX joerg Make pmf_device_suspend handle children? */
   5170 	if (!pmf_device_suspend(dv, PMF_Q_SELF))
   5171 		return;
   5172 
   5173 	if (!pmf_device_suspend(sc->sc_dev, PMF_Q_SELF))
   5174 		pmf_device_resume(dv, PMF_Q_SELF);
   5175 }
   5176 
   5177 static void
   5178 audio_activity(device_t dv, devactive_t type)
   5179 {
   5180 	struct audio_softc *sc = device_private(dv);
   5181 
   5182 	if (type != DVA_SYSTEM)
   5183 		return;
   5184 
   5185 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   5186 
   5187 	sc->sc_idle = false;
   5188 	if (!device_is_active(dv)) {
   5189 		/* XXX joerg How to deal with a failing resume... */
   5190 		pmf_device_resume(sc->sc_dev, PMF_Q_SELF);
   5191 		pmf_device_resume(dv, PMF_Q_SELF);
   5192 	}
   5193 }
   5194 #endif
   5195 
   5196 static bool
   5197 audio_suspend(device_t dv, const pmf_qual_t *qual)
   5198 {
   5199 	struct audio_softc *sc = device_private(dv);
   5200 	struct audio_chan *chan;
   5201 	const struct audio_hw_if *hwp = sc->hw_if;
   5202 	struct virtual_channel *vc;
   5203 	bool pbus, rbus;
   5204 
   5205 	pbus = rbus = false;
   5206 	mutex_enter(sc->sc_lock);
   5207 	audio_mixer_capture(sc);
   5208 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
   5209 		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan) ||
   5210 			chan->chan == MIXER_INUSE)
   5211 			continue;
   5212 
   5213 		vc = chan->vc;
   5214 		if (vc->sc_pbus && !pbus)
   5215 			pbus = true;
   5216 		if (vc->sc_rbus && !rbus)
   5217 			rbus = true;
   5218 	}
   5219 	mutex_enter(sc->sc_intr_lock);
   5220 	if (pbus == true)
   5221 		hwp->halt_output(sc->hw_hdl);
   5222 	if (rbus == true)
   5223 		hwp->halt_input(sc->hw_hdl);
   5224 	mutex_exit(sc->sc_intr_lock);
   5225 #ifdef AUDIO_PM_IDLE
   5226 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   5227 #endif
   5228 	mutex_exit(sc->sc_lock);
   5229 
   5230 	return true;
   5231 }
   5232 
   5233 static bool
   5234 audio_resume(device_t dv, const pmf_qual_t *qual)
   5235 {
   5236 	struct audio_softc *sc = device_private(dv);
   5237 	struct audio_chan *chan;
   5238 	struct virtual_channel *vc;
   5239 
   5240 	mutex_enter(sc->sc_lock);
   5241 	sc->sc_trigger_started = false;
   5242 	sc->sc_rec_started = false;
   5243 
   5244 	audio_set_vchan_defaults(sc, AUMODE_PLAY | AUMODE_PLAY_ALL |
   5245 	    AUMODE_RECORD, &sc->sc_format[0]);
   5246 
   5247 	audio_mixer_restore(sc);
   5248 	SIMPLEQ_FOREACH(chan, &sc->sc_audiochan, entries) {
   5249 		if (chan == SIMPLEQ_FIRST(&sc->sc_audiochan) ||
   5250 				chan->chan == MIXER_INUSE)
   5251 			continue;
   5252 		vc = chan->vc;
   5253 
   5254 		if (vc->sc_lastinfovalid == true)
   5255 			audiosetinfo(sc, &vc->sc_lastinfo, true, vc);
   5256 		if (vc->sc_pbus == true && !vc->sc_mpr.pause)
   5257 			audiostartp(sc, vc);
   5258 		if (vc->sc_rbus == true && !vc->sc_mrr.pause)
   5259 			audiostartr(sc, vc);
   5260 	}
   5261 	mutex_exit(sc->sc_lock);
   5262 
   5263 	return true;
   5264 }
   5265 
   5266 static void
   5267 audio_volume_down(device_t dv)
   5268 {
   5269 	struct audio_softc *sc = device_private(dv);
   5270 	mixer_devinfo_t mi;
   5271 	int newgain;
   5272 	u_int gain;
   5273 	u_char balance;
   5274 
   5275 	mutex_enter(sc->sc_lock);
   5276 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   5277 		mi.index = sc->sc_outports.master;
   5278 		mi.un.v.delta = 0;
   5279 		if (audio_query_devinfo(sc, &mi) == 0) {
   5280 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   5281 			newgain = gain - mi.un.v.delta;
   5282 			if (newgain < AUDIO_MIN_GAIN)
   5283 				newgain = AUDIO_MIN_GAIN;
   5284 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   5285 		}
   5286 	}
   5287 	mutex_exit(sc->sc_lock);
   5288 }
   5289 
   5290 static void
   5291 audio_volume_up(device_t dv)
   5292 {
   5293 	struct audio_softc *sc = device_private(dv);
   5294 	mixer_devinfo_t mi;
   5295 	u_int gain, newgain;
   5296 	u_char balance;
   5297 
   5298 	mutex_enter(sc->sc_lock);
   5299 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   5300 		mi.index = sc->sc_outports.master;
   5301 		mi.un.v.delta = 0;
   5302 		if (audio_query_devinfo(sc, &mi) == 0) {
   5303 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   5304 			newgain = gain + mi.un.v.delta;
   5305 			if (newgain > AUDIO_MAX_GAIN)
   5306 				newgain = AUDIO_MAX_GAIN;
   5307 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   5308 		}
   5309 	}
   5310 	mutex_exit(sc->sc_lock);
   5311 }
   5312 
   5313 static void
   5314 audio_volume_toggle(device_t dv)
   5315 {
   5316 	struct audio_softc *sc = device_private(dv);
   5317 	u_int gain, newgain;
   5318 	u_char balance;
   5319 
   5320 	mutex_enter(sc->sc_lock);
   5321 	au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   5322 	if (gain != 0) {
   5323 		sc->sc_lastgain = gain;
   5324 		newgain = 0;
   5325 	} else
   5326 		newgain = sc->sc_lastgain;
   5327 	au_set_gain(sc, &sc->sc_outports, newgain, balance);
   5328 	mutex_exit(sc->sc_lock);
   5329 }
   5330 
   5331 static int
   5332 audio_get_props(struct audio_softc *sc)
   5333 {
   5334 	const struct audio_hw_if *hw;
   5335 	int props;
   5336 
   5337 	KASSERT(mutex_owned(sc->sc_lock));
   5338 
   5339 	hw = sc->hw_if;
   5340 	props = hw->get_props(sc->hw_hdl);
   5341 
   5342 	/*
   5343 	 * if neither playback nor capture properties are reported,
   5344 	 * assume both are supported by the device driver
   5345 	 */
   5346 	if ((props & (AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE)) == 0)
   5347 		props |= (AUDIO_PROP_PLAYBACK | AUDIO_PROP_CAPTURE);
   5348 
   5349 	return props;
   5350 }
   5351 
   5352 static bool
   5353 audio_can_playback(struct audio_softc *sc)
   5354 {
   5355 	return audio_get_props(sc) & AUDIO_PROP_PLAYBACK ? true : false;
   5356 }
   5357 
   5358 static bool
   5359 audio_can_capture(struct audio_softc *sc)
   5360 {
   5361 	return audio_get_props(sc) & AUDIO_PROP_CAPTURE ? true : false;
   5362 }
   5363 
   5364 void
   5365 mix_read(void *arg)
   5366 {
   5367 	struct audio_softc *sc = arg;
   5368 	struct audio_chan *chan;
   5369 	struct virtual_channel *vc;
   5370 	stream_filter_t *filter;
   5371 	stream_fetcher_t *fetcher;
   5372 	stream_fetcher_t null_fetcher;
   5373 	int cc, cc1, blksize, error;
   5374 	uint8_t *inp;
   5375 
   5376 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   5377 	vc = chan->vc;
   5378 	blksize = vc->sc_mrr.blksize;
   5379 	cc = blksize;
   5380 
   5381 	if (sc->hw_if->trigger_input && sc->sc_rec_started == false) {
   5382 		DPRINTF(("%s: call trigger_input\n", __func__));
   5383 		error = sc->hw_if->trigger_input(sc->hw_hdl, vc->sc_mrr.s.start,
   5384 		    vc->sc_mrr.s.end, blksize,
   5385 		    audio_rint, (void *)sc, &vc->sc_mrr.s.param);
   5386 	} else if (sc->hw_if->start_input) {
   5387 		DPRINTF(("%s: call start_input\n", __func__));
   5388 		error = sc->hw_if->start_input(sc->hw_hdl,
   5389 		    vc->sc_mrr.s.inp, blksize,
   5390 		    audio_rint, (void *)sc);
   5391 		if (error) {
   5392 			/* XXX does this really help? */
   5393 			DPRINTF(("audio_upmix restart failed: %d\n", error));
   5394 			audio_clear(sc, 0);
   5395 		}
   5396 	}
   5397 	sc->sc_rec_started = true;
   5398 
   5399 	inp = vc->sc_mrr.s.inp;
   5400 	vc->sc_mrr.s.inp = audio_stream_add_inp(&vc->sc_mrr.s, inp, cc);
   5401 
   5402 	if (vc->sc_nrfilters > 0) {
   5403 		cc = vc->sc_rustream->end - vc->sc_rustream->start;
   5404 		null_fetcher.fetch_to = null_fetcher_fetch_to;
   5405 		filter = vc->sc_rfilters[0];
   5406 		filter->set_fetcher(filter, &null_fetcher);
   5407 		fetcher = &vc->sc_rfilters[vc->sc_nrfilters - 1]->base;
   5408 		fetcher->fetch_to(sc, fetcher, vc->sc_rustream, cc);
   5409 	}
   5410 
   5411 	blksize = audio_stream_get_used(vc->sc_rustream);
   5412 	cc1 = blksize;
   5413 	if (vc->sc_rustream->outp + blksize > vc->sc_rustream->end)
   5414 		cc1 = vc->sc_rustream->end - vc->sc_rustream->outp;
   5415 	memcpy(sc->sc_rr.s.start, vc->sc_rustream->outp, cc1);
   5416 	if (cc1 < blksize) {
   5417 		memcpy(sc->sc_rr.s.start + cc1, vc->sc_rustream->start,
   5418 		    blksize - cc1);
   5419 	}
   5420 	sc->sc_rr.s.inp = audio_stream_add_inp(&sc->sc_rr.s, sc->sc_rr.s.inp,
   5421 	    blksize);
   5422 	vc->sc_rustream->outp = audio_stream_add_outp(vc->sc_rustream,
   5423 	    vc->sc_rustream->outp, blksize);
   5424 }
   5425 
   5426 void
   5427 mix_write(void *arg)
   5428 {
   5429 	struct audio_softc *sc = arg;
   5430 	struct audio_chan *chan;
   5431 	struct virtual_channel *vc;
   5432 	stream_filter_t *filter;
   5433 	stream_fetcher_t *fetcher;
   5434 	stream_fetcher_t null_fetcher;
   5435 	int cc, cc1, cc2, blksize, error, used;
   5436 	uint8_t *inp, *orig, *tocopy;
   5437 
   5438 	chan = SIMPLEQ_FIRST(&sc->sc_audiochan);
   5439 	vc = chan->vc;
   5440 	blksize = vc->sc_mpr.blksize;
   5441 	cc = blksize;
   5442 	error = 0;
   5443 
   5444 	tocopy = vc->sc_pustream->inp;
   5445 	orig = __UNCONST(sc->sc_pr.s.outp);
   5446 	used = blksize;
   5447 	while (used > 0) {
   5448 		cc = used;
   5449 		cc1 = vc->sc_pustream->end - tocopy;
   5450 		cc2 = sc->sc_pr.s.end - orig;
   5451 		if (cc2 < cc1)
   5452 			cc = cc2;
   5453 		else
   5454 			cc = cc1;
   5455 		if (cc > used)
   5456 			cc = used;
   5457 		memcpy(tocopy, orig, cc);
   5458 		orig += cc;
   5459 		tocopy += cc;
   5460 
   5461 		if (tocopy >= vc->sc_pustream->end)
   5462 			tocopy = vc->sc_pustream->start;
   5463 		if (orig >= sc->sc_pr.s.end)
   5464 			orig = sc->sc_pr.s.start;
   5465 
   5466 		used -= cc;
   5467  	}
   5468 
   5469 	inp = vc->sc_pustream->inp;
   5470 	vc->sc_pustream->inp = audio_stream_add_inp(vc->sc_pustream,
   5471 	    inp, blksize);
   5472 
   5473 	cc = blksize;
   5474 	cc2 = sc->sc_pr.s.end - sc->sc_pr.s.inp;
   5475 	if (cc2 < cc) {
   5476 		memset(sc->sc_pr.s.inp, 0, cc2);
   5477 		cc -= cc2;
   5478 		memset(sc->sc_pr.s.start, 0, cc);
   5479 	} else
   5480 		memset(sc->sc_pr.s.inp, 0, cc);
   5481 
   5482 	sc->sc_pr.s.outp = audio_stream_add_outp(&sc->sc_pr.s,
   5483 	    sc->sc_pr.s.outp, blksize);
   5484 
   5485 	if (vc->sc_npfilters > 0) {
   5486 		null_fetcher.fetch_to = null_fetcher_fetch_to;
   5487 		filter = vc->sc_pfilters[0];
   5488 		filter->set_fetcher(filter, &null_fetcher);
   5489 		fetcher = &vc->sc_pfilters[vc->sc_npfilters - 1]->base;
   5490 		fetcher->fetch_to(sc, fetcher, &vc->sc_mpr.s, blksize);
   5491  	}
   5492 
   5493 	if (sc->hw_if->trigger_output && sc->sc_trigger_started == false) {
   5494 		DPRINTF(("%s: call trigger_output\n", __func__));
   5495 		error = sc->hw_if->trigger_output(sc->hw_hdl,
   5496 		    vc->sc_mpr.s.start, vc->sc_mpr.s.end, blksize,
   5497 		    audio_pint, (void *)sc, &vc->sc_mpr.s.param);
   5498 	} else if (sc->hw_if->start_output) {
   5499 		DPRINTF(("%s: call start_output\n", __func__));
   5500 		error = sc->hw_if->start_output(sc->hw_hdl,
   5501 		    __UNCONST(vc->sc_mpr.s.outp), blksize,
   5502 		    audio_pint, (void *)sc);
   5503 	}
   5504 	sc->sc_trigger_started = true;
   5505 
   5506 	if (error) {
   5507 		/* XXX does this really help? */
   5508 		DPRINTF(("audio_mix restart failed: %d\n", error));
   5509 		audio_clear(sc, 0);
   5510 		sc->sc_trigger_started = false;
   5511 	}
   5512 }
   5513 
   5514 void
   5515 mix_func8(struct audio_softc *sc, struct audio_ringbuffer *cb,
   5516 	  struct virtual_channel *vc)
   5517 {
   5518 	int blksize, cc, cc1, cc2, m, resid;
   5519 	int8_t *orig, *tomix;
   5520 
   5521 	blksize = sc->sc_pr.blksize;
   5522 	resid = blksize;
   5523 
   5524 	tomix = __UNCONST(cb->s.outp);
   5525 	orig = (int8_t *)(sc->sc_pr.s.inp);
   5526 
   5527 	while (resid > 0) {
   5528 		cc = resid;
   5529 		cc1 = sc->sc_pr.s.end - (uint8_t *)orig;
   5530 		cc2 = cb->s.end - (uint8_t *)tomix;
   5531 		if (cc > cc1)
   5532 			cc = cc1;
   5533 		if (cc > cc2)
   5534 			cc = cc2;
   5535 
   5536 		for (m = 0; m < cc;  m++) {
   5537 			orig[m] += (int8_t)((int32_t)(tomix[m] *
   5538 			    (vc->sc_swvol + 1)) / (sc->sc_opens *
   5539 			    256));
   5540 		}
   5541 
   5542 		if (&orig[m] >= (int8_t *)sc->sc_pr.s.end)
   5543 			orig = (int8_t *)sc->sc_pr.s.start;
   5544 		if (&tomix[m] >= (int8_t *)cb->s.end)
   5545 			tomix = (int8_t *)cb->s.start;
   5546 
   5547 		resid -= cc;
   5548 	}
   5549 }
   5550 
   5551 void
   5552 mix_func16(struct audio_softc *sc, struct audio_ringbuffer *cb,
   5553 	  struct virtual_channel *vc)
   5554 {
   5555 	int blksize, cc, cc1, cc2, m, resid;
   5556 	int16_t *orig, *tomix;
   5557 
   5558 	blksize = sc->sc_pr.blksize;
   5559 	resid = blksize;
   5560 
   5561 	tomix = __UNCONST(cb->s.outp);
   5562 	orig = (int16_t *)(sc->sc_pr.s.inp);
   5563 
   5564 	while (resid > 0) {
   5565 		cc = resid;
   5566 		cc1 = sc->sc_pr.s.end - (uint8_t *)orig;
   5567 		cc2 = cb->s.end - (uint8_t *)tomix;
   5568 		if (cc > cc1)
   5569 			cc = cc1;
   5570 		if (cc > cc2)
   5571 			cc = cc2;
   5572 
   5573 		for (m = 0; m < cc / 2; m++) {
   5574 			orig[m] += (int16_t)((int32_t)(tomix[m] *
   5575 			    (vc->sc_swvol + 1)) / (sc->sc_opens *
   5576 			    256));
   5577 		}
   5578 
   5579 		if (&orig[m] >= (int16_t *)sc->sc_pr.s.end)
   5580 			orig = (int16_t *)sc->sc_pr.s.start;
   5581 		if (&tomix[m] >= (int16_t *)cb->s.end)
   5582 			tomix = (int16_t *)cb->s.start;
   5583 
   5584 		resid -= cc;
   5585 	}
   5586 }
   5587 
   5588 void
   5589 mix_func32(struct audio_softc *sc, struct audio_ringbuffer *cb,
   5590 	  struct virtual_channel *vc)
   5591 {
   5592 	int blksize, cc, cc1, cc2, m, resid;
   5593 	int32_t *orig, *tomix;
   5594 
   5595 	blksize = sc->sc_pr.blksize;
   5596 	resid = blksize;
   5597 
   5598 	tomix = __UNCONST(cb->s.outp);
   5599 	orig = (int32_t *)(sc->sc_pr.s.inp);
   5600 
   5601 	while (resid > 0) {
   5602 		cc = resid;
   5603 		cc1 = sc->sc_pr.s.end - (uint8_t *)orig;
   5604 		cc2 = cb->s.end - (uint8_t *)tomix;
   5605 		if (cc > cc1)
   5606 			cc = cc1;
   5607 		if (cc > cc2)
   5608 			cc = cc2;
   5609 
   5610 		for (m = 0; m < cc / 4; m++) {
   5611 			orig[m] += (int32_t)((int32_t)(tomix[m] *
   5612 			    (vc->sc_swvol + 1)) / (sc->sc_opens *
   5613 			    256));
   5614 		}
   5615 
   5616 		if (&orig[m] >= (int32_t *)sc->sc_pr.s.end)
   5617 			orig = (int32_t *)sc->sc_pr.s.start;
   5618 		if (&tomix[m] >= (int32_t *)cb->s.end)
   5619 			tomix = (int32_t *)cb->s.start;
   5620 
   5621 		resid -= cc;
   5622 	}
   5623 }
   5624 
   5625 void
   5626 mix_func(struct audio_softc *sc, struct audio_ringbuffer *cb,
   5627 	 struct virtual_channel *vc)
   5628 {
   5629 	switch (sc->sc_precision) {
   5630 	case 8:
   5631 		mix_func8(sc, cb, vc);
   5632 		break;
   5633 	case 16:
   5634 		mix_func16(sc, cb, vc);
   5635 		break;
   5636 	case 24:
   5637 	case 32:
   5638 		mix_func32(sc, cb, vc);
   5639 		break;
   5640 	default:
   5641 		break;
   5642 	}
   5643 }
   5644 
   5645 void
   5646 saturate_func8(struct audio_softc *sc)
   5647 {
   5648 	int blksize, m, i, resid;
   5649 	int8_t *orig;
   5650 
   5651 	blksize = sc->sc_pr.blksize;
   5652 	resid = blksize;
   5653 	if (sc->sc_trigger_started == false)
   5654 		resid *= 2;
   5655 
   5656 	orig = (int8_t *)(sc->sc_pr.s.inp);
   5657 
   5658 	for (m = 0; m < resid;  m++) {
   5659 		i = 0;
   5660 		if (&orig[m] >= (int8_t *)sc->sc_pr.s.end) {
   5661 			orig = (int8_t *)sc->sc_pr.s.start;
   5662 			resid -= m;
   5663 			m = 0;
   5664 		}
   5665 		if (orig[m] != 0) {
   5666 			if (orig[m] > 0)
   5667 				i = INT8_MAX / orig[m];
   5668 			else
   5669 				i = INT8_MIN / orig[m];
   5670 		}
   5671 		if (i > sc->sc_opens)
   5672 			i = sc->sc_opens;
   5673 		orig[m] *= i;
   5674 	}
   5675 }
   5676 
   5677 void
   5678 saturate_func16(struct audio_softc *sc)
   5679 {
   5680 	int blksize, m, i, resid;
   5681 	int16_t *orig;
   5682 
   5683 	blksize = sc->sc_pr.blksize;
   5684 	resid = blksize;
   5685 	if (sc->sc_trigger_started == false)
   5686 		resid *= 2;
   5687 
   5688 	orig = (int16_t *)(sc->sc_pr.s.inp);
   5689 
   5690 	for (m = 0; m < resid / 2;  m++) {
   5691 		i = 0;
   5692 		if (&orig[m] >= (int16_t *)sc->sc_pr.s.end) {
   5693 			orig = (int16_t *)sc->sc_pr.s.start;
   5694 			resid -= m;
   5695 			m = 0;
   5696 		}
   5697 		if (orig[m] != 0) {
   5698 			if (orig[m] > 0)
   5699 				i = INT16_MAX / orig[m];
   5700 			else
   5701 				i = INT16_MIN / orig[m];
   5702 		}
   5703 		if (i > sc->sc_opens)
   5704 			i = sc->sc_opens;
   5705 		orig[m] *= i;
   5706 	}
   5707 }
   5708 
   5709 void
   5710 saturate_func32(struct audio_softc *sc)
   5711 {
   5712 	int blksize, m, i, resid;
   5713 	int32_t *orig;
   5714 
   5715 	blksize = sc->sc_pr.