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