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