Home | History | Annotate | Line # | Download | only in audio
audio.c revision 1.16.2.3
      1  1.16.2.3    martin /*	$NetBSD: audio.c,v 1.16.2.3 2020/04/13 08:04:18 martin Exp $	*/
      2  1.16.2.2  christos 
      3  1.16.2.2  christos /*-
      4  1.16.2.2  christos  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.16.2.2  christos  * All rights reserved.
      6  1.16.2.2  christos  *
      7  1.16.2.2  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.16.2.2  christos  * by Andrew Doran.
      9  1.16.2.2  christos  *
     10  1.16.2.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.16.2.2  christos  * modification, are permitted provided that the following conditions
     12  1.16.2.2  christos  * are met:
     13  1.16.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.16.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.16.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.16.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.16.2.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.16.2.2  christos  *
     19  1.16.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.16.2.2  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.16.2.2  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.16.2.2  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.16.2.2  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.16.2.2  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.16.2.2  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.16.2.2  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.16.2.2  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.16.2.2  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.16.2.2  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.16.2.2  christos  */
     31  1.16.2.2  christos 
     32  1.16.2.2  christos /*
     33  1.16.2.2  christos  * Copyright (c) 1991-1993 Regents of the University of California.
     34  1.16.2.2  christos  * All rights reserved.
     35  1.16.2.2  christos  *
     36  1.16.2.2  christos  * Redistribution and use in source and binary forms, with or without
     37  1.16.2.2  christos  * modification, are permitted provided that the following conditions
     38  1.16.2.2  christos  * are met:
     39  1.16.2.2  christos  * 1. Redistributions of source code must retain the above copyright
     40  1.16.2.2  christos  *    notice, this list of conditions and the following disclaimer.
     41  1.16.2.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.16.2.2  christos  *    notice, this list of conditions and the following disclaimer in the
     43  1.16.2.2  christos  *    documentation and/or other materials provided with the distribution.
     44  1.16.2.2  christos  * 3. All advertising materials mentioning features or use of this software
     45  1.16.2.2  christos  *    must display the following acknowledgement:
     46  1.16.2.2  christos  *	This product includes software developed by the Computer Systems
     47  1.16.2.2  christos  *	Engineering Group at Lawrence Berkeley Laboratory.
     48  1.16.2.2  christos  * 4. Neither the name of the University nor of the Laboratory may be used
     49  1.16.2.2  christos  *    to endorse or promote products derived from this software without
     50  1.16.2.2  christos  *    specific prior written permission.
     51  1.16.2.2  christos  *
     52  1.16.2.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  1.16.2.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.16.2.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.16.2.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  1.16.2.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.16.2.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.16.2.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.16.2.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.16.2.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.16.2.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.16.2.2  christos  * SUCH DAMAGE.
     63  1.16.2.2  christos  */
     64  1.16.2.2  christos 
     65  1.16.2.2  christos /*
     66  1.16.2.2  christos  * Locking: there are three locks per device.
     67  1.16.2.2  christos  *
     68  1.16.2.2  christos  * - sc_lock, provided by the underlying driver.  This is an adaptive lock,
     69  1.16.2.2  christos  *   returned in the second parameter to hw_if->get_locks().  It is known
     70  1.16.2.2  christos  *   as the "thread lock".
     71  1.16.2.2  christos  *
     72  1.16.2.2  christos  *   It serializes access to state in all places except the
     73  1.16.2.2  christos  *   driver's interrupt service routine.  This lock is taken from process
     74  1.16.2.2  christos  *   context (example: access to /dev/audio).  It is also taken from soft
     75  1.16.2.2  christos  *   interrupt handlers in this module, primarily to serialize delivery of
     76  1.16.2.2  christos  *   wakeups.  This lock may be used/provided by modules external to the
     77  1.16.2.2  christos  *   audio subsystem, so take care not to introduce a lock order problem.
     78  1.16.2.2  christos  *   LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
     79  1.16.2.2  christos  *
     80  1.16.2.2  christos  * - sc_intr_lock, provided by the underlying driver.  This may be either a
     81  1.16.2.2  christos  *   spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
     82  1.16.2.2  christos  *   IPL_SOFT*), returned in the first parameter to hw_if->get_locks().  It
     83  1.16.2.2  christos  *   is known as the "interrupt lock".
     84  1.16.2.2  christos  *
     85  1.16.2.2  christos  *   It provides atomic access to the device's hardware state, and to audio
     86  1.16.2.2  christos  *   channel data that may be accessed by the hardware driver's ISR.
     87  1.16.2.2  christos  *   In all places outside the ISR, sc_lock must be held before taking
     88  1.16.2.2  christos  *   sc_intr_lock.  This is to ensure that groups of hardware operations are
     89  1.16.2.2  christos  *   made atomically.  SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
     90  1.16.2.2  christos  *
     91  1.16.2.2  christos  * - sc_exlock, private to this module.  This is a variable protected by
     92  1.16.2.2  christos  *   sc_lock.  It is known as the "critical section".
     93  1.16.2.2  christos  *   Some operations release sc_lock in order to allocate memory, to wait
     94  1.16.2.2  christos  *   for in-flight I/O to complete, to copy to/from user context, etc.
     95  1.16.2.2  christos  *   sc_exlock provides a critical section even under the circumstance.
     96  1.16.2.2  christos  *   "+" in following list indicates the interfaces which necessary to be
     97  1.16.2.2  christos  *   protected by sc_exlock.
     98  1.16.2.2  christos  *
     99  1.16.2.2  christos  * List of hardware interface methods, and which locks are held when each
    100  1.16.2.2  christos  * is called by this module:
    101  1.16.2.2  christos  *
    102  1.16.2.2  christos  *	METHOD			INTR	THREAD  NOTES
    103  1.16.2.2  christos  *	----------------------- ------- -------	-------------------------
    104  1.16.2.2  christos  *	open 			x	x +
    105  1.16.2.2  christos  *	close 			x	x +
    106  1.16.2.2  christos  *	query_format		-	x
    107  1.16.2.2  christos  *	set_format		-	x
    108  1.16.2.2  christos  *	round_blocksize		-	x
    109  1.16.2.2  christos  *	commit_settings		-	x
    110  1.16.2.2  christos  *	init_output 		x	x
    111  1.16.2.2  christos  *	init_input 		x	x
    112  1.16.2.2  christos  *	start_output 		x	x +
    113  1.16.2.2  christos  *	start_input 		x	x +
    114  1.16.2.2  christos  *	halt_output 		x	x +
    115  1.16.2.2  christos  *	halt_input 		x	x +
    116  1.16.2.2  christos  *	speaker_ctl 		x	x
    117  1.16.2.2  christos  *	getdev 			-	x
    118  1.16.2.2  christos  *	set_port 		-	x +
    119  1.16.2.2  christos  *	get_port 		-	x +
    120  1.16.2.2  christos  *	query_devinfo 		-	x
    121  1.16.2.3    martin  *	allocm 			-	- +
    122  1.16.2.3    martin  *	freem 			-	- +
    123  1.16.2.2  christos  *	round_buffersize 	-	x
    124  1.16.2.3    martin  *	get_props 		-	-	Called at attach time
    125  1.16.2.2  christos  *	trigger_output 		x	x +
    126  1.16.2.2  christos  *	trigger_input 		x	x +
    127  1.16.2.2  christos  *	dev_ioctl 		-	x
    128  1.16.2.2  christos  *	get_locks 		-	-	Called at attach time
    129  1.16.2.2  christos  *
    130  1.16.2.2  christos  * In addition, there is an additional lock.
    131  1.16.2.2  christos  *
    132  1.16.2.2  christos  * - track->lock.  This is an atomic variable and is similar to the
    133  1.16.2.2  christos  *   "interrupt lock".  This is one for each track.  If any thread context
    134  1.16.2.2  christos  *   (and software interrupt context) and hardware interrupt context who
    135  1.16.2.2  christos  *   want to access some variables on this track, they must acquire this
    136  1.16.2.2  christos  *   lock before.  It protects track's consistency between hardware
    137  1.16.2.2  christos  *   interrupt context and others.
    138  1.16.2.2  christos  */
    139  1.16.2.2  christos 
    140  1.16.2.2  christos #include <sys/cdefs.h>
    141  1.16.2.3    martin __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.16.2.3 2020/04/13 08:04:18 martin Exp $");
    142  1.16.2.2  christos 
    143  1.16.2.2  christos #ifdef _KERNEL_OPT
    144  1.16.2.2  christos #include "audio.h"
    145  1.16.2.2  christos #include "midi.h"
    146  1.16.2.2  christos #endif
    147  1.16.2.2  christos 
    148  1.16.2.2  christos #if NAUDIO > 0
    149  1.16.2.2  christos 
    150  1.16.2.2  christos #include <sys/types.h>
    151  1.16.2.2  christos #include <sys/param.h>
    152  1.16.2.2  christos #include <sys/atomic.h>
    153  1.16.2.2  christos #include <sys/audioio.h>
    154  1.16.2.2  christos #include <sys/conf.h>
    155  1.16.2.2  christos #include <sys/cpu.h>
    156  1.16.2.2  christos #include <sys/device.h>
    157  1.16.2.2  christos #include <sys/fcntl.h>
    158  1.16.2.2  christos #include <sys/file.h>
    159  1.16.2.2  christos #include <sys/filedesc.h>
    160  1.16.2.2  christos #include <sys/intr.h>
    161  1.16.2.2  christos #include <sys/ioctl.h>
    162  1.16.2.2  christos #include <sys/kauth.h>
    163  1.16.2.2  christos #include <sys/kernel.h>
    164  1.16.2.2  christos #include <sys/kmem.h>
    165  1.16.2.2  christos #include <sys/malloc.h>
    166  1.16.2.2  christos #include <sys/mman.h>
    167  1.16.2.2  christos #include <sys/module.h>
    168  1.16.2.2  christos #include <sys/poll.h>
    169  1.16.2.2  christos #include <sys/proc.h>
    170  1.16.2.2  christos #include <sys/queue.h>
    171  1.16.2.2  christos #include <sys/select.h>
    172  1.16.2.2  christos #include <sys/signalvar.h>
    173  1.16.2.2  christos #include <sys/stat.h>
    174  1.16.2.2  christos #include <sys/sysctl.h>
    175  1.16.2.2  christos #include <sys/systm.h>
    176  1.16.2.2  christos #include <sys/syslog.h>
    177  1.16.2.2  christos #include <sys/vnode.h>
    178  1.16.2.2  christos 
    179  1.16.2.2  christos #include <dev/audio/audio_if.h>
    180  1.16.2.2  christos #include <dev/audio/audiovar.h>
    181  1.16.2.2  christos #include <dev/audio/audiodef.h>
    182  1.16.2.2  christos #include <dev/audio/linear.h>
    183  1.16.2.2  christos #include <dev/audio/mulaw.h>
    184  1.16.2.2  christos 
    185  1.16.2.2  christos #include <machine/endian.h>
    186  1.16.2.2  christos 
    187  1.16.2.3    martin #include <uvm/uvm_extern.h>
    188  1.16.2.2  christos 
    189  1.16.2.2  christos #include "ioconf.h"
    190  1.16.2.2  christos 
    191  1.16.2.2  christos /*
    192  1.16.2.2  christos  * 0: No debug logs
    193  1.16.2.2  christos  * 1: action changes like open/close/set_format...
    194  1.16.2.2  christos  * 2: + normal operations like read/write/ioctl...
    195  1.16.2.2  christos  * 3: + TRACEs except interrupt
    196  1.16.2.2  christos  * 4: + TRACEs including interrupt
    197  1.16.2.2  christos  */
    198  1.16.2.2  christos //#define AUDIO_DEBUG 1
    199  1.16.2.2  christos 
    200  1.16.2.2  christos #if defined(AUDIO_DEBUG)
    201  1.16.2.2  christos 
    202  1.16.2.2  christos int audiodebug = AUDIO_DEBUG;
    203  1.16.2.2  christos static void audio_vtrace(struct audio_softc *sc, const char *, const char *,
    204  1.16.2.2  christos 	const char *, va_list);
    205  1.16.2.2  christos static void audio_trace(struct audio_softc *sc, const char *, const char *, ...)
    206  1.16.2.2  christos 	__printflike(3, 4);
    207  1.16.2.2  christos static void audio_tracet(const char *, audio_track_t *, const char *, ...)
    208  1.16.2.2  christos 	__printflike(3, 4);
    209  1.16.2.2  christos static void audio_tracef(const char *, audio_file_t *, const char *, ...)
    210  1.16.2.2  christos 	__printflike(3, 4);
    211  1.16.2.2  christos 
    212  1.16.2.2  christos /* XXX sloppy memory logger */
    213  1.16.2.2  christos static void audio_mlog_init(void);
    214  1.16.2.2  christos static void audio_mlog_free(void);
    215  1.16.2.2  christos static void audio_mlog_softintr(void *);
    216  1.16.2.2  christos extern void audio_mlog_flush(void);
    217  1.16.2.2  christos extern void audio_mlog_printf(const char *, ...);
    218  1.16.2.2  christos 
    219  1.16.2.2  christos static int mlog_refs;		/* reference counter */
    220  1.16.2.2  christos static char *mlog_buf[2];	/* double buffer */
    221  1.16.2.2  christos static int mlog_buflen;		/* buffer length */
    222  1.16.2.2  christos static int mlog_used;		/* used length */
    223  1.16.2.2  christos static int mlog_full;		/* number of dropped lines by buffer full */
    224  1.16.2.2  christos static int mlog_drop;		/* number of dropped lines by busy */
    225  1.16.2.2  christos static volatile uint32_t mlog_inuse;	/* in-use */
    226  1.16.2.2  christos static int mlog_wpage;		/* active page */
    227  1.16.2.2  christos static void *mlog_sih;		/* softint handle */
    228  1.16.2.2  christos 
    229  1.16.2.2  christos static void
    230  1.16.2.2  christos audio_mlog_init(void)
    231  1.16.2.2  christos {
    232  1.16.2.2  christos 	mlog_refs++;
    233  1.16.2.2  christos 	if (mlog_refs > 1)
    234  1.16.2.2  christos 		return;
    235  1.16.2.2  christos 	mlog_buflen = 4096;
    236  1.16.2.2  christos 	mlog_buf[0] = kmem_zalloc(mlog_buflen, KM_SLEEP);
    237  1.16.2.2  christos 	mlog_buf[1] = kmem_zalloc(mlog_buflen, KM_SLEEP);
    238  1.16.2.2  christos 	mlog_used = 0;
    239  1.16.2.2  christos 	mlog_full = 0;
    240  1.16.2.2  christos 	mlog_drop = 0;
    241  1.16.2.2  christos 	mlog_inuse = 0;
    242  1.16.2.2  christos 	mlog_wpage = 0;
    243  1.16.2.2  christos 	mlog_sih = softint_establish(SOFTINT_SERIAL, audio_mlog_softintr, NULL);
    244  1.16.2.2  christos 	if (mlog_sih == NULL)
    245  1.16.2.2  christos 		printf("%s: softint_establish failed\n", __func__);
    246  1.16.2.2  christos }
    247  1.16.2.2  christos 
    248  1.16.2.2  christos static void
    249  1.16.2.2  christos audio_mlog_free(void)
    250  1.16.2.2  christos {
    251  1.16.2.2  christos 	mlog_refs--;
    252  1.16.2.2  christos 	if (mlog_refs > 0)
    253  1.16.2.2  christos 		return;
    254  1.16.2.2  christos 
    255  1.16.2.2  christos 	audio_mlog_flush();
    256  1.16.2.2  christos 	if (mlog_sih)
    257  1.16.2.2  christos 		softint_disestablish(mlog_sih);
    258  1.16.2.2  christos 	kmem_free(mlog_buf[0], mlog_buflen);
    259  1.16.2.2  christos 	kmem_free(mlog_buf[1], mlog_buflen);
    260  1.16.2.2  christos }
    261  1.16.2.2  christos 
    262  1.16.2.2  christos /*
    263  1.16.2.2  christos  * Flush memory buffer.
    264  1.16.2.2  christos  * It must not be called from hardware interrupt context.
    265  1.16.2.2  christos  */
    266  1.16.2.2  christos void
    267  1.16.2.2  christos audio_mlog_flush(void)
    268  1.16.2.2  christos {
    269  1.16.2.2  christos 	if (mlog_refs == 0)
    270  1.16.2.2  christos 		return;
    271  1.16.2.2  christos 
    272  1.16.2.2  christos 	/* Nothing to do if already in use ? */
    273  1.16.2.2  christos 	if (atomic_swap_32(&mlog_inuse, 1) == 1)
    274  1.16.2.2  christos 		return;
    275  1.16.2.2  christos 
    276  1.16.2.2  christos 	int rpage = mlog_wpage;
    277  1.16.2.2  christos 	mlog_wpage ^= 1;
    278  1.16.2.2  christos 	mlog_buf[mlog_wpage][0] = '\0';
    279  1.16.2.2  christos 	mlog_used = 0;
    280  1.16.2.2  christos 
    281  1.16.2.2  christos 	atomic_swap_32(&mlog_inuse, 0);
    282  1.16.2.2  christos 
    283  1.16.2.2  christos 	if (mlog_buf[rpage][0] != '\0') {
    284  1.16.2.2  christos 		printf("%s", mlog_buf[rpage]);
    285  1.16.2.2  christos 		if (mlog_drop > 0)
    286  1.16.2.2  christos 			printf("mlog_drop %d\n", mlog_drop);
    287  1.16.2.2  christos 		if (mlog_full > 0)
    288  1.16.2.2  christos 			printf("mlog_full %d\n", mlog_full);
    289  1.16.2.2  christos 	}
    290  1.16.2.2  christos 	mlog_full = 0;
    291  1.16.2.2  christos 	mlog_drop = 0;
    292  1.16.2.2  christos }
    293  1.16.2.2  christos 
    294  1.16.2.2  christos static void
    295  1.16.2.2  christos audio_mlog_softintr(void *cookie)
    296  1.16.2.2  christos {
    297  1.16.2.2  christos 	audio_mlog_flush();
    298  1.16.2.2  christos }
    299  1.16.2.2  christos 
    300  1.16.2.2  christos void
    301  1.16.2.2  christos audio_mlog_printf(const char *fmt, ...)
    302  1.16.2.2  christos {
    303  1.16.2.2  christos 	int len;
    304  1.16.2.2  christos 	va_list ap;
    305  1.16.2.2  christos 
    306  1.16.2.2  christos 	if (atomic_swap_32(&mlog_inuse, 1) == 1) {
    307  1.16.2.2  christos 		/* already inuse */
    308  1.16.2.2  christos 		mlog_drop++;
    309  1.16.2.2  christos 		return;
    310  1.16.2.2  christos 	}
    311  1.16.2.2  christos 
    312  1.16.2.2  christos 	va_start(ap, fmt);
    313  1.16.2.2  christos 	len = vsnprintf(
    314  1.16.2.2  christos 	    mlog_buf[mlog_wpage] + mlog_used,
    315  1.16.2.2  christos 	    mlog_buflen - mlog_used,
    316  1.16.2.2  christos 	    fmt, ap);
    317  1.16.2.2  christos 	va_end(ap);
    318  1.16.2.2  christos 
    319  1.16.2.2  christos 	mlog_used += len;
    320  1.16.2.2  christos 	if (mlog_buflen - mlog_used <= 1) {
    321  1.16.2.2  christos 		mlog_full++;
    322  1.16.2.2  christos 	}
    323  1.16.2.2  christos 
    324  1.16.2.2  christos 	atomic_swap_32(&mlog_inuse, 0);
    325  1.16.2.2  christos 
    326  1.16.2.2  christos 	if (mlog_sih)
    327  1.16.2.2  christos 		softint_schedule(mlog_sih);
    328  1.16.2.2  christos }
    329  1.16.2.2  christos 
    330  1.16.2.2  christos /* trace functions */
    331  1.16.2.2  christos static void
    332  1.16.2.2  christos audio_vtrace(struct audio_softc *sc, const char *funcname, const char *header,
    333  1.16.2.2  christos 	const char *fmt, va_list ap)
    334  1.16.2.2  christos {
    335  1.16.2.2  christos 	char buf[256];
    336  1.16.2.2  christos 	int n;
    337  1.16.2.2  christos 
    338  1.16.2.2  christos 	n = 0;
    339  1.16.2.2  christos 	buf[0] = '\0';
    340  1.16.2.2  christos 	n += snprintf(buf + n, sizeof(buf) - n, "%s@%d %s",
    341  1.16.2.2  christos 	    funcname, device_unit(sc->sc_dev), header);
    342  1.16.2.2  christos 	n += vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
    343  1.16.2.2  christos 
    344  1.16.2.2  christos 	if (cpu_intr_p()) {
    345  1.16.2.2  christos 		audio_mlog_printf("%s\n", buf);
    346  1.16.2.2  christos 	} else {
    347  1.16.2.2  christos 		audio_mlog_flush();
    348  1.16.2.2  christos 		printf("%s\n", buf);
    349  1.16.2.2  christos 	}
    350  1.16.2.2  christos }
    351  1.16.2.2  christos 
    352  1.16.2.2  christos static void
    353  1.16.2.2  christos audio_trace(struct audio_softc *sc, const char *funcname, const char *fmt, ...)
    354  1.16.2.2  christos {
    355  1.16.2.2  christos 	va_list ap;
    356  1.16.2.2  christos 
    357  1.16.2.2  christos 	va_start(ap, fmt);
    358  1.16.2.2  christos 	audio_vtrace(sc, funcname, "", fmt, ap);
    359  1.16.2.2  christos 	va_end(ap);
    360  1.16.2.2  christos }
    361  1.16.2.2  christos 
    362  1.16.2.2  christos static void
    363  1.16.2.2  christos audio_tracet(const char *funcname, audio_track_t *track, const char *fmt, ...)
    364  1.16.2.2  christos {
    365  1.16.2.2  christos 	char hdr[16];
    366  1.16.2.2  christos 	va_list ap;
    367  1.16.2.2  christos 
    368  1.16.2.2  christos 	snprintf(hdr, sizeof(hdr), "#%d ", track->id);
    369  1.16.2.2  christos 	va_start(ap, fmt);
    370  1.16.2.2  christos 	audio_vtrace(track->mixer->sc, funcname, hdr, fmt, ap);
    371  1.16.2.2  christos 	va_end(ap);
    372  1.16.2.2  christos }
    373  1.16.2.2  christos 
    374  1.16.2.2  christos static void
    375  1.16.2.2  christos audio_tracef(const char *funcname, audio_file_t *file, const char *fmt, ...)
    376  1.16.2.2  christos {
    377  1.16.2.2  christos 	char hdr[32];
    378  1.16.2.2  christos 	char phdr[16], rhdr[16];
    379  1.16.2.2  christos 	va_list ap;
    380  1.16.2.2  christos 
    381  1.16.2.2  christos 	phdr[0] = '\0';
    382  1.16.2.2  christos 	rhdr[0] = '\0';
    383  1.16.2.2  christos 	if (file->ptrack)
    384  1.16.2.2  christos 		snprintf(phdr, sizeof(phdr), "#%d", file->ptrack->id);
    385  1.16.2.2  christos 	if (file->rtrack)
    386  1.16.2.2  christos 		snprintf(rhdr, sizeof(rhdr), "#%d", file->rtrack->id);
    387  1.16.2.2  christos 	snprintf(hdr, sizeof(hdr), "{%s,%s} ", phdr, rhdr);
    388  1.16.2.2  christos 
    389  1.16.2.2  christos 	va_start(ap, fmt);
    390  1.16.2.2  christos 	audio_vtrace(file->sc, funcname, hdr, fmt, ap);
    391  1.16.2.2  christos 	va_end(ap);
    392  1.16.2.2  christos }
    393  1.16.2.2  christos 
    394  1.16.2.2  christos #define DPRINTF(n, fmt...)	do {	\
    395  1.16.2.2  christos 	if (audiodebug >= (n)) {	\
    396  1.16.2.2  christos 		audio_mlog_flush();	\
    397  1.16.2.2  christos 		printf(fmt);		\
    398  1.16.2.2  christos 	}				\
    399  1.16.2.2  christos } while (0)
    400  1.16.2.2  christos #define TRACE(n, fmt...)	do { \
    401  1.16.2.2  christos 	if (audiodebug >= (n)) audio_trace(sc, __func__, fmt); \
    402  1.16.2.2  christos } while (0)
    403  1.16.2.2  christos #define TRACET(n, t, fmt...)	do { \
    404  1.16.2.2  christos 	if (audiodebug >= (n)) audio_tracet(__func__, t, fmt); \
    405  1.16.2.2  christos } while (0)
    406  1.16.2.2  christos #define TRACEF(n, f, fmt...)	do { \
    407  1.16.2.2  christos 	if (audiodebug >= (n)) audio_tracef(__func__, f, fmt); \
    408  1.16.2.2  christos } while (0)
    409  1.16.2.2  christos 
    410  1.16.2.2  christos struct audio_track_debugbuf {
    411  1.16.2.2  christos 	char usrbuf[32];
    412  1.16.2.2  christos 	char codec[32];
    413  1.16.2.2  christos 	char chvol[32];
    414  1.16.2.2  christos 	char chmix[32];
    415  1.16.2.2  christos 	char freq[32];
    416  1.16.2.2  christos 	char outbuf[32];
    417  1.16.2.2  christos };
    418  1.16.2.2  christos 
    419  1.16.2.2  christos static void
    420  1.16.2.2  christos audio_track_bufstat(audio_track_t *track, struct audio_track_debugbuf *buf)
    421  1.16.2.2  christos {
    422  1.16.2.2  christos 
    423  1.16.2.2  christos 	memset(buf, 0, sizeof(*buf));
    424  1.16.2.2  christos 
    425  1.16.2.2  christos 	snprintf(buf->outbuf, sizeof(buf->outbuf), " out=%d/%d/%d",
    426  1.16.2.2  christos 	    track->outbuf.head, track->outbuf.used, track->outbuf.capacity);
    427  1.16.2.2  christos 	if (track->freq.filter)
    428  1.16.2.2  christos 		snprintf(buf->freq, sizeof(buf->freq), " f=%d/%d/%d",
    429  1.16.2.2  christos 		    track->freq.srcbuf.head,
    430  1.16.2.2  christos 		    track->freq.srcbuf.used,
    431  1.16.2.2  christos 		    track->freq.srcbuf.capacity);
    432  1.16.2.2  christos 	if (track->chmix.filter)
    433  1.16.2.2  christos 		snprintf(buf->chmix, sizeof(buf->chmix), " m=%d",
    434  1.16.2.2  christos 		    track->chmix.srcbuf.used);
    435  1.16.2.2  christos 	if (track->chvol.filter)
    436  1.16.2.2  christos 		snprintf(buf->chvol, sizeof(buf->chvol), " v=%d",
    437  1.16.2.2  christos 		    track->chvol.srcbuf.used);
    438  1.16.2.2  christos 	if (track->codec.filter)
    439  1.16.2.2  christos 		snprintf(buf->codec, sizeof(buf->codec), " e=%d",
    440  1.16.2.2  christos 		    track->codec.srcbuf.used);
    441  1.16.2.2  christos 	snprintf(buf->usrbuf, sizeof(buf->usrbuf), " usr=%d/%d/H%d",
    442  1.16.2.2  christos 	    track->usrbuf.head, track->usrbuf.used, track->usrbuf_usedhigh);
    443  1.16.2.2  christos }
    444  1.16.2.2  christos #else
    445  1.16.2.2  christos #define DPRINTF(n, fmt...)	do { } while (0)
    446  1.16.2.2  christos #define TRACE(n, fmt, ...)	do { } while (0)
    447  1.16.2.2  christos #define TRACET(n, t, fmt, ...)	do { } while (0)
    448  1.16.2.2  christos #define TRACEF(n, f, fmt, ...)	do { } while (0)
    449  1.16.2.2  christos #endif
    450  1.16.2.2  christos 
    451  1.16.2.2  christos #define SPECIFIED(x)	((x) != ~0)
    452  1.16.2.2  christos #define SPECIFIED_CH(x)	((x) != (u_char)~0)
    453  1.16.2.2  christos 
    454  1.16.2.2  christos /* Device timeout in msec */
    455  1.16.2.2  christos #define AUDIO_TIMEOUT	(3000)
    456  1.16.2.2  christos 
    457  1.16.2.2  christos /* #define AUDIO_PM_IDLE */
    458  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
    459  1.16.2.2  christos int audio_idle_timeout = 30;
    460  1.16.2.2  christos #endif
    461  1.16.2.2  christos 
    462  1.16.2.3    martin /* Number of elements of async mixer's pid */
    463  1.16.2.3    martin #define AM_CAPACITY	(4)
    464  1.16.2.3    martin 
    465  1.16.2.2  christos struct portname {
    466  1.16.2.2  christos 	const char *name;
    467  1.16.2.2  christos 	int mask;
    468  1.16.2.2  christos };
    469  1.16.2.2  christos 
    470  1.16.2.2  christos static int audiomatch(device_t, cfdata_t, void *);
    471  1.16.2.2  christos static void audioattach(device_t, device_t, void *);
    472  1.16.2.2  christos static int audiodetach(device_t, int);
    473  1.16.2.2  christos static int audioactivate(device_t, enum devact);
    474  1.16.2.2  christos static void audiochilddet(device_t, device_t);
    475  1.16.2.2  christos static int audiorescan(device_t, const char *, const int *);
    476  1.16.2.2  christos 
    477  1.16.2.2  christos static int audio_modcmd(modcmd_t, void *);
    478  1.16.2.2  christos 
    479  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
    480  1.16.2.2  christos static void audio_idle(void *);
    481  1.16.2.2  christos static void audio_activity(device_t, devactive_t);
    482  1.16.2.2  christos #endif
    483  1.16.2.2  christos 
    484  1.16.2.2  christos static bool audio_suspend(device_t dv, const pmf_qual_t *);
    485  1.16.2.2  christos static bool audio_resume(device_t dv, const pmf_qual_t *);
    486  1.16.2.2  christos static void audio_volume_down(device_t);
    487  1.16.2.2  christos static void audio_volume_up(device_t);
    488  1.16.2.2  christos static void audio_volume_toggle(device_t);
    489  1.16.2.2  christos 
    490  1.16.2.2  christos static void audio_mixer_capture(struct audio_softc *);
    491  1.16.2.2  christos static void audio_mixer_restore(struct audio_softc *);
    492  1.16.2.2  christos 
    493  1.16.2.2  christos static void audio_softintr_rd(void *);
    494  1.16.2.2  christos static void audio_softintr_wr(void *);
    495  1.16.2.2  christos 
    496  1.16.2.3    martin static int audio_exlock_mutex_enter(struct audio_softc *);
    497  1.16.2.3    martin static void audio_exlock_mutex_exit(struct audio_softc *);
    498  1.16.2.3    martin static int audio_exlock_enter(struct audio_softc *);
    499  1.16.2.3    martin static void audio_exlock_exit(struct audio_softc *);
    500  1.16.2.3    martin static struct audio_softc *audio_file_enter(audio_file_t *, struct psref *);
    501  1.16.2.3    martin static void audio_file_exit(struct audio_softc *, struct psref *);
    502  1.16.2.2  christos static int audio_track_waitio(struct audio_softc *, audio_track_t *);
    503  1.16.2.2  christos 
    504  1.16.2.2  christos static int audioclose(struct file *);
    505  1.16.2.2  christos static int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    506  1.16.2.2  christos static int audiowrite(struct file *, off_t *, struct uio *, kauth_cred_t, int);
    507  1.16.2.2  christos static int audioioctl(struct file *, u_long, void *);
    508  1.16.2.2  christos static int audiopoll(struct file *, int);
    509  1.16.2.2  christos static int audiokqfilter(struct file *, struct knote *);
    510  1.16.2.2  christos static int audiommap(struct file *, off_t *, size_t, int, int *, int *,
    511  1.16.2.2  christos 	struct uvm_object **, int *);
    512  1.16.2.2  christos static int audiostat(struct file *, struct stat *);
    513  1.16.2.2  christos 
    514  1.16.2.2  christos static void filt_audiowrite_detach(struct knote *);
    515  1.16.2.2  christos static int  filt_audiowrite_event(struct knote *, long);
    516  1.16.2.2  christos static void filt_audioread_detach(struct knote *);
    517  1.16.2.2  christos static int  filt_audioread_event(struct knote *, long);
    518  1.16.2.2  christos 
    519  1.16.2.2  christos static int audio_open(dev_t, struct audio_softc *, int, int, struct lwp *,
    520  1.16.2.3    martin 	audio_file_t **);
    521  1.16.2.2  christos static int audio_close(struct audio_softc *, audio_file_t *);
    522  1.16.2.3    martin static int audio_unlink(struct audio_softc *, audio_file_t *);
    523  1.16.2.2  christos static int audio_read(struct audio_softc *, struct uio *, int, audio_file_t *);
    524  1.16.2.2  christos static int audio_write(struct audio_softc *, struct uio *, int, audio_file_t *);
    525  1.16.2.2  christos static void audio_file_clear(struct audio_softc *, audio_file_t *);
    526  1.16.2.2  christos static int audio_ioctl(dev_t, struct audio_softc *, u_long, void *, int,
    527  1.16.2.2  christos 	struct lwp *, audio_file_t *);
    528  1.16.2.2  christos static int audio_poll(struct audio_softc *, int, struct lwp *, audio_file_t *);
    529  1.16.2.2  christos static int audio_kqfilter(struct audio_softc *, audio_file_t *, struct knote *);
    530  1.16.2.2  christos static int audio_mmap(struct audio_softc *, off_t *, size_t, int, int *, int *,
    531  1.16.2.2  christos 	struct uvm_object **, int *, audio_file_t *);
    532  1.16.2.2  christos 
    533  1.16.2.2  christos static int audioctl_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    534  1.16.2.2  christos 
    535  1.16.2.2  christos static void audio_pintr(void *);
    536  1.16.2.2  christos static void audio_rintr(void *);
    537  1.16.2.2  christos 
    538  1.16.2.2  christos static int audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
    539  1.16.2.2  christos 
    540  1.16.2.2  christos static __inline int audio_track_readablebytes(const audio_track_t *);
    541  1.16.2.2  christos static int audio_file_setinfo(struct audio_softc *, audio_file_t *,
    542  1.16.2.2  christos 	const struct audio_info *);
    543  1.16.2.3    martin static int audio_track_setinfo_check(audio_track_t *,
    544  1.16.2.3    martin 	audio_format2_t *, const struct audio_prinfo *);
    545  1.16.2.2  christos static void audio_track_setinfo_water(audio_track_t *,
    546  1.16.2.2  christos 	const struct audio_info *);
    547  1.16.2.2  christos static int audio_hw_setinfo(struct audio_softc *, const struct audio_info *,
    548  1.16.2.2  christos 	struct audio_info *);
    549  1.16.2.2  christos static int audio_hw_set_format(struct audio_softc *, int,
    550  1.16.2.3    martin 	const audio_format2_t *, const audio_format2_t *,
    551  1.16.2.2  christos 	audio_filter_reg_t *, audio_filter_reg_t *);
    552  1.16.2.2  christos static int audiogetinfo(struct audio_softc *, struct audio_info *, int,
    553  1.16.2.2  christos 	audio_file_t *);
    554  1.16.2.2  christos static bool audio_can_playback(struct audio_softc *);
    555  1.16.2.2  christos static bool audio_can_capture(struct audio_softc *);
    556  1.16.2.2  christos static int audio_check_params(audio_format2_t *);
    557  1.16.2.2  christos static int audio_mixers_init(struct audio_softc *sc, int,
    558  1.16.2.2  christos 	const audio_format2_t *, const audio_format2_t *,
    559  1.16.2.2  christos 	const audio_filter_reg_t *, const audio_filter_reg_t *);
    560  1.16.2.2  christos static int audio_select_freq(const struct audio_format *);
    561  1.16.2.3    martin static int audio_hw_probe(struct audio_softc *, audio_format2_t *, int);
    562  1.16.2.2  christos static int audio_hw_validate_format(struct audio_softc *, int,
    563  1.16.2.2  christos 	const audio_format2_t *);
    564  1.16.2.2  christos static int audio_mixers_set_format(struct audio_softc *,
    565  1.16.2.2  christos 	const struct audio_info *);
    566  1.16.2.2  christos static void audio_mixers_get_format(struct audio_softc *, struct audio_info *);
    567  1.16.2.2  christos static int audio_sysctl_blk_ms(SYSCTLFN_PROTO);
    568  1.16.2.2  christos static int audio_sysctl_multiuser(SYSCTLFN_PROTO);
    569  1.16.2.2  christos #if defined(AUDIO_DEBUG)
    570  1.16.2.2  christos static int audio_sysctl_debug(SYSCTLFN_PROTO);
    571  1.16.2.2  christos static void audio_format2_tostr(char *, size_t, const audio_format2_t *);
    572  1.16.2.2  christos static void audio_print_format2(const char *, const audio_format2_t *) __unused;
    573  1.16.2.2  christos #endif
    574  1.16.2.2  christos 
    575  1.16.2.2  christos static void *audio_realloc(void *, size_t);
    576  1.16.2.2  christos static int audio_realloc_usrbuf(audio_track_t *, int);
    577  1.16.2.2  christos static void audio_free_usrbuf(audio_track_t *);
    578  1.16.2.2  christos 
    579  1.16.2.2  christos static audio_track_t *audio_track_create(struct audio_softc *,
    580  1.16.2.2  christos 	audio_trackmixer_t *);
    581  1.16.2.2  christos static void audio_track_destroy(audio_track_t *);
    582  1.16.2.2  christos static audio_filter_t audio_track_get_codec(audio_track_t *,
    583  1.16.2.2  christos 	const audio_format2_t *, const audio_format2_t *);
    584  1.16.2.2  christos static int audio_track_set_format(audio_track_t *, audio_format2_t *);
    585  1.16.2.2  christos static void audio_track_play(audio_track_t *);
    586  1.16.2.2  christos static int audio_track_drain(struct audio_softc *, audio_track_t *);
    587  1.16.2.2  christos static void audio_track_record(audio_track_t *);
    588  1.16.2.2  christos static void audio_track_clear(struct audio_softc *, audio_track_t *);
    589  1.16.2.2  christos 
    590  1.16.2.2  christos static int audio_mixer_init(struct audio_softc *, int,
    591  1.16.2.2  christos 	const audio_format2_t *, const audio_filter_reg_t *);
    592  1.16.2.2  christos static void audio_mixer_destroy(struct audio_softc *, audio_trackmixer_t *);
    593  1.16.2.2  christos static void audio_pmixer_start(struct audio_softc *, bool);
    594  1.16.2.2  christos static void audio_pmixer_process(struct audio_softc *);
    595  1.16.2.3    martin static void audio_pmixer_agc(audio_trackmixer_t *, int);
    596  1.16.2.2  christos static int  audio_pmixer_mix_track(audio_trackmixer_t *, audio_track_t *, int);
    597  1.16.2.2  christos static void audio_pmixer_output(struct audio_softc *);
    598  1.16.2.2  christos static int  audio_pmixer_halt(struct audio_softc *);
    599  1.16.2.2  christos static void audio_rmixer_start(struct audio_softc *);
    600  1.16.2.2  christos static void audio_rmixer_process(struct audio_softc *);
    601  1.16.2.2  christos static void audio_rmixer_input(struct audio_softc *);
    602  1.16.2.2  christos static int  audio_rmixer_halt(struct audio_softc *);
    603  1.16.2.2  christos 
    604  1.16.2.2  christos static void mixer_init(struct audio_softc *);
    605  1.16.2.2  christos static int mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *);
    606  1.16.2.2  christos static int mixer_close(struct audio_softc *, audio_file_t *);
    607  1.16.2.2  christos static int mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
    608  1.16.2.3    martin static void mixer_async_add(struct audio_softc *, pid_t);
    609  1.16.2.3    martin static void mixer_async_remove(struct audio_softc *, pid_t);
    610  1.16.2.2  christos static void mixer_signal(struct audio_softc *);
    611  1.16.2.2  christos 
    612  1.16.2.2  christos static int au_portof(struct audio_softc *, char *, int);
    613  1.16.2.2  christos 
    614  1.16.2.2  christos static void au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
    615  1.16.2.2  christos 	mixer_devinfo_t *, const struct portname *);
    616  1.16.2.2  christos static int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
    617  1.16.2.2  christos static int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
    618  1.16.2.2  christos static int au_set_gain(struct audio_softc *, struct au_mixer_ports *, int, int);
    619  1.16.2.2  christos static void au_get_gain(struct audio_softc *, struct au_mixer_ports *,
    620  1.16.2.2  christos 	u_int *, u_char *);
    621  1.16.2.2  christos static int au_set_port(struct audio_softc *, struct au_mixer_ports *, u_int);
    622  1.16.2.2  christos static int au_get_port(struct audio_softc *, struct au_mixer_ports *);
    623  1.16.2.2  christos static int au_set_monitor_gain(struct audio_softc *, int);
    624  1.16.2.2  christos static int au_get_monitor_gain(struct audio_softc *);
    625  1.16.2.2  christos static int audio_get_port(struct audio_softc *, mixer_ctrl_t *);
    626  1.16.2.2  christos static int audio_set_port(struct audio_softc *, mixer_ctrl_t *);
    627  1.16.2.2  christos 
    628  1.16.2.2  christos static __inline struct audio_params
    629  1.16.2.2  christos format2_to_params(const audio_format2_t *f2)
    630  1.16.2.2  christos {
    631  1.16.2.2  christos 	audio_params_t p;
    632  1.16.2.2  christos 
    633  1.16.2.2  christos 	/* validbits/precision <-> precision/stride */
    634  1.16.2.2  christos 	p.sample_rate = f2->sample_rate;
    635  1.16.2.2  christos 	p.channels    = f2->channels;
    636  1.16.2.2  christos 	p.encoding    = f2->encoding;
    637  1.16.2.2  christos 	p.validbits   = f2->precision;
    638  1.16.2.2  christos 	p.precision   = f2->stride;
    639  1.16.2.2  christos 	return p;
    640  1.16.2.2  christos }
    641  1.16.2.2  christos 
    642  1.16.2.2  christos static __inline audio_format2_t
    643  1.16.2.2  christos params_to_format2(const struct audio_params *p)
    644  1.16.2.2  christos {
    645  1.16.2.2  christos 	audio_format2_t f2;
    646  1.16.2.2  christos 
    647  1.16.2.2  christos 	/* precision/stride <-> validbits/precision */
    648  1.16.2.2  christos 	f2.sample_rate = p->sample_rate;
    649  1.16.2.2  christos 	f2.channels    = p->channels;
    650  1.16.2.2  christos 	f2.encoding    = p->encoding;
    651  1.16.2.2  christos 	f2.precision   = p->validbits;
    652  1.16.2.2  christos 	f2.stride      = p->precision;
    653  1.16.2.2  christos 	return f2;
    654  1.16.2.2  christos }
    655  1.16.2.2  christos 
    656  1.16.2.2  christos /* Return true if this track is a playback track. */
    657  1.16.2.2  christos static __inline bool
    658  1.16.2.2  christos audio_track_is_playback(const audio_track_t *track)
    659  1.16.2.2  christos {
    660  1.16.2.2  christos 
    661  1.16.2.2  christos 	return ((track->mode & AUMODE_PLAY) != 0);
    662  1.16.2.2  christos }
    663  1.16.2.2  christos 
    664  1.16.2.2  christos /* Return true if this track is a recording track. */
    665  1.16.2.2  christos static __inline bool
    666  1.16.2.2  christos audio_track_is_record(const audio_track_t *track)
    667  1.16.2.2  christos {
    668  1.16.2.2  christos 
    669  1.16.2.2  christos 	return ((track->mode & AUMODE_RECORD) != 0);
    670  1.16.2.2  christos }
    671  1.16.2.2  christos 
    672  1.16.2.2  christos #if 0 /* XXX Not used yet */
    673  1.16.2.2  christos /*
    674  1.16.2.2  christos  * Convert 0..255 volume used in userland to internal presentation 0..256.
    675  1.16.2.2  christos  */
    676  1.16.2.2  christos static __inline u_int
    677  1.16.2.2  christos audio_volume_to_inner(u_int v)
    678  1.16.2.2  christos {
    679  1.16.2.2  christos 
    680  1.16.2.2  christos 	return v < 127 ? v : v + 1;
    681  1.16.2.2  christos }
    682  1.16.2.2  christos 
    683  1.16.2.2  christos /*
    684  1.16.2.2  christos  * Convert 0..256 internal presentation to 0..255 volume used in userland.
    685  1.16.2.2  christos  */
    686  1.16.2.2  christos static __inline u_int
    687  1.16.2.2  christos audio_volume_to_outer(u_int v)
    688  1.16.2.2  christos {
    689  1.16.2.2  christos 
    690  1.16.2.2  christos 	return v < 127 ? v : v - 1;
    691  1.16.2.2  christos }
    692  1.16.2.2  christos #endif /* 0 */
    693  1.16.2.2  christos 
    694  1.16.2.2  christos static dev_type_open(audioopen);
    695  1.16.2.2  christos /* XXXMRG use more dev_type_xxx */
    696  1.16.2.2  christos 
    697  1.16.2.2  christos const struct cdevsw audio_cdevsw = {
    698  1.16.2.2  christos 	.d_open = audioopen,
    699  1.16.2.2  christos 	.d_close = noclose,
    700  1.16.2.2  christos 	.d_read = noread,
    701  1.16.2.2  christos 	.d_write = nowrite,
    702  1.16.2.2  christos 	.d_ioctl = noioctl,
    703  1.16.2.2  christos 	.d_stop = nostop,
    704  1.16.2.2  christos 	.d_tty = notty,
    705  1.16.2.2  christos 	.d_poll = nopoll,
    706  1.16.2.2  christos 	.d_mmap = nommap,
    707  1.16.2.2  christos 	.d_kqfilter = nokqfilter,
    708  1.16.2.2  christos 	.d_discard = nodiscard,
    709  1.16.2.2  christos 	.d_flag = D_OTHER | D_MPSAFE
    710  1.16.2.2  christos };
    711  1.16.2.2  christos 
    712  1.16.2.2  christos const struct fileops audio_fileops = {
    713  1.16.2.2  christos 	.fo_name = "audio",
    714  1.16.2.2  christos 	.fo_read = audioread,
    715  1.16.2.2  christos 	.fo_write = audiowrite,
    716  1.16.2.2  christos 	.fo_ioctl = audioioctl,
    717  1.16.2.2  christos 	.fo_fcntl = fnullop_fcntl,
    718  1.16.2.2  christos 	.fo_stat = audiostat,
    719  1.16.2.2  christos 	.fo_poll = audiopoll,
    720  1.16.2.2  christos 	.fo_close = audioclose,
    721  1.16.2.2  christos 	.fo_mmap = audiommap,
    722  1.16.2.2  christos 	.fo_kqfilter = audiokqfilter,
    723  1.16.2.2  christos 	.fo_restart = fnullop_restart
    724  1.16.2.2  christos };
    725  1.16.2.2  christos 
    726  1.16.2.2  christos /* The default audio mode: 8 kHz mono mu-law */
    727  1.16.2.2  christos static const struct audio_params audio_default = {
    728  1.16.2.2  christos 	.sample_rate = 8000,
    729  1.16.2.2  christos 	.encoding = AUDIO_ENCODING_ULAW,
    730  1.16.2.2  christos 	.precision = 8,
    731  1.16.2.2  christos 	.validbits = 8,
    732  1.16.2.2  christos 	.channels = 1,
    733  1.16.2.2  christos };
    734  1.16.2.2  christos 
    735  1.16.2.2  christos static const char *encoding_names[] = {
    736  1.16.2.2  christos 	"none",
    737  1.16.2.2  christos 	AudioEmulaw,
    738  1.16.2.2  christos 	AudioEalaw,
    739  1.16.2.2  christos 	"pcm16",
    740  1.16.2.2  christos 	"pcm8",
    741  1.16.2.2  christos 	AudioEadpcm,
    742  1.16.2.2  christos 	AudioEslinear_le,
    743  1.16.2.2  christos 	AudioEslinear_be,
    744  1.16.2.2  christos 	AudioEulinear_le,
    745  1.16.2.2  christos 	AudioEulinear_be,
    746  1.16.2.2  christos 	AudioEslinear,
    747  1.16.2.2  christos 	AudioEulinear,
    748  1.16.2.2  christos 	AudioEmpeg_l1_stream,
    749  1.16.2.2  christos 	AudioEmpeg_l1_packets,
    750  1.16.2.2  christos 	AudioEmpeg_l1_system,
    751  1.16.2.2  christos 	AudioEmpeg_l2_stream,
    752  1.16.2.2  christos 	AudioEmpeg_l2_packets,
    753  1.16.2.2  christos 	AudioEmpeg_l2_system,
    754  1.16.2.2  christos 	AudioEac3,
    755  1.16.2.2  christos };
    756  1.16.2.2  christos 
    757  1.16.2.2  christos /*
    758  1.16.2.2  christos  * Returns encoding name corresponding to AUDIO_ENCODING_*.
    759  1.16.2.2  christos  * Note that it may return a local buffer because it is mainly for debugging.
    760  1.16.2.2  christos  */
    761  1.16.2.2  christos const char *
    762  1.16.2.2  christos audio_encoding_name(int encoding)
    763  1.16.2.2  christos {
    764  1.16.2.2  christos 	static char buf[16];
    765  1.16.2.2  christos 
    766  1.16.2.2  christos 	if (0 <= encoding && encoding < __arraycount(encoding_names)) {
    767  1.16.2.2  christos 		return encoding_names[encoding];
    768  1.16.2.2  christos 	} else {
    769  1.16.2.2  christos 		snprintf(buf, sizeof(buf), "enc=%d", encoding);
    770  1.16.2.2  christos 		return buf;
    771  1.16.2.2  christos 	}
    772  1.16.2.2  christos }
    773  1.16.2.2  christos 
    774  1.16.2.2  christos /*
    775  1.16.2.2  christos  * Supported encodings used by AUDIO_GETENC.
    776  1.16.2.2  christos  * index and flags are set by code.
    777  1.16.2.2  christos  * XXX is there any needs for SLINEAR_OE:>=16/ULINEAR_OE:>=16 ?
    778  1.16.2.2  christos  */
    779  1.16.2.2  christos static const audio_encoding_t audio_encodings[] = {
    780  1.16.2.2  christos 	{ 0, AudioEmulaw,	AUDIO_ENCODING_ULAW,		8,  0 },
    781  1.16.2.2  christos 	{ 0, AudioEalaw,	AUDIO_ENCODING_ALAW,		8,  0 },
    782  1.16.2.2  christos 	{ 0, AudioEslinear,	AUDIO_ENCODING_SLINEAR,		8,  0 },
    783  1.16.2.2  christos 	{ 0, AudioEulinear,	AUDIO_ENCODING_ULINEAR,		8,  0 },
    784  1.16.2.2  christos 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	16, 0 },
    785  1.16.2.2  christos 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	16, 0 },
    786  1.16.2.2  christos 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	16, 0 },
    787  1.16.2.2  christos 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	16, 0 },
    788  1.16.2.2  christos #if defined(AUDIO_SUPPORT_LINEAR24)
    789  1.16.2.2  christos 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	24, 0 },
    790  1.16.2.2  christos 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	24, 0 },
    791  1.16.2.2  christos 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	24, 0 },
    792  1.16.2.2  christos 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	24, 0 },
    793  1.16.2.2  christos #endif
    794  1.16.2.2  christos 	{ 0, AudioEslinear_le,	AUDIO_ENCODING_SLINEAR_LE,	32, 0 },
    795  1.16.2.2  christos 	{ 0, AudioEulinear_le,	AUDIO_ENCODING_ULINEAR_LE,	32, 0 },
    796  1.16.2.2  christos 	{ 0, AudioEslinear_be,	AUDIO_ENCODING_SLINEAR_BE,	32, 0 },
    797  1.16.2.2  christos 	{ 0, AudioEulinear_be,	AUDIO_ENCODING_ULINEAR_BE,	32, 0 },
    798  1.16.2.2  christos };
    799  1.16.2.2  christos 
    800  1.16.2.2  christos static const struct portname itable[] = {
    801  1.16.2.2  christos 	{ AudioNmicrophone,	AUDIO_MICROPHONE },
    802  1.16.2.2  christos 	{ AudioNline,		AUDIO_LINE_IN },
    803  1.16.2.2  christos 	{ AudioNcd,		AUDIO_CD },
    804  1.16.2.2  christos 	{ 0, 0 }
    805  1.16.2.2  christos };
    806  1.16.2.2  christos static const struct portname otable[] = {
    807  1.16.2.2  christos 	{ AudioNspeaker,	AUDIO_SPEAKER },
    808  1.16.2.2  christos 	{ AudioNheadphone,	AUDIO_HEADPHONE },
    809  1.16.2.2  christos 	{ AudioNline,		AUDIO_LINE_OUT },
    810  1.16.2.2  christos 	{ 0, 0 }
    811  1.16.2.2  christos };
    812  1.16.2.2  christos 
    813  1.16.2.3    martin static struct psref_class *audio_psref_class __read_mostly;
    814  1.16.2.3    martin 
    815  1.16.2.2  christos CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
    816  1.16.2.2  christos     audiomatch, audioattach, audiodetach, audioactivate, audiorescan,
    817  1.16.2.2  christos     audiochilddet, DVF_DETACH_SHUTDOWN);
    818  1.16.2.2  christos 
    819  1.16.2.2  christos static int
    820  1.16.2.2  christos audiomatch(device_t parent, cfdata_t match, void *aux)
    821  1.16.2.2  christos {
    822  1.16.2.2  christos 	struct audio_attach_args *sa;
    823  1.16.2.2  christos 
    824  1.16.2.2  christos 	sa = aux;
    825  1.16.2.2  christos 	DPRINTF(1, "%s: type=%d sa=%p hw=%p\n",
    826  1.16.2.2  christos 	     __func__, sa->type, sa, sa->hwif);
    827  1.16.2.2  christos 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
    828  1.16.2.2  christos }
    829  1.16.2.2  christos 
    830  1.16.2.2  christos static void
    831  1.16.2.2  christos audioattach(device_t parent, device_t self, void *aux)
    832  1.16.2.2  christos {
    833  1.16.2.2  christos 	struct audio_softc *sc;
    834  1.16.2.2  christos 	struct audio_attach_args *sa;
    835  1.16.2.2  christos 	const struct audio_hw_if *hw_if;
    836  1.16.2.2  christos 	audio_format2_t phwfmt;
    837  1.16.2.2  christos 	audio_format2_t rhwfmt;
    838  1.16.2.2  christos 	audio_filter_reg_t pfil;
    839  1.16.2.2  christos 	audio_filter_reg_t rfil;
    840  1.16.2.2  christos 	const struct sysctlnode *node;
    841  1.16.2.2  christos 	void *hdlp;
    842  1.16.2.2  christos 	bool has_playback;
    843  1.16.2.2  christos 	bool has_capture;
    844  1.16.2.2  christos 	bool has_indep;
    845  1.16.2.2  christos 	bool has_fulldup;
    846  1.16.2.2  christos 	int mode;
    847  1.16.2.2  christos 	int error;
    848  1.16.2.2  christos 
    849  1.16.2.2  christos 	sc = device_private(self);
    850  1.16.2.2  christos 	sc->sc_dev = self;
    851  1.16.2.2  christos 	sa = (struct audio_attach_args *)aux;
    852  1.16.2.2  christos 	hw_if = sa->hwif;
    853  1.16.2.2  christos 	hdlp = sa->hdl;
    854  1.16.2.2  christos 
    855  1.16.2.3    martin 	if (hw_if == NULL) {
    856  1.16.2.2  christos 		panic("audioattach: missing hw_if method");
    857  1.16.2.2  christos 	}
    858  1.16.2.3    martin 	if (hw_if->get_locks == NULL || hw_if->get_props == NULL) {
    859  1.16.2.3    martin 		aprint_error(": missing mandatory method\n");
    860  1.16.2.3    martin 		return;
    861  1.16.2.3    martin 	}
    862  1.16.2.2  christos 
    863  1.16.2.2  christos 	hw_if->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
    864  1.16.2.3    martin 	sc->sc_props = hw_if->get_props(hdlp);
    865  1.16.2.3    martin 
    866  1.16.2.3    martin 	has_playback = (sc->sc_props & AUDIO_PROP_PLAYBACK);
    867  1.16.2.3    martin 	has_capture  = (sc->sc_props & AUDIO_PROP_CAPTURE);
    868  1.16.2.3    martin 	has_indep    = (sc->sc_props & AUDIO_PROP_INDEPENDENT);
    869  1.16.2.3    martin 	has_fulldup  = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
    870  1.16.2.2  christos 
    871  1.16.2.2  christos #ifdef DIAGNOSTIC
    872  1.16.2.2  christos 	if (hw_if->query_format == NULL ||
    873  1.16.2.2  christos 	    hw_if->set_format == NULL ||
    874  1.16.2.2  christos 	    hw_if->getdev == NULL ||
    875  1.16.2.2  christos 	    hw_if->set_port == NULL ||
    876  1.16.2.2  christos 	    hw_if->get_port == NULL ||
    877  1.16.2.3    martin 	    hw_if->query_devinfo == NULL) {
    878  1.16.2.3    martin 		aprint_error(": missing mandatory method\n");
    879  1.16.2.2  christos 		return;
    880  1.16.2.2  christos 	}
    881  1.16.2.3    martin 	if (has_playback) {
    882  1.16.2.3    martin 		if ((hw_if->start_output == NULL && hw_if->trigger_output == NULL) ||
    883  1.16.2.3    martin 		    hw_if->halt_output == NULL) {
    884  1.16.2.3    martin 			aprint_error(": missing playback method\n");
    885  1.16.2.3    martin 		}
    886  1.16.2.3    martin 	}
    887  1.16.2.3    martin 	if (has_capture) {
    888  1.16.2.3    martin 		if ((hw_if->start_input == NULL && hw_if->trigger_input == NULL) ||
    889  1.16.2.3    martin 		    hw_if->halt_input == NULL) {
    890  1.16.2.3    martin 			aprint_error(": missing capture method\n");
    891  1.16.2.3    martin 		}
    892  1.16.2.3    martin 	}
    893  1.16.2.2  christos #endif
    894  1.16.2.2  christos 
    895  1.16.2.2  christos 	sc->hw_if = hw_if;
    896  1.16.2.2  christos 	sc->hw_hdl = hdlp;
    897  1.16.2.2  christos 	sc->hw_dev = parent;
    898  1.16.2.2  christos 
    899  1.16.2.3    martin 	sc->sc_exlock = 1;
    900  1.16.2.2  christos 	sc->sc_blk_ms = AUDIO_BLK_MS;
    901  1.16.2.2  christos 	SLIST_INIT(&sc->sc_files);
    902  1.16.2.2  christos 	cv_init(&sc->sc_exlockcv, "audiolk");
    903  1.16.2.3    martin 	sc->sc_am_capacity = 0;
    904  1.16.2.3    martin 	sc->sc_am_used = 0;
    905  1.16.2.3    martin 	sc->sc_am = NULL;
    906  1.16.2.2  christos 
    907  1.16.2.3    martin 	/* MMAP is now supported by upper layer.  */
    908  1.16.2.3    martin 	sc->sc_props |= AUDIO_PROP_MMAP;
    909  1.16.2.2  christos 
    910  1.16.2.2  christos 	KASSERT(has_playback || has_capture);
    911  1.16.2.2  christos 	/* Unidirectional device must have neither FULLDUP nor INDEPENDENT. */
    912  1.16.2.2  christos 	if (!has_playback || !has_capture) {
    913  1.16.2.2  christos 		KASSERT(!has_indep);
    914  1.16.2.2  christos 		KASSERT(!has_fulldup);
    915  1.16.2.2  christos 	}
    916  1.16.2.2  christos 
    917  1.16.2.2  christos 	mode = 0;
    918  1.16.2.2  christos 	if (has_playback) {
    919  1.16.2.2  christos 		aprint_normal(": playback");
    920  1.16.2.2  christos 		mode |= AUMODE_PLAY;
    921  1.16.2.2  christos 	}
    922  1.16.2.2  christos 	if (has_capture) {
    923  1.16.2.2  christos 		aprint_normal("%c capture", has_playback ? ',' : ':');
    924  1.16.2.2  christos 		mode |= AUMODE_RECORD;
    925  1.16.2.2  christos 	}
    926  1.16.2.2  christos 	if (has_playback && has_capture) {
    927  1.16.2.2  christos 		if (has_fulldup)
    928  1.16.2.2  christos 			aprint_normal(", full duplex");
    929  1.16.2.2  christos 		else
    930  1.16.2.2  christos 			aprint_normal(", half duplex");
    931  1.16.2.2  christos 
    932  1.16.2.2  christos 		if (has_indep)
    933  1.16.2.2  christos 			aprint_normal(", independent");
    934  1.16.2.2  christos 	}
    935  1.16.2.2  christos 
    936  1.16.2.2  christos 	aprint_naive("\n");
    937  1.16.2.2  christos 	aprint_normal("\n");
    938  1.16.2.2  christos 
    939  1.16.2.2  christos 	/* probe hw params */
    940  1.16.2.2  christos 	memset(&phwfmt, 0, sizeof(phwfmt));
    941  1.16.2.2  christos 	memset(&rhwfmt, 0, sizeof(rhwfmt));
    942  1.16.2.2  christos 	memset(&pfil, 0, sizeof(pfil));
    943  1.16.2.2  christos 	memset(&rfil, 0, sizeof(rfil));
    944  1.16.2.3    martin 	if (has_indep) {
    945  1.16.2.3    martin 		int perror, rerror;
    946  1.16.2.3    martin 
    947  1.16.2.3    martin 		/* On independent devices, probe separately. */
    948  1.16.2.3    martin 		perror = audio_hw_probe(sc, &phwfmt, AUMODE_PLAY);
    949  1.16.2.3    martin 		rerror = audio_hw_probe(sc, &rhwfmt, AUMODE_RECORD);
    950  1.16.2.3    martin 		if (perror && rerror) {
    951  1.16.2.3    martin 			aprint_error_dev(self, "audio_hw_probe failed, "
    952  1.16.2.3    martin 			    "perror = %d, rerror = %d\n", perror, rerror);
    953  1.16.2.3    martin 			goto bad;
    954  1.16.2.3    martin 		}
    955  1.16.2.3    martin 		if (perror) {
    956  1.16.2.3    martin 			mode &= ~AUMODE_PLAY;
    957  1.16.2.3    martin 			aprint_error_dev(self, "audio_hw_probe failed with "
    958  1.16.2.3    martin 			    "%d, playback disabled\n", perror);
    959  1.16.2.3    martin 		}
    960  1.16.2.3    martin 		if (rerror) {
    961  1.16.2.3    martin 			mode &= ~AUMODE_RECORD;
    962  1.16.2.3    martin 			aprint_error_dev(self, "audio_hw_probe failed with "
    963  1.16.2.3    martin 			    "%d, capture disabled\n", rerror);
    964  1.16.2.3    martin 		}
    965  1.16.2.3    martin 	} else {
    966  1.16.2.3    martin 		/*
    967  1.16.2.3    martin 		 * On non independent devices or uni-directional devices,
    968  1.16.2.3    martin 		 * probe once (simultaneously).
    969  1.16.2.3    martin 		 */
    970  1.16.2.3    martin 		audio_format2_t *fmt = has_playback ? &phwfmt : &rhwfmt;
    971  1.16.2.3    martin 		error = audio_hw_probe(sc, fmt, mode);
    972  1.16.2.3    martin 		if (error) {
    973  1.16.2.3    martin 			aprint_error_dev(self, "audio_hw_probe failed, "
    974  1.16.2.3    martin 			    "error = %d\n", error);
    975  1.16.2.3    martin 			goto bad;
    976  1.16.2.3    martin 		}
    977  1.16.2.3    martin 		if (has_playback && has_capture)
    978  1.16.2.3    martin 			rhwfmt = phwfmt;
    979  1.16.2.2  christos 	}
    980  1.16.2.3    martin 
    981  1.16.2.2  christos 	/* Init hardware. */
    982  1.16.2.2  christos 	/* hw_probe() also validates [pr]hwfmt.  */
    983  1.16.2.2  christos 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
    984  1.16.2.2  christos 	if (error) {
    985  1.16.2.2  christos 		aprint_error_dev(self, "audio_hw_set_format failed, "
    986  1.16.2.2  christos 		    "error = %d\n", error);
    987  1.16.2.2  christos 		goto bad;
    988  1.16.2.2  christos 	}
    989  1.16.2.2  christos 
    990  1.16.2.2  christos 	/*
    991  1.16.2.2  christos 	 * Init track mixers.  If at least one direction is available on
    992  1.16.2.2  christos 	 * attach time, we assume a success.
    993  1.16.2.2  christos 	 */
    994  1.16.2.2  christos 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
    995  1.16.2.2  christos 	if (sc->sc_pmixer == NULL && sc->sc_rmixer == NULL) {
    996  1.16.2.2  christos 		aprint_error_dev(self, "audio_mixers_init failed, "
    997  1.16.2.2  christos 		    "error = %d\n", error);
    998  1.16.2.2  christos 		goto bad;
    999  1.16.2.2  christos 	}
   1000  1.16.2.2  christos 
   1001  1.16.2.3    martin 	sc->sc_psz = pserialize_create();
   1002  1.16.2.3    martin 	psref_target_init(&sc->sc_psref, audio_psref_class);
   1003  1.16.2.3    martin 
   1004  1.16.2.2  christos 	selinit(&sc->sc_wsel);
   1005  1.16.2.2  christos 	selinit(&sc->sc_rsel);
   1006  1.16.2.2  christos 
   1007  1.16.2.2  christos 	/* Initial parameter of /dev/sound */
   1008  1.16.2.2  christos 	sc->sc_sound_pparams = params_to_format2(&audio_default);
   1009  1.16.2.2  christos 	sc->sc_sound_rparams = params_to_format2(&audio_default);
   1010  1.16.2.2  christos 	sc->sc_sound_ppause = false;
   1011  1.16.2.2  christos 	sc->sc_sound_rpause = false;
   1012  1.16.2.2  christos 
   1013  1.16.2.2  christos 	/* XXX TODO: consider about sc_ai */
   1014  1.16.2.2  christos 
   1015  1.16.2.2  christos 	mixer_init(sc);
   1016  1.16.2.2  christos 	TRACE(2, "inputs ports=0x%x, input master=%d, "
   1017  1.16.2.2  christos 	    "output ports=0x%x, output master=%d",
   1018  1.16.2.2  christos 	    sc->sc_inports.allports, sc->sc_inports.master,
   1019  1.16.2.2  christos 	    sc->sc_outports.allports, sc->sc_outports.master);
   1020  1.16.2.2  christos 
   1021  1.16.2.2  christos 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
   1022  1.16.2.2  christos 	    0,
   1023  1.16.2.2  christos 	    CTLTYPE_NODE, device_xname(sc->sc_dev),
   1024  1.16.2.2  christos 	    SYSCTL_DESCR("audio test"),
   1025  1.16.2.2  christos 	    NULL, 0,
   1026  1.16.2.2  christos 	    NULL, 0,
   1027  1.16.2.2  christos 	    CTL_HW,
   1028  1.16.2.2  christos 	    CTL_CREATE, CTL_EOL);
   1029  1.16.2.2  christos 
   1030  1.16.2.2  christos 	if (node != NULL) {
   1031  1.16.2.2  christos 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1032  1.16.2.2  christos 		    CTLFLAG_READWRITE,
   1033  1.16.2.2  christos 		    CTLTYPE_INT, "blk_ms",
   1034  1.16.2.2  christos 		    SYSCTL_DESCR("blocksize in msec"),
   1035  1.16.2.2  christos 		    audio_sysctl_blk_ms, 0, (void *)sc, 0,
   1036  1.16.2.2  christos 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1037  1.16.2.2  christos 
   1038  1.16.2.2  christos 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1039  1.16.2.2  christos 		    CTLFLAG_READWRITE,
   1040  1.16.2.2  christos 		    CTLTYPE_BOOL, "multiuser",
   1041  1.16.2.2  christos 		    SYSCTL_DESCR("allow multiple user access"),
   1042  1.16.2.2  christos 		    audio_sysctl_multiuser, 0, (void *)sc, 0,
   1043  1.16.2.2  christos 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1044  1.16.2.2  christos 
   1045  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   1046  1.16.2.2  christos 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
   1047  1.16.2.2  christos 		    CTLFLAG_READWRITE,
   1048  1.16.2.2  christos 		    CTLTYPE_INT, "debug",
   1049  1.16.2.2  christos 		    SYSCTL_DESCR("debug level (0..4)"),
   1050  1.16.2.2  christos 		    audio_sysctl_debug, 0, (void *)sc, 0,
   1051  1.16.2.2  christos 		    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1052  1.16.2.2  christos #endif
   1053  1.16.2.2  christos 	}
   1054  1.16.2.2  christos 
   1055  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   1056  1.16.2.2  christos 	callout_init(&sc->sc_idle_counter, 0);
   1057  1.16.2.2  christos 	callout_setfunc(&sc->sc_idle_counter, audio_idle, self);
   1058  1.16.2.2  christos #endif
   1059  1.16.2.2  christos 
   1060  1.16.2.2  christos 	if (!pmf_device_register(self, audio_suspend, audio_resume))
   1061  1.16.2.2  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
   1062  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   1063  1.16.2.2  christos 	if (!device_active_register(self, audio_activity))
   1064  1.16.2.2  christos 		aprint_error_dev(self, "couldn't register activity handler\n");
   1065  1.16.2.2  christos #endif
   1066  1.16.2.2  christos 
   1067  1.16.2.2  christos 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
   1068  1.16.2.2  christos 	    audio_volume_down, true))
   1069  1.16.2.2  christos 		aprint_error_dev(self, "couldn't add volume down handler\n");
   1070  1.16.2.2  christos 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
   1071  1.16.2.2  christos 	    audio_volume_up, true))
   1072  1.16.2.2  christos 		aprint_error_dev(self, "couldn't add volume up handler\n");
   1073  1.16.2.2  christos 	if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE,
   1074  1.16.2.2  christos 	    audio_volume_toggle, true))
   1075  1.16.2.2  christos 		aprint_error_dev(self, "couldn't add volume toggle handler\n");
   1076  1.16.2.2  christos 
   1077  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   1078  1.16.2.2  christos 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   1079  1.16.2.2  christos #endif
   1080  1.16.2.2  christos 
   1081  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   1082  1.16.2.2  christos 	audio_mlog_init();
   1083  1.16.2.2  christos #endif
   1084  1.16.2.2  christos 
   1085  1.16.2.2  christos 	audiorescan(self, "audio", NULL);
   1086  1.16.2.3    martin 	sc->sc_exlock = 0;
   1087  1.16.2.2  christos 	return;
   1088  1.16.2.2  christos 
   1089  1.16.2.2  christos bad:
   1090  1.16.2.2  christos 	/* Clearing hw_if means that device is attached but disabled. */
   1091  1.16.2.2  christos 	sc->hw_if = NULL;
   1092  1.16.2.3    martin 	sc->sc_exlock = 0;
   1093  1.16.2.2  christos 	aprint_error_dev(sc->sc_dev, "disabled\n");
   1094  1.16.2.2  christos 	return;
   1095  1.16.2.2  christos }
   1096  1.16.2.2  christos 
   1097  1.16.2.2  christos /*
   1098  1.16.2.2  christos  * Initialize hardware mixer.
   1099  1.16.2.2  christos  * This function is called from audioattach().
   1100  1.16.2.2  christos  */
   1101  1.16.2.2  christos static void
   1102  1.16.2.2  christos mixer_init(struct audio_softc *sc)
   1103  1.16.2.2  christos {
   1104  1.16.2.2  christos 	mixer_devinfo_t mi;
   1105  1.16.2.2  christos 	int iclass, mclass, oclass, rclass;
   1106  1.16.2.2  christos 	int record_master_found, record_source_found;
   1107  1.16.2.2  christos 
   1108  1.16.2.2  christos 	iclass = mclass = oclass = rclass = -1;
   1109  1.16.2.2  christos 	sc->sc_inports.index = -1;
   1110  1.16.2.2  christos 	sc->sc_inports.master = -1;
   1111  1.16.2.2  christos 	sc->sc_inports.nports = 0;
   1112  1.16.2.2  christos 	sc->sc_inports.isenum = false;
   1113  1.16.2.2  christos 	sc->sc_inports.allports = 0;
   1114  1.16.2.2  christos 	sc->sc_inports.isdual = false;
   1115  1.16.2.2  christos 	sc->sc_inports.mixerout = -1;
   1116  1.16.2.2  christos 	sc->sc_inports.cur_port = -1;
   1117  1.16.2.2  christos 	sc->sc_outports.index = -1;
   1118  1.16.2.2  christos 	sc->sc_outports.master = -1;
   1119  1.16.2.2  christos 	sc->sc_outports.nports = 0;
   1120  1.16.2.2  christos 	sc->sc_outports.isenum = false;
   1121  1.16.2.2  christos 	sc->sc_outports.allports = 0;
   1122  1.16.2.2  christos 	sc->sc_outports.isdual = false;
   1123  1.16.2.2  christos 	sc->sc_outports.mixerout = -1;
   1124  1.16.2.2  christos 	sc->sc_outports.cur_port = -1;
   1125  1.16.2.2  christos 	sc->sc_monitor_port = -1;
   1126  1.16.2.2  christos 	/*
   1127  1.16.2.2  christos 	 * Read through the underlying driver's list, picking out the class
   1128  1.16.2.2  christos 	 * names from the mixer descriptions. We'll need them to decode the
   1129  1.16.2.2  christos 	 * mixer descriptions on the next pass through the loop.
   1130  1.16.2.2  christos 	 */
   1131  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   1132  1.16.2.2  christos 	for(mi.index = 0; ; mi.index++) {
   1133  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) != 0)
   1134  1.16.2.2  christos 			break;
   1135  1.16.2.2  christos 		 /*
   1136  1.16.2.2  christos 		  * The type of AUDIO_MIXER_CLASS merely introduces a class.
   1137  1.16.2.2  christos 		  * All the other types describe an actual mixer.
   1138  1.16.2.2  christos 		  */
   1139  1.16.2.2  christos 		if (mi.type == AUDIO_MIXER_CLASS) {
   1140  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioCinputs) == 0)
   1141  1.16.2.2  christos 				iclass = mi.mixer_class;
   1142  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioCmonitor) == 0)
   1143  1.16.2.2  christos 				mclass = mi.mixer_class;
   1144  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioCoutputs) == 0)
   1145  1.16.2.2  christos 				oclass = mi.mixer_class;
   1146  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioCrecord) == 0)
   1147  1.16.2.2  christos 				rclass = mi.mixer_class;
   1148  1.16.2.2  christos 		}
   1149  1.16.2.2  christos 	}
   1150  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   1151  1.16.2.2  christos 
   1152  1.16.2.2  christos 	/* Allocate save area.  Ensure non-zero allocation. */
   1153  1.16.2.2  christos 	sc->sc_nmixer_states = mi.index;
   1154  1.16.2.2  christos 	sc->sc_mixer_state = kmem_zalloc(sizeof(mixer_ctrl_t) *
   1155  1.16.2.2  christos 	    (sc->sc_nmixer_states + 1), KM_SLEEP);
   1156  1.16.2.2  christos 
   1157  1.16.2.2  christos 	/*
   1158  1.16.2.2  christos 	 * This is where we assign each control in the "audio" model, to the
   1159  1.16.2.2  christos 	 * underlying "mixer" control.  We walk through the whole list once,
   1160  1.16.2.2  christos 	 * assigning likely candidates as we come across them.
   1161  1.16.2.2  christos 	 */
   1162  1.16.2.2  christos 	record_master_found = 0;
   1163  1.16.2.2  christos 	record_source_found = 0;
   1164  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   1165  1.16.2.2  christos 	for(mi.index = 0; ; mi.index++) {
   1166  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) != 0)
   1167  1.16.2.2  christos 			break;
   1168  1.16.2.2  christos 		KASSERT(mi.index < sc->sc_nmixer_states);
   1169  1.16.2.2  christos 		if (mi.type == AUDIO_MIXER_CLASS)
   1170  1.16.2.2  christos 			continue;
   1171  1.16.2.2  christos 		if (mi.mixer_class == iclass) {
   1172  1.16.2.2  christos 			/*
   1173  1.16.2.2  christos 			 * AudioCinputs is only a fallback, when we don't
   1174  1.16.2.2  christos 			 * find what we're looking for in AudioCrecord, so
   1175  1.16.2.2  christos 			 * check the flags before accepting one of these.
   1176  1.16.2.2  christos 			 */
   1177  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNmaster) == 0
   1178  1.16.2.2  christos 			    && record_master_found == 0)
   1179  1.16.2.2  christos 				sc->sc_inports.master = mi.index;
   1180  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNsource) == 0
   1181  1.16.2.2  christos 			    && record_source_found == 0) {
   1182  1.16.2.2  christos 				if (mi.type == AUDIO_MIXER_ENUM) {
   1183  1.16.2.2  christos 				    int i;
   1184  1.16.2.2  christos 				    for(i = 0; i < mi.un.e.num_mem; i++)
   1185  1.16.2.2  christos 					if (strcmp(mi.un.e.member[i].label.name,
   1186  1.16.2.2  christos 						    AudioNmixerout) == 0)
   1187  1.16.2.2  christos 						sc->sc_inports.mixerout =
   1188  1.16.2.2  christos 						    mi.un.e.member[i].ord;
   1189  1.16.2.2  christos 				}
   1190  1.16.2.2  christos 				au_setup_ports(sc, &sc->sc_inports, &mi,
   1191  1.16.2.2  christos 				    itable);
   1192  1.16.2.2  christos 			}
   1193  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNdac) == 0 &&
   1194  1.16.2.2  christos 			    sc->sc_outports.master == -1)
   1195  1.16.2.2  christos 				sc->sc_outports.master = mi.index;
   1196  1.16.2.2  christos 		} else if (mi.mixer_class == mclass) {
   1197  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNmonitor) == 0)
   1198  1.16.2.2  christos 				sc->sc_monitor_port = mi.index;
   1199  1.16.2.2  christos 		} else if (mi.mixer_class == oclass) {
   1200  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNmaster) == 0)
   1201  1.16.2.2  christos 				sc->sc_outports.master = mi.index;
   1202  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNselect) == 0)
   1203  1.16.2.2  christos 				au_setup_ports(sc, &sc->sc_outports, &mi,
   1204  1.16.2.2  christos 				    otable);
   1205  1.16.2.2  christos 		} else if (mi.mixer_class == rclass) {
   1206  1.16.2.2  christos 			/*
   1207  1.16.2.2  christos 			 * These are the preferred mixers for the audio record
   1208  1.16.2.2  christos 			 * controls, so set the flags here, but don't check.
   1209  1.16.2.2  christos 			 */
   1210  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNmaster) == 0) {
   1211  1.16.2.2  christos 				sc->sc_inports.master = mi.index;
   1212  1.16.2.2  christos 				record_master_found = 1;
   1213  1.16.2.2  christos 			}
   1214  1.16.2.2  christos #if 1	/* Deprecated. Use AudioNmaster. */
   1215  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNrecord) == 0) {
   1216  1.16.2.2  christos 				sc->sc_inports.master = mi.index;
   1217  1.16.2.2  christos 				record_master_found = 1;
   1218  1.16.2.2  christos 			}
   1219  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNvolume) == 0) {
   1220  1.16.2.2  christos 				sc->sc_inports.master = mi.index;
   1221  1.16.2.2  christos 				record_master_found = 1;
   1222  1.16.2.2  christos 			}
   1223  1.16.2.2  christos #endif
   1224  1.16.2.2  christos 			if (strcmp(mi.label.name, AudioNsource) == 0) {
   1225  1.16.2.2  christos 				if (mi.type == AUDIO_MIXER_ENUM) {
   1226  1.16.2.2  christos 				    int i;
   1227  1.16.2.2  christos 				    for(i = 0; i < mi.un.e.num_mem; i++)
   1228  1.16.2.2  christos 					if (strcmp(mi.un.e.member[i].label.name,
   1229  1.16.2.2  christos 						    AudioNmixerout) == 0)
   1230  1.16.2.2  christos 						sc->sc_inports.mixerout =
   1231  1.16.2.2  christos 						    mi.un.e.member[i].ord;
   1232  1.16.2.2  christos 				}
   1233  1.16.2.2  christos 				au_setup_ports(sc, &sc->sc_inports, &mi,
   1234  1.16.2.2  christos 				    itable);
   1235  1.16.2.2  christos 				record_source_found = 1;
   1236  1.16.2.2  christos 			}
   1237  1.16.2.2  christos 		}
   1238  1.16.2.2  christos 	}
   1239  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   1240  1.16.2.2  christos }
   1241  1.16.2.2  christos 
   1242  1.16.2.2  christos static int
   1243  1.16.2.2  christos audioactivate(device_t self, enum devact act)
   1244  1.16.2.2  christos {
   1245  1.16.2.2  christos 	struct audio_softc *sc = device_private(self);
   1246  1.16.2.2  christos 
   1247  1.16.2.2  christos 	switch (act) {
   1248  1.16.2.2  christos 	case DVACT_DEACTIVATE:
   1249  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   1250  1.16.2.2  christos 		sc->sc_dying = true;
   1251  1.16.2.2  christos 		cv_broadcast(&sc->sc_exlockcv);
   1252  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   1253  1.16.2.2  christos 		return 0;
   1254  1.16.2.2  christos 	default:
   1255  1.16.2.2  christos 		return EOPNOTSUPP;
   1256  1.16.2.2  christos 	}
   1257  1.16.2.2  christos }
   1258  1.16.2.2  christos 
   1259  1.16.2.2  christos static int
   1260  1.16.2.2  christos audiodetach(device_t self, int flags)
   1261  1.16.2.2  christos {
   1262  1.16.2.2  christos 	struct audio_softc *sc;
   1263  1.16.2.3    martin 	struct audio_file *file;
   1264  1.16.2.2  christos 	int error;
   1265  1.16.2.2  christos 
   1266  1.16.2.2  christos 	sc = device_private(self);
   1267  1.16.2.2  christos 	TRACE(2, "flags=%d", flags);
   1268  1.16.2.2  christos 
   1269  1.16.2.2  christos 	/* device is not initialized */
   1270  1.16.2.2  christos 	if (sc->hw_if == NULL)
   1271  1.16.2.2  christos 		return 0;
   1272  1.16.2.2  christos 
   1273  1.16.2.2  christos 	/* Start draining existing accessors of the device. */
   1274  1.16.2.2  christos 	error = config_detach_children(self, flags);
   1275  1.16.2.2  christos 	if (error)
   1276  1.16.2.2  christos 		return error;
   1277  1.16.2.2  christos 
   1278  1.16.2.3    martin 	/* delete sysctl nodes */
   1279  1.16.2.3    martin 	sysctl_teardown(&sc->sc_log);
   1280  1.16.2.3    martin 
   1281  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   1282  1.16.2.2  christos 	sc->sc_dying = true;
   1283  1.16.2.2  christos 	cv_broadcast(&sc->sc_exlockcv);
   1284  1.16.2.2  christos 	if (sc->sc_pmixer)
   1285  1.16.2.2  christos 		cv_broadcast(&sc->sc_pmixer->outcv);
   1286  1.16.2.2  christos 	if (sc->sc_rmixer)
   1287  1.16.2.2  christos 		cv_broadcast(&sc->sc_rmixer->outcv);
   1288  1.16.2.3    martin 
   1289  1.16.2.3    martin 	/* Prevent new users */
   1290  1.16.2.3    martin 	SLIST_FOREACH(file, &sc->sc_files, entry) {
   1291  1.16.2.3    martin 		atomic_store_relaxed(&file->dying, true);
   1292  1.16.2.3    martin 	}
   1293  1.16.2.3    martin 
   1294  1.16.2.3    martin 	/*
   1295  1.16.2.3    martin 	 * Wait for existing users to drain.
   1296  1.16.2.3    martin 	 * - pserialize_perform waits for all pserialize_read sections on
   1297  1.16.2.3    martin 	 *   all CPUs; after this, no more new psref_acquire can happen.
   1298  1.16.2.3    martin 	 * - psref_target_destroy waits for all extant acquired psrefs to
   1299  1.16.2.3    martin 	 *   be psref_released.
   1300  1.16.2.3    martin 	 */
   1301  1.16.2.3    martin 	pserialize_perform(sc->sc_psz);
   1302  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   1303  1.16.2.3    martin 	psref_target_destroy(&sc->sc_psref, audio_psref_class);
   1304  1.16.2.2  christos 
   1305  1.16.2.3    martin 	/*
   1306  1.16.2.3    martin 	 * We are now guaranteed that there are no calls to audio fileops
   1307  1.16.2.3    martin 	 * that hold sc, and any new calls with files that were for sc will
   1308  1.16.2.3    martin 	 * fail.  Thus, we now have exclusive access to the softc.
   1309  1.16.2.3    martin 	 */
   1310  1.16.2.3    martin 	sc->sc_exlock = 1;
   1311  1.16.2.2  christos 
   1312  1.16.2.2  christos 	/*
   1313  1.16.2.3    martin 	 * Nuke all open instances.
   1314  1.16.2.3    martin 	 * Here, we no longer need any locks to traverse sc_files.
   1315  1.16.2.2  christos 	 */
   1316  1.16.2.3    martin 	while ((file = SLIST_FIRST(&sc->sc_files)) != NULL) {
   1317  1.16.2.3    martin 		audio_unlink(sc, file);
   1318  1.16.2.3    martin 	}
   1319  1.16.2.2  christos 
   1320  1.16.2.2  christos 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN,
   1321  1.16.2.2  christos 	    audio_volume_down, true);
   1322  1.16.2.2  christos 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP,
   1323  1.16.2.2  christos 	    audio_volume_up, true);
   1324  1.16.2.2  christos 	pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE,
   1325  1.16.2.2  christos 	    audio_volume_toggle, true);
   1326  1.16.2.2  christos 
   1327  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   1328  1.16.2.2  christos 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   1329  1.16.2.2  christos 
   1330  1.16.2.2  christos 	device_active_deregister(self, audio_activity);
   1331  1.16.2.2  christos #endif
   1332  1.16.2.2  christos 
   1333  1.16.2.2  christos 	pmf_device_deregister(self);
   1334  1.16.2.2  christos 
   1335  1.16.2.2  christos 	/* Free resources */
   1336  1.16.2.2  christos 	if (sc->sc_pmixer) {
   1337  1.16.2.2  christos 		audio_mixer_destroy(sc, sc->sc_pmixer);
   1338  1.16.2.2  christos 		kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
   1339  1.16.2.2  christos 	}
   1340  1.16.2.2  christos 	if (sc->sc_rmixer) {
   1341  1.16.2.2  christos 		audio_mixer_destroy(sc, sc->sc_rmixer);
   1342  1.16.2.2  christos 		kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
   1343  1.16.2.2  christos 	}
   1344  1.16.2.3    martin 	if (sc->sc_am)
   1345  1.16.2.3    martin 		kern_free(sc->sc_am);
   1346  1.16.2.2  christos 
   1347  1.16.2.2  christos 	seldestroy(&sc->sc_wsel);
   1348  1.16.2.2  christos 	seldestroy(&sc->sc_rsel);
   1349  1.16.2.2  christos 
   1350  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   1351  1.16.2.2  christos 	callout_destroy(&sc->sc_idle_counter);
   1352  1.16.2.2  christos #endif
   1353  1.16.2.2  christos 
   1354  1.16.2.2  christos 	cv_destroy(&sc->sc_exlockcv);
   1355  1.16.2.2  christos 
   1356  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   1357  1.16.2.2  christos 	audio_mlog_free();
   1358  1.16.2.2  christos #endif
   1359  1.16.2.2  christos 
   1360  1.16.2.2  christos 	return 0;
   1361  1.16.2.2  christos }
   1362  1.16.2.2  christos 
   1363  1.16.2.2  christos static void
   1364  1.16.2.2  christos audiochilddet(device_t self, device_t child)
   1365  1.16.2.2  christos {
   1366  1.16.2.2  christos 
   1367  1.16.2.2  christos 	/* we hold no child references, so do nothing */
   1368  1.16.2.2  christos }
   1369  1.16.2.2  christos 
   1370  1.16.2.2  christos static int
   1371  1.16.2.2  christos audiosearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
   1372  1.16.2.2  christos {
   1373  1.16.2.2  christos 
   1374  1.16.2.2  christos 	if (config_match(parent, cf, aux))
   1375  1.16.2.2  christos 		config_attach_loc(parent, cf, locs, aux, NULL);
   1376  1.16.2.2  christos 
   1377  1.16.2.2  christos 	return 0;
   1378  1.16.2.2  christos }
   1379  1.16.2.2  christos 
   1380  1.16.2.2  christos static int
   1381  1.16.2.2  christos audiorescan(device_t self, const char *ifattr, const int *flags)
   1382  1.16.2.2  christos {
   1383  1.16.2.2  christos 	struct audio_softc *sc = device_private(self);
   1384  1.16.2.2  christos 
   1385  1.16.2.2  christos 	if (!ifattr_match(ifattr, "audio"))
   1386  1.16.2.2  christos 		return 0;
   1387  1.16.2.2  christos 
   1388  1.16.2.2  christos 	config_search_loc(audiosearch, sc->sc_dev, "audio", NULL, NULL);
   1389  1.16.2.2  christos 
   1390  1.16.2.2  christos 	return 0;
   1391  1.16.2.2  christos }
   1392  1.16.2.2  christos 
   1393  1.16.2.2  christos /*
   1394  1.16.2.2  christos  * Called from hardware driver.  This is where the MI audio driver gets
   1395  1.16.2.2  christos  * probed/attached to the hardware driver.
   1396  1.16.2.2  christos  */
   1397  1.16.2.2  christos device_t
   1398  1.16.2.2  christos audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev)
   1399  1.16.2.2  christos {
   1400  1.16.2.2  christos 	struct audio_attach_args arg;
   1401  1.16.2.2  christos 
   1402  1.16.2.2  christos #ifdef DIAGNOSTIC
   1403  1.16.2.2  christos 	if (ahwp == NULL) {
   1404  1.16.2.2  christos 		aprint_error("audio_attach_mi: NULL\n");
   1405  1.16.2.2  christos 		return 0;
   1406  1.16.2.2  christos 	}
   1407  1.16.2.2  christos #endif
   1408  1.16.2.2  christos 	arg.type = AUDIODEV_TYPE_AUDIO;
   1409  1.16.2.2  christos 	arg.hwif = ahwp;
   1410  1.16.2.2  christos 	arg.hdl = hdlp;
   1411  1.16.2.2  christos 	return config_found(dev, &arg, audioprint);
   1412  1.16.2.2  christos }
   1413  1.16.2.2  christos 
   1414  1.16.2.2  christos /*
   1415  1.16.2.3    martin  * Enter critical section and also keep sc_lock.
   1416  1.16.2.3    martin  * If successful, returns 0 with sc_lock held.  Otherwise returns errno.
   1417  1.16.2.3    martin  * Must be called without sc_lock held.
   1418  1.16.2.2  christos  */
   1419  1.16.2.2  christos static int
   1420  1.16.2.3    martin audio_exlock_mutex_enter(struct audio_softc *sc)
   1421  1.16.2.2  christos {
   1422  1.16.2.2  christos 	int error;
   1423  1.16.2.2  christos 
   1424  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   1425  1.16.2.2  christos 	if (sc->sc_dying) {
   1426  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   1427  1.16.2.2  christos 		return EIO;
   1428  1.16.2.2  christos 	}
   1429  1.16.2.2  christos 
   1430  1.16.2.2  christos 	while (__predict_false(sc->sc_exlock != 0)) {
   1431  1.16.2.2  christos 		error = cv_wait_sig(&sc->sc_exlockcv, sc->sc_lock);
   1432  1.16.2.2  christos 		if (sc->sc_dying)
   1433  1.16.2.2  christos 			error = EIO;
   1434  1.16.2.2  christos 		if (error) {
   1435  1.16.2.2  christos 			mutex_exit(sc->sc_lock);
   1436  1.16.2.2  christos 			return error;
   1437  1.16.2.2  christos 		}
   1438  1.16.2.2  christos 	}
   1439  1.16.2.2  christos 
   1440  1.16.2.2  christos 	/* Acquire */
   1441  1.16.2.2  christos 	sc->sc_exlock = 1;
   1442  1.16.2.2  christos 	return 0;
   1443  1.16.2.2  christos }
   1444  1.16.2.2  christos 
   1445  1.16.2.2  christos /*
   1446  1.16.2.3    martin  * Exit critical section and exit sc_lock.
   1447  1.16.2.2  christos  * Must be called with sc_lock held.
   1448  1.16.2.2  christos  */
   1449  1.16.2.2  christos static void
   1450  1.16.2.3    martin audio_exlock_mutex_exit(struct audio_softc *sc)
   1451  1.16.2.2  christos {
   1452  1.16.2.2  christos 
   1453  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   1454  1.16.2.2  christos 
   1455  1.16.2.2  christos 	sc->sc_exlock = 0;
   1456  1.16.2.2  christos 	cv_broadcast(&sc->sc_exlockcv);
   1457  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   1458  1.16.2.2  christos }
   1459  1.16.2.2  christos 
   1460  1.16.2.2  christos /*
   1461  1.16.2.3    martin  * Enter critical section.
   1462  1.16.2.3    martin  * If successful, it returns 0.  Otherwise returns errno.
   1463  1.16.2.3    martin  * Must be called without sc_lock held.
   1464  1.16.2.3    martin  * This function returns without sc_lock held.
   1465  1.16.2.3    martin  */
   1466  1.16.2.3    martin static int
   1467  1.16.2.3    martin audio_exlock_enter(struct audio_softc *sc)
   1468  1.16.2.3    martin {
   1469  1.16.2.3    martin 	int error;
   1470  1.16.2.3    martin 
   1471  1.16.2.3    martin 	error = audio_exlock_mutex_enter(sc);
   1472  1.16.2.3    martin 	if (error)
   1473  1.16.2.3    martin 		return error;
   1474  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   1475  1.16.2.3    martin 	return 0;
   1476  1.16.2.3    martin }
   1477  1.16.2.3    martin 
   1478  1.16.2.3    martin /*
   1479  1.16.2.3    martin  * Exit critical section.
   1480  1.16.2.3    martin  * Must be called without sc_lock held.
   1481  1.16.2.3    martin  */
   1482  1.16.2.3    martin static void
   1483  1.16.2.3    martin audio_exlock_exit(struct audio_softc *sc)
   1484  1.16.2.3    martin {
   1485  1.16.2.3    martin 
   1486  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   1487  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   1488  1.16.2.3    martin }
   1489  1.16.2.3    martin 
   1490  1.16.2.3    martin /*
   1491  1.16.2.3    martin  * Acquire sc from file, and increment the psref count.
   1492  1.16.2.3    martin  * If successful, returns sc.  Otherwise returns NULL.
   1493  1.16.2.3    martin  */
   1494  1.16.2.3    martin struct audio_softc *
   1495  1.16.2.3    martin audio_file_enter(audio_file_t *file, struct psref *refp)
   1496  1.16.2.3    martin {
   1497  1.16.2.3    martin 	int s;
   1498  1.16.2.3    martin 	bool dying;
   1499  1.16.2.3    martin 
   1500  1.16.2.3    martin 	/* psref(9) forbids to migrate CPUs */
   1501  1.16.2.3    martin 	curlwp_bind();
   1502  1.16.2.3    martin 
   1503  1.16.2.3    martin 	/* Block audiodetach while we acquire a reference */
   1504  1.16.2.3    martin 	s = pserialize_read_enter();
   1505  1.16.2.3    martin 
   1506  1.16.2.3    martin 	/* If close or audiodetach already ran, tough -- no more audio */
   1507  1.16.2.3    martin 	dying = atomic_load_relaxed(&file->dying);
   1508  1.16.2.3    martin 	if (dying) {
   1509  1.16.2.3    martin 		pserialize_read_exit(s);
   1510  1.16.2.3    martin 		return NULL;
   1511  1.16.2.3    martin 	}
   1512  1.16.2.3    martin 
   1513  1.16.2.3    martin 	/* Acquire a reference */
   1514  1.16.2.3    martin 	psref_acquire(refp, &file->sc->sc_psref, audio_psref_class);
   1515  1.16.2.3    martin 
   1516  1.16.2.3    martin 	/* Now sc won't go away until we drop the reference count */
   1517  1.16.2.3    martin 	pserialize_read_exit(s);
   1518  1.16.2.3    martin 
   1519  1.16.2.3    martin 	return file->sc;
   1520  1.16.2.3    martin }
   1521  1.16.2.3    martin 
   1522  1.16.2.3    martin /*
   1523  1.16.2.3    martin  * Decrement the psref count.
   1524  1.16.2.3    martin  */
   1525  1.16.2.3    martin void
   1526  1.16.2.3    martin audio_file_exit(struct audio_softc *sc, struct psref *refp)
   1527  1.16.2.3    martin {
   1528  1.16.2.3    martin 
   1529  1.16.2.3    martin 	psref_release(refp, &sc->sc_psref, audio_psref_class);
   1530  1.16.2.3    martin }
   1531  1.16.2.3    martin 
   1532  1.16.2.3    martin /*
   1533  1.16.2.2  christos  * Wait for I/O to complete, releasing sc_lock.
   1534  1.16.2.2  christos  * Must be called with sc_lock held.
   1535  1.16.2.2  christos  */
   1536  1.16.2.2  christos static int
   1537  1.16.2.2  christos audio_track_waitio(struct audio_softc *sc, audio_track_t *track)
   1538  1.16.2.2  christos {
   1539  1.16.2.2  christos 	int error;
   1540  1.16.2.2  christos 
   1541  1.16.2.2  christos 	KASSERT(track);
   1542  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   1543  1.16.2.2  christos 
   1544  1.16.2.2  christos 	/* Wait for pending I/O to complete. */
   1545  1.16.2.2  christos 	error = cv_timedwait_sig(&track->mixer->outcv, sc->sc_lock,
   1546  1.16.2.2  christos 	    mstohz(AUDIO_TIMEOUT));
   1547  1.16.2.2  christos 	if (sc->sc_dying) {
   1548  1.16.2.2  christos 		error = EIO;
   1549  1.16.2.2  christos 	}
   1550  1.16.2.2  christos 	if (error) {
   1551  1.16.2.2  christos 		TRACET(2, track, "cv_timedwait_sig failed %d", error);
   1552  1.16.2.2  christos 		if (error == EWOULDBLOCK)
   1553  1.16.2.2  christos 			device_printf(sc->sc_dev, "device timeout\n");
   1554  1.16.2.2  christos 	} else {
   1555  1.16.2.2  christos 		TRACET(3, track, "wakeup");
   1556  1.16.2.2  christos 	}
   1557  1.16.2.2  christos 	return error;
   1558  1.16.2.2  christos }
   1559  1.16.2.2  christos 
   1560  1.16.2.2  christos /*
   1561  1.16.2.2  christos  * Try to acquire track lock.
   1562  1.16.2.2  christos  * It doesn't block if the track lock is already aquired.
   1563  1.16.2.2  christos  * Returns true if the track lock was acquired, or false if the track
   1564  1.16.2.2  christos  * lock was already acquired.
   1565  1.16.2.2  christos  */
   1566  1.16.2.2  christos static __inline bool
   1567  1.16.2.2  christos audio_track_lock_tryenter(audio_track_t *track)
   1568  1.16.2.2  christos {
   1569  1.16.2.2  christos 	return (atomic_cas_uint(&track->lock, 0, 1) == 0);
   1570  1.16.2.2  christos }
   1571  1.16.2.2  christos 
   1572  1.16.2.2  christos /*
   1573  1.16.2.2  christos  * Acquire track lock.
   1574  1.16.2.2  christos  */
   1575  1.16.2.2  christos static __inline void
   1576  1.16.2.2  christos audio_track_lock_enter(audio_track_t *track)
   1577  1.16.2.2  christos {
   1578  1.16.2.2  christos 	/* Don't sleep here. */
   1579  1.16.2.2  christos 	while (audio_track_lock_tryenter(track) == false)
   1580  1.16.2.2  christos 		;
   1581  1.16.2.2  christos }
   1582  1.16.2.2  christos 
   1583  1.16.2.2  christos /*
   1584  1.16.2.2  christos  * Release track lock.
   1585  1.16.2.2  christos  */
   1586  1.16.2.2  christos static __inline void
   1587  1.16.2.2  christos audio_track_lock_exit(audio_track_t *track)
   1588  1.16.2.2  christos {
   1589  1.16.2.2  christos 	atomic_swap_uint(&track->lock, 0);
   1590  1.16.2.2  christos }
   1591  1.16.2.2  christos 
   1592  1.16.2.2  christos 
   1593  1.16.2.2  christos static int
   1594  1.16.2.2  christos audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
   1595  1.16.2.2  christos {
   1596  1.16.2.2  christos 	struct audio_softc *sc;
   1597  1.16.2.2  christos 	int error;
   1598  1.16.2.2  christos 
   1599  1.16.2.2  christos 	/* Find the device */
   1600  1.16.2.2  christos 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1601  1.16.2.2  christos 	if (sc == NULL || sc->hw_if == NULL)
   1602  1.16.2.2  christos 		return ENXIO;
   1603  1.16.2.2  christos 
   1604  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   1605  1.16.2.2  christos 	if (error)
   1606  1.16.2.2  christos 		return error;
   1607  1.16.2.2  christos 
   1608  1.16.2.2  christos 	device_active(sc->sc_dev, DVA_SYSTEM);
   1609  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1610  1.16.2.2  christos 	case SOUND_DEVICE:
   1611  1.16.2.2  christos 	case AUDIO_DEVICE:
   1612  1.16.2.2  christos 		error = audio_open(dev, sc, flags, ifmt, l, NULL);
   1613  1.16.2.2  christos 		break;
   1614  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1615  1.16.2.2  christos 		error = audioctl_open(dev, sc, flags, ifmt, l);
   1616  1.16.2.2  christos 		break;
   1617  1.16.2.2  christos 	case MIXER_DEVICE:
   1618  1.16.2.2  christos 		error = mixer_open(dev, sc, flags, ifmt, l);
   1619  1.16.2.2  christos 		break;
   1620  1.16.2.2  christos 	default:
   1621  1.16.2.2  christos 		error = ENXIO;
   1622  1.16.2.2  christos 		break;
   1623  1.16.2.2  christos 	}
   1624  1.16.2.3    martin 	audio_exlock_exit(sc);
   1625  1.16.2.2  christos 
   1626  1.16.2.2  christos 	return error;
   1627  1.16.2.2  christos }
   1628  1.16.2.2  christos 
   1629  1.16.2.2  christos static int
   1630  1.16.2.2  christos audioclose(struct file *fp)
   1631  1.16.2.2  christos {
   1632  1.16.2.2  christos 	struct audio_softc *sc;
   1633  1.16.2.3    martin 	struct psref sc_ref;
   1634  1.16.2.2  christos 	audio_file_t *file;
   1635  1.16.2.2  christos 	int error;
   1636  1.16.2.2  christos 	dev_t dev;
   1637  1.16.2.2  christos 
   1638  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1639  1.16.2.2  christos 	file = fp->f_audioctx;
   1640  1.16.2.2  christos 	dev = file->dev;
   1641  1.16.2.3    martin 	error = 0;
   1642  1.16.2.2  christos 
   1643  1.16.2.3    martin 	/*
   1644  1.16.2.3    martin 	 * audioclose() must
   1645  1.16.2.3    martin 	 * - unplug track from the trackmixer (and unplug anything from softc),
   1646  1.16.2.3    martin 	 *   if sc exists.
   1647  1.16.2.3    martin 	 * - free all memory objects, regardless of sc.
   1648  1.16.2.3    martin 	 */
   1649  1.16.2.2  christos 
   1650  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1651  1.16.2.3    martin 	if (sc) {
   1652  1.16.2.3    martin 		switch (AUDIODEV(dev)) {
   1653  1.16.2.3    martin 		case SOUND_DEVICE:
   1654  1.16.2.3    martin 		case AUDIO_DEVICE:
   1655  1.16.2.3    martin 			error = audio_close(sc, file);
   1656  1.16.2.3    martin 			break;
   1657  1.16.2.3    martin 		case AUDIOCTL_DEVICE:
   1658  1.16.2.3    martin 			error = 0;
   1659  1.16.2.3    martin 			break;
   1660  1.16.2.3    martin 		case MIXER_DEVICE:
   1661  1.16.2.3    martin 			error = mixer_close(sc, file);
   1662  1.16.2.3    martin 			break;
   1663  1.16.2.3    martin 		default:
   1664  1.16.2.3    martin 			error = ENXIO;
   1665  1.16.2.3    martin 			break;
   1666  1.16.2.3    martin 		}
   1667  1.16.2.3    martin 
   1668  1.16.2.3    martin 		audio_file_exit(sc, &sc_ref);
   1669  1.16.2.2  christos 	}
   1670  1.16.2.2  christos 
   1671  1.16.2.3    martin 	/* Free memory objects anyway */
   1672  1.16.2.3    martin 	TRACEF(2, file, "free memory");
   1673  1.16.2.3    martin 	if (file->ptrack)
   1674  1.16.2.3    martin 		audio_track_destroy(file->ptrack);
   1675  1.16.2.3    martin 	if (file->rtrack)
   1676  1.16.2.3    martin 		audio_track_destroy(file->rtrack);
   1677  1.16.2.3    martin 	kmem_free(file, sizeof(*file));
   1678  1.16.2.3    martin 	fp->f_audioctx = NULL;
   1679  1.16.2.3    martin 
   1680  1.16.2.2  christos 	return error;
   1681  1.16.2.2  christos }
   1682  1.16.2.2  christos 
   1683  1.16.2.2  christos static int
   1684  1.16.2.2  christos audioread(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1685  1.16.2.2  christos 	int ioflag)
   1686  1.16.2.2  christos {
   1687  1.16.2.2  christos 	struct audio_softc *sc;
   1688  1.16.2.3    martin 	struct psref sc_ref;
   1689  1.16.2.2  christos 	audio_file_t *file;
   1690  1.16.2.2  christos 	int error;
   1691  1.16.2.2  christos 	dev_t dev;
   1692  1.16.2.2  christos 
   1693  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1694  1.16.2.2  christos 	file = fp->f_audioctx;
   1695  1.16.2.2  christos 	dev = file->dev;
   1696  1.16.2.2  christos 
   1697  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1698  1.16.2.3    martin 	if (sc == NULL)
   1699  1.16.2.3    martin 		return EIO;
   1700  1.16.2.3    martin 
   1701  1.16.2.2  christos 	if (fp->f_flag & O_NONBLOCK)
   1702  1.16.2.2  christos 		ioflag |= IO_NDELAY;
   1703  1.16.2.2  christos 
   1704  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1705  1.16.2.2  christos 	case SOUND_DEVICE:
   1706  1.16.2.2  christos 	case AUDIO_DEVICE:
   1707  1.16.2.2  christos 		error = audio_read(sc, uio, ioflag, file);
   1708  1.16.2.2  christos 		break;
   1709  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1710  1.16.2.2  christos 	case MIXER_DEVICE:
   1711  1.16.2.2  christos 		error = ENODEV;
   1712  1.16.2.2  christos 		break;
   1713  1.16.2.2  christos 	default:
   1714  1.16.2.2  christos 		error = ENXIO;
   1715  1.16.2.2  christos 		break;
   1716  1.16.2.2  christos 	}
   1717  1.16.2.2  christos 
   1718  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1719  1.16.2.2  christos 	return error;
   1720  1.16.2.2  christos }
   1721  1.16.2.2  christos 
   1722  1.16.2.2  christos static int
   1723  1.16.2.2  christos audiowrite(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
   1724  1.16.2.2  christos 	int ioflag)
   1725  1.16.2.2  christos {
   1726  1.16.2.2  christos 	struct audio_softc *sc;
   1727  1.16.2.3    martin 	struct psref sc_ref;
   1728  1.16.2.2  christos 	audio_file_t *file;
   1729  1.16.2.2  christos 	int error;
   1730  1.16.2.2  christos 	dev_t dev;
   1731  1.16.2.2  christos 
   1732  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1733  1.16.2.2  christos 	file = fp->f_audioctx;
   1734  1.16.2.2  christos 	dev = file->dev;
   1735  1.16.2.2  christos 
   1736  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1737  1.16.2.3    martin 	if (sc == NULL)
   1738  1.16.2.3    martin 		return EIO;
   1739  1.16.2.3    martin 
   1740  1.16.2.2  christos 	if (fp->f_flag & O_NONBLOCK)
   1741  1.16.2.2  christos 		ioflag |= IO_NDELAY;
   1742  1.16.2.2  christos 
   1743  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1744  1.16.2.2  christos 	case SOUND_DEVICE:
   1745  1.16.2.2  christos 	case AUDIO_DEVICE:
   1746  1.16.2.2  christos 		error = audio_write(sc, uio, ioflag, file);
   1747  1.16.2.2  christos 		break;
   1748  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1749  1.16.2.2  christos 	case MIXER_DEVICE:
   1750  1.16.2.2  christos 		error = ENODEV;
   1751  1.16.2.2  christos 		break;
   1752  1.16.2.2  christos 	default:
   1753  1.16.2.2  christos 		error = ENXIO;
   1754  1.16.2.2  christos 		break;
   1755  1.16.2.2  christos 	}
   1756  1.16.2.2  christos 
   1757  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1758  1.16.2.2  christos 	return error;
   1759  1.16.2.2  christos }
   1760  1.16.2.2  christos 
   1761  1.16.2.2  christos static int
   1762  1.16.2.2  christos audioioctl(struct file *fp, u_long cmd, void *addr)
   1763  1.16.2.2  christos {
   1764  1.16.2.2  christos 	struct audio_softc *sc;
   1765  1.16.2.3    martin 	struct psref sc_ref;
   1766  1.16.2.2  christos 	audio_file_t *file;
   1767  1.16.2.2  christos 	struct lwp *l = curlwp;
   1768  1.16.2.2  christos 	int error;
   1769  1.16.2.2  christos 	dev_t dev;
   1770  1.16.2.2  christos 
   1771  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1772  1.16.2.2  christos 	file = fp->f_audioctx;
   1773  1.16.2.2  christos 	dev = file->dev;
   1774  1.16.2.2  christos 
   1775  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1776  1.16.2.3    martin 	if (sc == NULL)
   1777  1.16.2.3    martin 		return EIO;
   1778  1.16.2.3    martin 
   1779  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1780  1.16.2.2  christos 	case SOUND_DEVICE:
   1781  1.16.2.2  christos 	case AUDIO_DEVICE:
   1782  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1783  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   1784  1.16.2.2  christos 		device_active(sc->sc_dev, DVA_SYSTEM);
   1785  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   1786  1.16.2.2  christos 		if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ))
   1787  1.16.2.2  christos 			error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   1788  1.16.2.2  christos 		else
   1789  1.16.2.2  christos 			error = audio_ioctl(dev, sc, cmd, addr, fp->f_flag, l,
   1790  1.16.2.2  christos 			    file);
   1791  1.16.2.2  christos 		break;
   1792  1.16.2.2  christos 	case MIXER_DEVICE:
   1793  1.16.2.2  christos 		error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
   1794  1.16.2.2  christos 		break;
   1795  1.16.2.2  christos 	default:
   1796  1.16.2.2  christos 		error = ENXIO;
   1797  1.16.2.2  christos 		break;
   1798  1.16.2.2  christos 	}
   1799  1.16.2.2  christos 
   1800  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1801  1.16.2.2  christos 	return error;
   1802  1.16.2.2  christos }
   1803  1.16.2.2  christos 
   1804  1.16.2.2  christos static int
   1805  1.16.2.2  christos audiostat(struct file *fp, struct stat *st)
   1806  1.16.2.2  christos {
   1807  1.16.2.3    martin 	struct audio_softc *sc;
   1808  1.16.2.3    martin 	struct psref sc_ref;
   1809  1.16.2.2  christos 	audio_file_t *file;
   1810  1.16.2.2  christos 
   1811  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1812  1.16.2.2  christos 	file = fp->f_audioctx;
   1813  1.16.2.2  christos 
   1814  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1815  1.16.2.3    martin 	if (sc == NULL)
   1816  1.16.2.3    martin 		return EIO;
   1817  1.16.2.3    martin 
   1818  1.16.2.2  christos 	memset(st, 0, sizeof(*st));
   1819  1.16.2.2  christos 
   1820  1.16.2.2  christos 	st->st_dev = file->dev;
   1821  1.16.2.2  christos 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
   1822  1.16.2.2  christos 	st->st_gid = kauth_cred_getegid(fp->f_cred);
   1823  1.16.2.2  christos 	st->st_mode = S_IFCHR;
   1824  1.16.2.3    martin 
   1825  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1826  1.16.2.2  christos 	return 0;
   1827  1.16.2.2  christos }
   1828  1.16.2.2  christos 
   1829  1.16.2.2  christos static int
   1830  1.16.2.2  christos audiopoll(struct file *fp, int events)
   1831  1.16.2.2  christos {
   1832  1.16.2.2  christos 	struct audio_softc *sc;
   1833  1.16.2.3    martin 	struct psref sc_ref;
   1834  1.16.2.2  christos 	audio_file_t *file;
   1835  1.16.2.2  christos 	struct lwp *l = curlwp;
   1836  1.16.2.2  christos 	int revents;
   1837  1.16.2.2  christos 	dev_t dev;
   1838  1.16.2.2  christos 
   1839  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1840  1.16.2.2  christos 	file = fp->f_audioctx;
   1841  1.16.2.2  christos 	dev = file->dev;
   1842  1.16.2.2  christos 
   1843  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1844  1.16.2.3    martin 	if (sc == NULL)
   1845  1.16.2.3    martin 		return EIO;
   1846  1.16.2.3    martin 
   1847  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1848  1.16.2.2  christos 	case SOUND_DEVICE:
   1849  1.16.2.2  christos 	case AUDIO_DEVICE:
   1850  1.16.2.2  christos 		revents = audio_poll(sc, events, l, file);
   1851  1.16.2.2  christos 		break;
   1852  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1853  1.16.2.2  christos 	case MIXER_DEVICE:
   1854  1.16.2.2  christos 		revents = 0;
   1855  1.16.2.2  christos 		break;
   1856  1.16.2.2  christos 	default:
   1857  1.16.2.2  christos 		revents = POLLERR;
   1858  1.16.2.2  christos 		break;
   1859  1.16.2.2  christos 	}
   1860  1.16.2.2  christos 
   1861  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1862  1.16.2.2  christos 	return revents;
   1863  1.16.2.2  christos }
   1864  1.16.2.2  christos 
   1865  1.16.2.2  christos static int
   1866  1.16.2.2  christos audiokqfilter(struct file *fp, struct knote *kn)
   1867  1.16.2.2  christos {
   1868  1.16.2.2  christos 	struct audio_softc *sc;
   1869  1.16.2.3    martin 	struct psref sc_ref;
   1870  1.16.2.2  christos 	audio_file_t *file;
   1871  1.16.2.2  christos 	dev_t dev;
   1872  1.16.2.2  christos 	int error;
   1873  1.16.2.2  christos 
   1874  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1875  1.16.2.2  christos 	file = fp->f_audioctx;
   1876  1.16.2.2  christos 	dev = file->dev;
   1877  1.16.2.2  christos 
   1878  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1879  1.16.2.3    martin 	if (sc == NULL)
   1880  1.16.2.3    martin 		return EIO;
   1881  1.16.2.3    martin 
   1882  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1883  1.16.2.2  christos 	case SOUND_DEVICE:
   1884  1.16.2.2  christos 	case AUDIO_DEVICE:
   1885  1.16.2.2  christos 		error = audio_kqfilter(sc, file, kn);
   1886  1.16.2.2  christos 		break;
   1887  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1888  1.16.2.2  christos 	case MIXER_DEVICE:
   1889  1.16.2.2  christos 		error = ENODEV;
   1890  1.16.2.2  christos 		break;
   1891  1.16.2.2  christos 	default:
   1892  1.16.2.2  christos 		error = ENXIO;
   1893  1.16.2.2  christos 		break;
   1894  1.16.2.2  christos 	}
   1895  1.16.2.2  christos 
   1896  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1897  1.16.2.2  christos 	return error;
   1898  1.16.2.2  christos }
   1899  1.16.2.2  christos 
   1900  1.16.2.2  christos static int
   1901  1.16.2.2  christos audiommap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
   1902  1.16.2.2  christos 	int *advicep, struct uvm_object **uobjp, int *maxprotp)
   1903  1.16.2.2  christos {
   1904  1.16.2.2  christos 	struct audio_softc *sc;
   1905  1.16.2.3    martin 	struct psref sc_ref;
   1906  1.16.2.2  christos 	audio_file_t *file;
   1907  1.16.2.2  christos 	dev_t dev;
   1908  1.16.2.2  christos 	int error;
   1909  1.16.2.2  christos 
   1910  1.16.2.2  christos 	KASSERT(fp->f_audioctx);
   1911  1.16.2.2  christos 	file = fp->f_audioctx;
   1912  1.16.2.2  christos 	dev = file->dev;
   1913  1.16.2.2  christos 
   1914  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1915  1.16.2.3    martin 	if (sc == NULL)
   1916  1.16.2.3    martin 		return EIO;
   1917  1.16.2.3    martin 
   1918  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   1919  1.16.2.2  christos 	device_active(sc->sc_dev, DVA_SYSTEM); /* XXXJDM */
   1920  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   1921  1.16.2.2  christos 
   1922  1.16.2.2  christos 	switch (AUDIODEV(dev)) {
   1923  1.16.2.2  christos 	case SOUND_DEVICE:
   1924  1.16.2.2  christos 	case AUDIO_DEVICE:
   1925  1.16.2.2  christos 		error = audio_mmap(sc, offp, len, prot, flagsp, advicep,
   1926  1.16.2.2  christos 		    uobjp, maxprotp, file);
   1927  1.16.2.2  christos 		break;
   1928  1.16.2.2  christos 	case AUDIOCTL_DEVICE:
   1929  1.16.2.2  christos 	case MIXER_DEVICE:
   1930  1.16.2.2  christos 	default:
   1931  1.16.2.2  christos 		error = ENOTSUP;
   1932  1.16.2.2  christos 		break;
   1933  1.16.2.2  christos 	}
   1934  1.16.2.2  christos 
   1935  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1936  1.16.2.2  christos 	return error;
   1937  1.16.2.2  christos }
   1938  1.16.2.2  christos 
   1939  1.16.2.2  christos 
   1940  1.16.2.2  christos /* Exported interfaces for audiobell. */
   1941  1.16.2.2  christos 
   1942  1.16.2.2  christos /*
   1943  1.16.2.2  christos  * Open for audiobell.
   1944  1.16.2.3    martin  * It stores allocated file to *filep.
   1945  1.16.2.2  christos  * If successful returns 0, otherwise errno.
   1946  1.16.2.2  christos  */
   1947  1.16.2.2  christos int
   1948  1.16.2.3    martin audiobellopen(dev_t dev, audio_file_t **filep)
   1949  1.16.2.2  christos {
   1950  1.16.2.2  christos 	struct audio_softc *sc;
   1951  1.16.2.2  christos 	int error;
   1952  1.16.2.2  christos 
   1953  1.16.2.2  christos 	/* Find the device */
   1954  1.16.2.2  christos 	sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
   1955  1.16.2.2  christos 	if (sc == NULL || sc->hw_if == NULL)
   1956  1.16.2.2  christos 		return ENXIO;
   1957  1.16.2.2  christos 
   1958  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   1959  1.16.2.2  christos 	if (error)
   1960  1.16.2.2  christos 		return error;
   1961  1.16.2.2  christos 
   1962  1.16.2.2  christos 	device_active(sc->sc_dev, DVA_SYSTEM);
   1963  1.16.2.3    martin 	error = audio_open(dev, sc, FWRITE, 0, curlwp, filep);
   1964  1.16.2.2  christos 
   1965  1.16.2.3    martin 	audio_exlock_exit(sc);
   1966  1.16.2.2  christos 	return error;
   1967  1.16.2.2  christos }
   1968  1.16.2.2  christos 
   1969  1.16.2.2  christos /* Close for audiobell */
   1970  1.16.2.2  christos int
   1971  1.16.2.2  christos audiobellclose(audio_file_t *file)
   1972  1.16.2.2  christos {
   1973  1.16.2.2  christos 	struct audio_softc *sc;
   1974  1.16.2.3    martin 	struct psref sc_ref;
   1975  1.16.2.2  christos 	int error;
   1976  1.16.2.2  christos 
   1977  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   1978  1.16.2.3    martin 	if (sc == NULL)
   1979  1.16.2.3    martin 		return EIO;
   1980  1.16.2.2  christos 
   1981  1.16.2.2  christos 	error = audio_close(sc, file);
   1982  1.16.2.2  christos 
   1983  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   1984  1.16.2.2  christos 
   1985  1.16.2.3    martin 	KASSERT(file->ptrack);
   1986  1.16.2.3    martin 	audio_track_destroy(file->ptrack);
   1987  1.16.2.3    martin 	KASSERT(file->rtrack == NULL);
   1988  1.16.2.3    martin 	kmem_free(file, sizeof(*file));
   1989  1.16.2.3    martin 	return error;
   1990  1.16.2.3    martin }
   1991  1.16.2.3    martin 
   1992  1.16.2.3    martin /* Set sample rate for audiobell */
   1993  1.16.2.3    martin int
   1994  1.16.2.3    martin audiobellsetrate(audio_file_t *file, u_int sample_rate)
   1995  1.16.2.3    martin {
   1996  1.16.2.3    martin 	struct audio_softc *sc;
   1997  1.16.2.3    martin 	struct psref sc_ref;
   1998  1.16.2.3    martin 	struct audio_info ai;
   1999  1.16.2.3    martin 	int error;
   2000  1.16.2.3    martin 
   2001  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   2002  1.16.2.3    martin 	if (sc == NULL)
   2003  1.16.2.3    martin 		return EIO;
   2004  1.16.2.3    martin 
   2005  1.16.2.3    martin 	AUDIO_INITINFO(&ai);
   2006  1.16.2.3    martin 	ai.play.sample_rate = sample_rate;
   2007  1.16.2.3    martin 
   2008  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   2009  1.16.2.3    martin 	if (error)
   2010  1.16.2.3    martin 		goto done;
   2011  1.16.2.3    martin 	error = audio_file_setinfo(sc, file, &ai);
   2012  1.16.2.3    martin 	audio_exlock_exit(sc);
   2013  1.16.2.3    martin 
   2014  1.16.2.3    martin done:
   2015  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   2016  1.16.2.2  christos 	return error;
   2017  1.16.2.2  christos }
   2018  1.16.2.2  christos 
   2019  1.16.2.2  christos /* Playback for audiobell */
   2020  1.16.2.2  christos int
   2021  1.16.2.2  christos audiobellwrite(audio_file_t *file, struct uio *uio)
   2022  1.16.2.2  christos {
   2023  1.16.2.2  christos 	struct audio_softc *sc;
   2024  1.16.2.3    martin 	struct psref sc_ref;
   2025  1.16.2.2  christos 	int error;
   2026  1.16.2.2  christos 
   2027  1.16.2.3    martin 	sc = audio_file_enter(file, &sc_ref);
   2028  1.16.2.3    martin 	if (sc == NULL)
   2029  1.16.2.3    martin 		return EIO;
   2030  1.16.2.3    martin 
   2031  1.16.2.2  christos 	error = audio_write(sc, uio, 0, file);
   2032  1.16.2.3    martin 
   2033  1.16.2.3    martin 	audio_file_exit(sc, &sc_ref);
   2034  1.16.2.2  christos 	return error;
   2035  1.16.2.2  christos }
   2036  1.16.2.2  christos 
   2037  1.16.2.2  christos 
   2038  1.16.2.2  christos /*
   2039  1.16.2.2  christos  * Audio driver
   2040  1.16.2.2  christos  */
   2041  1.16.2.3    martin 
   2042  1.16.2.3    martin /*
   2043  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   2044  1.16.2.3    martin  */
   2045  1.16.2.2  christos int
   2046  1.16.2.2  christos audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   2047  1.16.2.3    martin 	struct lwp *l, audio_file_t **bellfile)
   2048  1.16.2.2  christos {
   2049  1.16.2.2  christos 	struct audio_info ai;
   2050  1.16.2.2  christos 	struct file *fp;
   2051  1.16.2.2  christos 	audio_file_t *af;
   2052  1.16.2.2  christos 	audio_ring_t *hwbuf;
   2053  1.16.2.2  christos 	bool fullduplex;
   2054  1.16.2.2  christos 	int fd;
   2055  1.16.2.2  christos 	int error;
   2056  1.16.2.2  christos 
   2057  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   2058  1.16.2.2  christos 
   2059  1.16.2.3    martin 	TRACE(1, "%sdev=%s flags=0x%x po=%d ro=%d",
   2060  1.16.2.2  christos 	    (audiodebug >= 3) ? "start " : "",
   2061  1.16.2.3    martin 	    ISDEVSOUND(dev) ? "sound" : "audio",
   2062  1.16.2.2  christos 	    flags, sc->sc_popens, sc->sc_ropens);
   2063  1.16.2.2  christos 
   2064  1.16.2.2  christos 	af = kmem_zalloc(sizeof(audio_file_t), KM_SLEEP);
   2065  1.16.2.2  christos 	af->sc = sc;
   2066  1.16.2.2  christos 	af->dev = dev;
   2067  1.16.2.2  christos 	if ((flags & FWRITE) != 0 && audio_can_playback(sc))
   2068  1.16.2.2  christos 		af->mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
   2069  1.16.2.2  christos 	if ((flags & FREAD) != 0 && audio_can_capture(sc))
   2070  1.16.2.2  christos 		af->mode |= AUMODE_RECORD;
   2071  1.16.2.2  christos 	if (af->mode == 0) {
   2072  1.16.2.2  christos 		error = ENXIO;
   2073  1.16.2.2  christos 		goto bad1;
   2074  1.16.2.2  christos 	}
   2075  1.16.2.2  christos 
   2076  1.16.2.3    martin 	fullduplex = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
   2077  1.16.2.2  christos 
   2078  1.16.2.2  christos 	/*
   2079  1.16.2.2  christos 	 * On half duplex hardware,
   2080  1.16.2.2  christos 	 * 1. if mode is (PLAY | REC), let mode PLAY.
   2081  1.16.2.2  christos 	 * 2. if mode is PLAY, let mode PLAY if no rec tracks, otherwise error.
   2082  1.16.2.2  christos 	 * 3. if mode is REC, let mode REC if no play tracks, otherwise error.
   2083  1.16.2.2  christos 	 */
   2084  1.16.2.2  christos 	if (fullduplex == false) {
   2085  1.16.2.2  christos 		if ((af->mode & AUMODE_PLAY)) {
   2086  1.16.2.2  christos 			if (sc->sc_ropens != 0) {
   2087  1.16.2.2  christos 				TRACE(1, "record track already exists");
   2088  1.16.2.2  christos 				error = ENODEV;
   2089  1.16.2.2  christos 				goto bad1;
   2090  1.16.2.2  christos 			}
   2091  1.16.2.2  christos 			/* Play takes precedence */
   2092  1.16.2.2  christos 			af->mode &= ~AUMODE_RECORD;
   2093  1.16.2.2  christos 		}
   2094  1.16.2.2  christos 		if ((af->mode & AUMODE_RECORD)) {
   2095  1.16.2.2  christos 			if (sc->sc_popens != 0) {
   2096  1.16.2.2  christos 				TRACE(1, "play track already exists");
   2097  1.16.2.2  christos 				error = ENODEV;
   2098  1.16.2.2  christos 				goto bad1;
   2099  1.16.2.2  christos 			}
   2100  1.16.2.2  christos 		}
   2101  1.16.2.2  christos 	}
   2102  1.16.2.2  christos 
   2103  1.16.2.2  christos 	/* Create tracks */
   2104  1.16.2.2  christos 	if ((af->mode & AUMODE_PLAY))
   2105  1.16.2.2  christos 		af->ptrack = audio_track_create(sc, sc->sc_pmixer);
   2106  1.16.2.2  christos 	if ((af->mode & AUMODE_RECORD))
   2107  1.16.2.2  christos 		af->rtrack = audio_track_create(sc, sc->sc_rmixer);
   2108  1.16.2.2  christos 
   2109  1.16.2.2  christos 	/* Set parameters */
   2110  1.16.2.2  christos 	AUDIO_INITINFO(&ai);
   2111  1.16.2.3    martin 	if (bellfile) {
   2112  1.16.2.3    martin 		/* If audiobell, only sample_rate will be set later. */
   2113  1.16.2.3    martin 		ai.play.sample_rate   = audio_default.sample_rate;
   2114  1.16.2.3    martin 		ai.play.encoding      = AUDIO_ENCODING_SLINEAR_NE;
   2115  1.16.2.3    martin 		ai.play.channels      = 1;
   2116  1.16.2.3    martin 		ai.play.precision     = 16;
   2117  1.16.2.3    martin 		ai.play.pause         = 0;
   2118  1.16.2.2  christos 	} else if (ISDEVAUDIO(dev)) {
   2119  1.16.2.2  christos 		/* If /dev/audio, initialize everytime. */
   2120  1.16.2.2  christos 		ai.play.sample_rate   = audio_default.sample_rate;
   2121  1.16.2.2  christos 		ai.play.encoding      = audio_default.encoding;
   2122  1.16.2.2  christos 		ai.play.channels      = audio_default.channels;
   2123  1.16.2.2  christos 		ai.play.precision     = audio_default.precision;
   2124  1.16.2.3    martin 		ai.play.pause         = 0;
   2125  1.16.2.2  christos 		ai.record.sample_rate = audio_default.sample_rate;
   2126  1.16.2.2  christos 		ai.record.encoding    = audio_default.encoding;
   2127  1.16.2.2  christos 		ai.record.channels    = audio_default.channels;
   2128  1.16.2.2  christos 		ai.record.precision   = audio_default.precision;
   2129  1.16.2.3    martin 		ai.record.pause       = 0;
   2130  1.16.2.2  christos 	} else {
   2131  1.16.2.2  christos 		/* If /dev/sound, take over the previous parameters. */
   2132  1.16.2.2  christos 		ai.play.sample_rate   = sc->sc_sound_pparams.sample_rate;
   2133  1.16.2.2  christos 		ai.play.encoding      = sc->sc_sound_pparams.encoding;
   2134  1.16.2.2  christos 		ai.play.channels      = sc->sc_sound_pparams.channels;
   2135  1.16.2.2  christos 		ai.play.precision     = sc->sc_sound_pparams.precision;
   2136  1.16.2.2  christos 		ai.play.pause         = sc->sc_sound_ppause;
   2137  1.16.2.2  christos 		ai.record.sample_rate = sc->sc_sound_rparams.sample_rate;
   2138  1.16.2.2  christos 		ai.record.encoding    = sc->sc_sound_rparams.encoding;
   2139  1.16.2.2  christos 		ai.record.channels    = sc->sc_sound_rparams.channels;
   2140  1.16.2.2  christos 		ai.record.precision   = sc->sc_sound_rparams.precision;
   2141  1.16.2.2  christos 		ai.record.pause       = sc->sc_sound_rpause;
   2142  1.16.2.2  christos 	}
   2143  1.16.2.2  christos 	error = audio_file_setinfo(sc, af, &ai);
   2144  1.16.2.2  christos 	if (error)
   2145  1.16.2.2  christos 		goto bad2;
   2146  1.16.2.2  christos 
   2147  1.16.2.2  christos 	if (sc->sc_popens + sc->sc_ropens == 0) {
   2148  1.16.2.2  christos 		/* First open */
   2149  1.16.2.2  christos 
   2150  1.16.2.2  christos 		sc->sc_cred = kauth_cred_get();
   2151  1.16.2.2  christos 		kauth_cred_hold(sc->sc_cred);
   2152  1.16.2.2  christos 
   2153  1.16.2.2  christos 		if (sc->hw_if->open) {
   2154  1.16.2.2  christos 			int hwflags;
   2155  1.16.2.2  christos 
   2156  1.16.2.2  christos 			/*
   2157  1.16.2.2  christos 			 * Call hw_if->open() only at first open of
   2158  1.16.2.2  christos 			 * combination of playback and recording.
   2159  1.16.2.2  christos 			 * On full duplex hardware, the flags passed to
   2160  1.16.2.2  christos 			 * hw_if->open() is always (FREAD | FWRITE)
   2161  1.16.2.2  christos 			 * regardless of this open()'s flags.
   2162  1.16.2.2  christos 			 * see also dev/isa/aria.c
   2163  1.16.2.2  christos 			 * On half duplex hardware, the flags passed to
   2164  1.16.2.2  christos 			 * hw_if->open() is either FREAD or FWRITE.
   2165  1.16.2.2  christos 			 * see also arch/evbarm/mini2440/audio_mini2440.c
   2166  1.16.2.2  christos 			 */
   2167  1.16.2.2  christos 			if (fullduplex) {
   2168  1.16.2.2  christos 				hwflags = FREAD | FWRITE;
   2169  1.16.2.2  christos 			} else {
   2170  1.16.2.2  christos 				/* Construct hwflags from af->mode. */
   2171  1.16.2.2  christos 				hwflags = 0;
   2172  1.16.2.2  christos 				if ((af->mode & AUMODE_PLAY) != 0)
   2173  1.16.2.2  christos 					hwflags |= FWRITE;
   2174  1.16.2.2  christos 				if ((af->mode & AUMODE_RECORD) != 0)
   2175  1.16.2.2  christos 					hwflags |= FREAD;
   2176  1.16.2.2  christos 			}
   2177  1.16.2.2  christos 
   2178  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   2179  1.16.2.2  christos 			mutex_enter(sc->sc_intr_lock);
   2180  1.16.2.2  christos 			error = sc->hw_if->open(sc->hw_hdl, hwflags);
   2181  1.16.2.2  christos 			mutex_exit(sc->sc_intr_lock);
   2182  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2183  1.16.2.2  christos 			if (error)
   2184  1.16.2.2  christos 				goto bad2;
   2185  1.16.2.2  christos 		}
   2186  1.16.2.2  christos 
   2187  1.16.2.2  christos 		/*
   2188  1.16.2.2  christos 		 * Set speaker mode when a half duplex.
   2189  1.16.2.2  christos 		 * XXX I'm not sure this is correct.
   2190  1.16.2.2  christos 		 */
   2191  1.16.2.2  christos 		if (1/*XXX*/) {
   2192  1.16.2.2  christos 			if (sc->hw_if->speaker_ctl) {
   2193  1.16.2.2  christos 				int on;
   2194  1.16.2.2  christos 				if (af->ptrack) {
   2195  1.16.2.2  christos 					on = 1;
   2196  1.16.2.2  christos 				} else {
   2197  1.16.2.2  christos 					on = 0;
   2198  1.16.2.2  christos 				}
   2199  1.16.2.3    martin 				mutex_enter(sc->sc_lock);
   2200  1.16.2.2  christos 				mutex_enter(sc->sc_intr_lock);
   2201  1.16.2.2  christos 				error = sc->hw_if->speaker_ctl(sc->hw_hdl, on);
   2202  1.16.2.2  christos 				mutex_exit(sc->sc_intr_lock);
   2203  1.16.2.3    martin 				mutex_exit(sc->sc_lock);
   2204  1.16.2.2  christos 				if (error)
   2205  1.16.2.2  christos 					goto bad3;
   2206  1.16.2.2  christos 			}
   2207  1.16.2.2  christos 		}
   2208  1.16.2.2  christos 	} else if (sc->sc_multiuser == false) {
   2209  1.16.2.2  christos 		uid_t euid = kauth_cred_geteuid(kauth_cred_get());
   2210  1.16.2.2  christos 		if (euid != 0 && euid != kauth_cred_geteuid(sc->sc_cred)) {
   2211  1.16.2.2  christos 			error = EPERM;
   2212  1.16.2.2  christos 			goto bad2;
   2213  1.16.2.2  christos 		}
   2214  1.16.2.2  christos 	}
   2215  1.16.2.2  christos 
   2216  1.16.2.2  christos 	/* Call init_output if this is the first playback open. */
   2217  1.16.2.2  christos 	if (af->ptrack && sc->sc_popens == 0) {
   2218  1.16.2.2  christos 		if (sc->hw_if->init_output) {
   2219  1.16.2.2  christos 			hwbuf = &sc->sc_pmixer->hwbuf;
   2220  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   2221  1.16.2.2  christos 			mutex_enter(sc->sc_intr_lock);
   2222  1.16.2.2  christos 			error = sc->hw_if->init_output(sc->hw_hdl,
   2223  1.16.2.2  christos 			    hwbuf->mem,
   2224  1.16.2.2  christos 			    hwbuf->capacity *
   2225  1.16.2.2  christos 			    hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
   2226  1.16.2.2  christos 			mutex_exit(sc->sc_intr_lock);
   2227  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2228  1.16.2.2  christos 			if (error)
   2229  1.16.2.2  christos 				goto bad3;
   2230  1.16.2.2  christos 		}
   2231  1.16.2.2  christos 	}
   2232  1.16.2.3    martin 	/*
   2233  1.16.2.3    martin 	 * Call init_input and start rmixer, if this is the first recording
   2234  1.16.2.3    martin 	 * open.  See pause consideration notes.
   2235  1.16.2.3    martin 	 */
   2236  1.16.2.2  christos 	if (af->rtrack && sc->sc_ropens == 0) {
   2237  1.16.2.2  christos 		if (sc->hw_if->init_input) {
   2238  1.16.2.2  christos 			hwbuf = &sc->sc_rmixer->hwbuf;
   2239  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   2240  1.16.2.2  christos 			mutex_enter(sc->sc_intr_lock);
   2241  1.16.2.2  christos 			error = sc->hw_if->init_input(sc->hw_hdl,
   2242  1.16.2.2  christos 			    hwbuf->mem,
   2243  1.16.2.2  christos 			    hwbuf->capacity *
   2244  1.16.2.2  christos 			    hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
   2245  1.16.2.2  christos 			mutex_exit(sc->sc_intr_lock);
   2246  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2247  1.16.2.2  christos 			if (error)
   2248  1.16.2.2  christos 				goto bad3;
   2249  1.16.2.2  christos 		}
   2250  1.16.2.3    martin 
   2251  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   2252  1.16.2.3    martin 		audio_rmixer_start(sc);
   2253  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   2254  1.16.2.2  christos 	}
   2255  1.16.2.2  christos 
   2256  1.16.2.3    martin 	if (bellfile == NULL) {
   2257  1.16.2.2  christos 		error = fd_allocfile(&fp, &fd);
   2258  1.16.2.2  christos 		if (error)
   2259  1.16.2.2  christos 			goto bad3;
   2260  1.16.2.2  christos 	}
   2261  1.16.2.2  christos 
   2262  1.16.2.2  christos 	/*
   2263  1.16.2.2  christos 	 * Count up finally.
   2264  1.16.2.2  christos 	 * Don't fail from here.
   2265  1.16.2.2  christos 	 */
   2266  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   2267  1.16.2.2  christos 	if (af->ptrack)
   2268  1.16.2.2  christos 		sc->sc_popens++;
   2269  1.16.2.2  christos 	if (af->rtrack)
   2270  1.16.2.2  christos 		sc->sc_ropens++;
   2271  1.16.2.2  christos 	mutex_enter(sc->sc_intr_lock);
   2272  1.16.2.2  christos 	SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
   2273  1.16.2.2  christos 	mutex_exit(sc->sc_intr_lock);
   2274  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   2275  1.16.2.2  christos 
   2276  1.16.2.3    martin 	if (bellfile) {
   2277  1.16.2.3    martin 		*bellfile = af;
   2278  1.16.2.2  christos 	} else {
   2279  1.16.2.2  christos 		error = fd_clone(fp, fd, flags, &audio_fileops, af);
   2280  1.16.2.3    martin 		KASSERTMSG(error == EMOVEFD, "error=%d", error);
   2281  1.16.2.2  christos 	}
   2282  1.16.2.2  christos 
   2283  1.16.2.2  christos 	TRACEF(3, af, "done");
   2284  1.16.2.2  christos 	return error;
   2285  1.16.2.2  christos 
   2286  1.16.2.2  christos 	/*
   2287  1.16.2.2  christos 	 * Since track here is not yet linked to sc_files,
   2288  1.16.2.2  christos 	 * you can call track_destroy() without sc_intr_lock.
   2289  1.16.2.2  christos 	 */
   2290  1.16.2.2  christos bad3:
   2291  1.16.2.2  christos 	if (sc->sc_popens + sc->sc_ropens == 0) {
   2292  1.16.2.2  christos 		if (sc->hw_if->close) {
   2293  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   2294  1.16.2.2  christos 			mutex_enter(sc->sc_intr_lock);
   2295  1.16.2.2  christos 			sc->hw_if->close(sc->hw_hdl);
   2296  1.16.2.2  christos 			mutex_exit(sc->sc_intr_lock);
   2297  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2298  1.16.2.2  christos 		}
   2299  1.16.2.2  christos 	}
   2300  1.16.2.2  christos bad2:
   2301  1.16.2.2  christos 	if (af->rtrack) {
   2302  1.16.2.2  christos 		audio_track_destroy(af->rtrack);
   2303  1.16.2.2  christos 		af->rtrack = NULL;
   2304  1.16.2.2  christos 	}
   2305  1.16.2.2  christos 	if (af->ptrack) {
   2306  1.16.2.2  christos 		audio_track_destroy(af->ptrack);
   2307  1.16.2.2  christos 		af->ptrack = NULL;
   2308  1.16.2.2  christos 	}
   2309  1.16.2.2  christos bad1:
   2310  1.16.2.2  christos 	kmem_free(af, sizeof(*af));
   2311  1.16.2.2  christos 	return error;
   2312  1.16.2.2  christos }
   2313  1.16.2.2  christos 
   2314  1.16.2.2  christos /*
   2315  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   2316  1.16.2.2  christos  */
   2317  1.16.2.2  christos int
   2318  1.16.2.2  christos audio_close(struct audio_softc *sc, audio_file_t *file)
   2319  1.16.2.2  christos {
   2320  1.16.2.3    martin 
   2321  1.16.2.3    martin 	/* Protect entering new fileops to this file */
   2322  1.16.2.3    martin 	atomic_store_relaxed(&file->dying, true);
   2323  1.16.2.3    martin 
   2324  1.16.2.3    martin 	/*
   2325  1.16.2.3    martin 	 * Drain first.
   2326  1.16.2.3    martin 	 * It must be done before unlinking(acquiring exlock).
   2327  1.16.2.3    martin 	 */
   2328  1.16.2.3    martin 	if (file->ptrack) {
   2329  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   2330  1.16.2.3    martin 		audio_track_drain(sc, file->ptrack);
   2331  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   2332  1.16.2.3    martin 	}
   2333  1.16.2.3    martin 
   2334  1.16.2.3    martin 	return audio_unlink(sc, file);
   2335  1.16.2.3    martin }
   2336  1.16.2.3    martin 
   2337  1.16.2.3    martin /*
   2338  1.16.2.3    martin  * Unlink this file, but not freeing memory here.
   2339  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   2340  1.16.2.3    martin  */
   2341  1.16.2.3    martin int
   2342  1.16.2.3    martin audio_unlink(struct audio_softc *sc, audio_file_t *file)
   2343  1.16.2.3    martin {
   2344  1.16.2.2  christos 	int error;
   2345  1.16.2.2  christos 
   2346  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   2347  1.16.2.2  christos 
   2348  1.16.2.2  christos 	TRACEF(1, file, "%spid=%d.%d po=%d ro=%d",
   2349  1.16.2.2  christos 	    (audiodebug >= 3) ? "start " : "",
   2350  1.16.2.2  christos 	    (int)curproc->p_pid, (int)curlwp->l_lid,
   2351  1.16.2.2  christos 	    sc->sc_popens, sc->sc_ropens);
   2352  1.16.2.2  christos 	KASSERTMSG(sc->sc_popens + sc->sc_ropens > 0,
   2353  1.16.2.2  christos 	    "sc->sc_popens=%d, sc->sc_ropens=%d",
   2354  1.16.2.2  christos 	    sc->sc_popens, sc->sc_ropens);
   2355  1.16.2.2  christos 
   2356  1.16.2.2  christos 	/*
   2357  1.16.2.3    martin 	 * Acquire exlock to protect counters.
   2358  1.16.2.3    martin 	 * Does not use audio_exlock_enter() due to sc_dying.
   2359  1.16.2.2  christos 	 */
   2360  1.16.2.3    martin 	while (__predict_false(sc->sc_exlock != 0)) {
   2361  1.16.2.3    martin 		error = cv_timedwait_sig(&sc->sc_exlockcv, sc->sc_lock,
   2362  1.16.2.3    martin 		    mstohz(AUDIO_TIMEOUT));
   2363  1.16.2.3    martin 		/* XXX what should I do on error? */
   2364  1.16.2.3    martin 		if (error == EWOULDBLOCK) {
   2365  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2366  1.16.2.3    martin 			device_printf(sc->sc_dev,
   2367  1.16.2.3    martin 			    "%s: cv_timedwait_sig failed %d", __func__, error);
   2368  1.16.2.3    martin 			return error;
   2369  1.16.2.3    martin 		}
   2370  1.16.2.2  christos 	}
   2371  1.16.2.3    martin 	sc->sc_exlock = 1;
   2372  1.16.2.2  christos 
   2373  1.16.2.3    martin 	device_active(sc->sc_dev, DVA_SYSTEM);
   2374  1.16.2.3    martin 
   2375  1.16.2.3    martin 	mutex_enter(sc->sc_intr_lock);
   2376  1.16.2.3    martin 	SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
   2377  1.16.2.3    martin 	mutex_exit(sc->sc_intr_lock);
   2378  1.16.2.2  christos 
   2379  1.16.2.2  christos 	if (file->ptrack) {
   2380  1.16.2.3    martin 		TRACET(3, file->ptrack, "dropframes=%" PRIu64,
   2381  1.16.2.3    martin 		    file->ptrack->dropframes);
   2382  1.16.2.3    martin 
   2383  1.16.2.3    martin 		KASSERT(sc->sc_popens > 0);
   2384  1.16.2.3    martin 		sc->sc_popens--;
   2385  1.16.2.3    martin 
   2386  1.16.2.2  christos 		/* Call hw halt_output if this is the last playback track. */
   2387  1.16.2.3    martin 		if (sc->sc_popens == 0 && sc->sc_pbusy) {
   2388  1.16.2.2  christos 			error = audio_pmixer_halt(sc);
   2389  1.16.2.2  christos 			if (error) {
   2390  1.16.2.2  christos 				device_printf(sc->sc_dev,
   2391  1.16.2.3    martin 				    "halt_output failed with %d (ignored)\n",
   2392  1.16.2.3    martin 				    error);
   2393  1.16.2.2  christos 			}
   2394  1.16.2.2  christos 		}
   2395  1.16.2.2  christos 
   2396  1.16.2.3    martin 		/* Restore mixing volume if all tracks are gone. */
   2397  1.16.2.3    martin 		if (sc->sc_popens == 0) {
   2398  1.16.2.3    martin 			/* intr_lock is not necessary, but just manners. */
   2399  1.16.2.3    martin 			mutex_enter(sc->sc_intr_lock);
   2400  1.16.2.3    martin 			sc->sc_pmixer->volume = 256;
   2401  1.16.2.3    martin 			sc->sc_pmixer->voltimer = 0;
   2402  1.16.2.3    martin 			mutex_exit(sc->sc_intr_lock);
   2403  1.16.2.3    martin 		}
   2404  1.16.2.2  christos 	}
   2405  1.16.2.2  christos 	if (file->rtrack) {
   2406  1.16.2.3    martin 		TRACET(3, file->rtrack, "dropframes=%" PRIu64,
   2407  1.16.2.3    martin 		    file->rtrack->dropframes);
   2408  1.16.2.3    martin 
   2409  1.16.2.3    martin 		KASSERT(sc->sc_ropens > 0);
   2410  1.16.2.3    martin 		sc->sc_ropens--;
   2411  1.16.2.3    martin 
   2412  1.16.2.2  christos 		/* Call hw halt_input if this is the last recording track. */
   2413  1.16.2.3    martin 		if (sc->sc_ropens == 0 && sc->sc_rbusy) {
   2414  1.16.2.2  christos 			error = audio_rmixer_halt(sc);
   2415  1.16.2.2  christos 			if (error) {
   2416  1.16.2.2  christos 				device_printf(sc->sc_dev,
   2417  1.16.2.3    martin 				    "halt_input failed with %d (ignored)\n",
   2418  1.16.2.3    martin 				    error);
   2419  1.16.2.2  christos 			}
   2420  1.16.2.2  christos 		}
   2421  1.16.2.2  christos 
   2422  1.16.2.2  christos 	}
   2423  1.16.2.2  christos 
   2424  1.16.2.2  christos 	/* Call hw close if this is the last track. */
   2425  1.16.2.2  christos 	if (sc->sc_popens + sc->sc_ropens == 0) {
   2426  1.16.2.2  christos 		if (sc->hw_if->close) {
   2427  1.16.2.2  christos 			TRACE(2, "hw_if close");
   2428  1.16.2.2  christos 			mutex_enter(sc->sc_intr_lock);
   2429  1.16.2.2  christos 			sc->hw_if->close(sc->hw_hdl);
   2430  1.16.2.2  christos 			mutex_exit(sc->sc_intr_lock);
   2431  1.16.2.2  christos 		}
   2432  1.16.2.2  christos 	}
   2433  1.16.2.2  christos 
   2434  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   2435  1.16.2.3    martin 	if (sc->sc_popens + sc->sc_ropens == 0)
   2436  1.16.2.3    martin 		kauth_cred_free(sc->sc_cred);
   2437  1.16.2.2  christos 
   2438  1.16.2.2  christos 	TRACE(3, "done");
   2439  1.16.2.3    martin 	audio_exlock_exit(sc);
   2440  1.16.2.3    martin 
   2441  1.16.2.2  christos 	return 0;
   2442  1.16.2.2  christos }
   2443  1.16.2.2  christos 
   2444  1.16.2.3    martin /*
   2445  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   2446  1.16.2.3    martin  */
   2447  1.16.2.3    martin int
   2448  1.16.2.3    martin audio_read(struct audio_softc *sc, struct uio *uio, int ioflag,
   2449  1.16.2.3    martin 	audio_file_t *file)
   2450  1.16.2.3    martin {
   2451  1.16.2.3    martin 	audio_track_t *track;
   2452  1.16.2.3    martin 	audio_ring_t *usrbuf;
   2453  1.16.2.3    martin 	audio_ring_t *input;
   2454  1.16.2.3    martin 	int error;
   2455  1.16.2.2  christos 
   2456  1.16.2.2  christos 	/*
   2457  1.16.2.2  christos 	 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
   2458  1.16.2.2  christos 	 * However read() system call itself can be called because it's
   2459  1.16.2.2  christos 	 * opened with O_RDWR.  So in this case, deny this read().
   2460  1.16.2.2  christos 	 */
   2461  1.16.2.3    martin 	track = file->rtrack;
   2462  1.16.2.3    martin 	if (track == NULL) {
   2463  1.16.2.2  christos 		return EBADF;
   2464  1.16.2.2  christos 	}
   2465  1.16.2.2  christos 
   2466  1.16.2.3    martin 	/* I think it's better than EINVAL. */
   2467  1.16.2.3    martin 	if (track->mmapped)
   2468  1.16.2.3    martin 		return EPERM;
   2469  1.16.2.2  christos 
   2470  1.16.2.3    martin 	TRACET(2, track, "resid=%zd", uio->uio_resid);
   2471  1.16.2.2  christos 
   2472  1.16.2.3    martin #ifdef AUDIO_PM_IDLE
   2473  1.16.2.3    martin 	error = audio_exlock_mutex_enter(sc);
   2474  1.16.2.2  christos 	if (error)
   2475  1.16.2.2  christos 		return error;
   2476  1.16.2.2  christos 
   2477  1.16.2.3    martin 	if (device_is_active(&sc->sc_dev) || sc->sc_idle)
   2478  1.16.2.3    martin 		device_active(&sc->sc_dev, DVA_SYSTEM);
   2479  1.16.2.3    martin 
   2480  1.16.2.3    martin 	/* In recording, unlike playback, read() never operates rmixer. */
   2481  1.16.2.3    martin 
   2482  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   2483  1.16.2.3    martin #endif
   2484  1.16.2.3    martin 
   2485  1.16.2.3    martin 	usrbuf = &track->usrbuf;
   2486  1.16.2.3    martin 	input = track->input;
   2487  1.16.2.2  christos 	error = 0;
   2488  1.16.2.3    martin 
   2489  1.16.2.2  christos 	while (uio->uio_resid > 0 && error == 0) {
   2490  1.16.2.2  christos 		int bytes;
   2491  1.16.2.2  christos 
   2492  1.16.2.2  christos 		TRACET(3, track,
   2493  1.16.2.2  christos 		    "while resid=%zd input=%d/%d/%d usrbuf=%d/%d/H%d",
   2494  1.16.2.2  christos 		    uio->uio_resid,
   2495  1.16.2.2  christos 		    input->head, input->used, input->capacity,
   2496  1.16.2.2  christos 		    usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
   2497  1.16.2.2  christos 
   2498  1.16.2.2  christos 		/* Wait when buffers are empty. */
   2499  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   2500  1.16.2.2  christos 		for (;;) {
   2501  1.16.2.2  christos 			bool empty;
   2502  1.16.2.2  christos 			audio_track_lock_enter(track);
   2503  1.16.2.2  christos 			empty = (input->used == 0 && usrbuf->used == 0);
   2504  1.16.2.2  christos 			audio_track_lock_exit(track);
   2505  1.16.2.2  christos 			if (!empty)
   2506  1.16.2.2  christos 				break;
   2507  1.16.2.2  christos 
   2508  1.16.2.2  christos 			if ((ioflag & IO_NDELAY)) {
   2509  1.16.2.2  christos 				mutex_exit(sc->sc_lock);
   2510  1.16.2.2  christos 				return EWOULDBLOCK;
   2511  1.16.2.2  christos 			}
   2512  1.16.2.2  christos 
   2513  1.16.2.2  christos 			TRACET(3, track, "sleep");
   2514  1.16.2.2  christos 			error = audio_track_waitio(sc, track);
   2515  1.16.2.2  christos 			if (error) {
   2516  1.16.2.2  christos 				mutex_exit(sc->sc_lock);
   2517  1.16.2.2  christos 				return error;
   2518  1.16.2.2  christos 			}
   2519  1.16.2.2  christos 		}
   2520  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   2521  1.16.2.2  christos 
   2522  1.16.2.2  christos 		audio_track_lock_enter(track);
   2523  1.16.2.2  christos 		audio_track_record(track);
   2524  1.16.2.2  christos 
   2525  1.16.2.2  christos 		/* uiomove from usrbuf as much as possible. */
   2526  1.16.2.2  christos 		bytes = uimin(usrbuf->used, uio->uio_resid);
   2527  1.16.2.2  christos 		while (bytes > 0) {
   2528  1.16.2.2  christos 			int head = usrbuf->head;
   2529  1.16.2.2  christos 			int len = uimin(bytes, usrbuf->capacity - head);
   2530  1.16.2.2  christos 			error = uiomove((uint8_t *)usrbuf->mem + head, len,
   2531  1.16.2.2  christos 			    uio);
   2532  1.16.2.2  christos 			if (error) {
   2533  1.16.2.2  christos 				audio_track_lock_exit(track);
   2534  1.16.2.2  christos 				device_printf(sc->sc_dev,
   2535  1.16.2.2  christos 				    "uiomove(len=%d) failed with %d\n",
   2536  1.16.2.2  christos 				    len, error);
   2537  1.16.2.2  christos 				goto abort;
   2538  1.16.2.2  christos 			}
   2539  1.16.2.2  christos 			auring_take(usrbuf, len);
   2540  1.16.2.2  christos 			track->useriobytes += len;
   2541  1.16.2.2  christos 			TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
   2542  1.16.2.2  christos 			    len,
   2543  1.16.2.2  christos 			    usrbuf->head, usrbuf->used, usrbuf->capacity);
   2544  1.16.2.2  christos 			bytes -= len;
   2545  1.16.2.2  christos 		}
   2546  1.16.2.2  christos 
   2547  1.16.2.2  christos 		audio_track_lock_exit(track);
   2548  1.16.2.2  christos 	}
   2549  1.16.2.2  christos 
   2550  1.16.2.2  christos abort:
   2551  1.16.2.2  christos 	return error;
   2552  1.16.2.2  christos }
   2553  1.16.2.2  christos 
   2554  1.16.2.2  christos 
   2555  1.16.2.2  christos /*
   2556  1.16.2.2  christos  * Clear file's playback and/or record track buffer immediately.
   2557  1.16.2.2  christos  */
   2558  1.16.2.2  christos static void
   2559  1.16.2.2  christos audio_file_clear(struct audio_softc *sc, audio_file_t *file)
   2560  1.16.2.2  christos {
   2561  1.16.2.2  christos 
   2562  1.16.2.2  christos 	if (file->ptrack)
   2563  1.16.2.2  christos 		audio_track_clear(sc, file->ptrack);
   2564  1.16.2.2  christos 	if (file->rtrack)
   2565  1.16.2.2  christos 		audio_track_clear(sc, file->rtrack);
   2566  1.16.2.2  christos }
   2567  1.16.2.2  christos 
   2568  1.16.2.3    martin /*
   2569  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   2570  1.16.2.3    martin  */
   2571  1.16.2.2  christos int
   2572  1.16.2.2  christos audio_write(struct audio_softc *sc, struct uio *uio, int ioflag,
   2573  1.16.2.2  christos 	audio_file_t *file)
   2574  1.16.2.2  christos {
   2575  1.16.2.2  christos 	audio_track_t *track;
   2576  1.16.2.2  christos 	audio_ring_t *usrbuf;
   2577  1.16.2.2  christos 	audio_ring_t *outbuf;
   2578  1.16.2.2  christos 	int error;
   2579  1.16.2.2  christos 
   2580  1.16.2.2  christos 	track = file->ptrack;
   2581  1.16.2.2  christos 	KASSERT(track);
   2582  1.16.2.2  christos 
   2583  1.16.2.2  christos 	/* I think it's better than EINVAL. */
   2584  1.16.2.2  christos 	if (track->mmapped)
   2585  1.16.2.2  christos 		return EPERM;
   2586  1.16.2.2  christos 
   2587  1.16.2.3    martin 	TRACET(2, track, "%sresid=%zd pid=%d.%d ioflag=0x%x",
   2588  1.16.2.3    martin 	    audiodebug >= 3 ? "begin " : "",
   2589  1.16.2.3    martin 	    uio->uio_resid, (int)curproc->p_pid, (int)curlwp->l_lid, ioflag);
   2590  1.16.2.3    martin 
   2591  1.16.2.2  christos 	if (uio->uio_resid == 0) {
   2592  1.16.2.2  christos 		track->eofcounter++;
   2593  1.16.2.2  christos 		return 0;
   2594  1.16.2.2  christos 	}
   2595  1.16.2.2  christos 
   2596  1.16.2.3    martin 	error = audio_exlock_mutex_enter(sc);
   2597  1.16.2.3    martin 	if (error)
   2598  1.16.2.3    martin 		return error;
   2599  1.16.2.3    martin 
   2600  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   2601  1.16.2.2  christos 	if (device_is_active(&sc->sc_dev) || sc->sc_idle)
   2602  1.16.2.2  christos 		device_active(&sc->sc_dev, DVA_SYSTEM);
   2603  1.16.2.2  christos #endif
   2604  1.16.2.2  christos 
   2605  1.16.2.2  christos 	/*
   2606  1.16.2.2  christos 	 * The first write starts pmixer.
   2607  1.16.2.2  christos 	 */
   2608  1.16.2.2  christos 	if (sc->sc_pbusy == false)
   2609  1.16.2.2  christos 		audio_pmixer_start(sc, false);
   2610  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   2611  1.16.2.2  christos 
   2612  1.16.2.3    martin 	usrbuf = &track->usrbuf;
   2613  1.16.2.3    martin 	outbuf = &track->outbuf;
   2614  1.16.2.2  christos 	track->pstate = AUDIO_STATE_RUNNING;
   2615  1.16.2.2  christos 	error = 0;
   2616  1.16.2.3    martin 
   2617  1.16.2.2  christos 	while (uio->uio_resid > 0 && error == 0) {
   2618  1.16.2.2  christos 		int bytes;
   2619  1.16.2.2  christos 
   2620  1.16.2.2  christos 		TRACET(3, track, "while resid=%zd usrbuf=%d/%d/H%d",
   2621  1.16.2.2  christos 		    uio->uio_resid,
   2622  1.16.2.2  christos 		    usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
   2623  1.16.2.2  christos 
   2624  1.16.2.2  christos 		/* Wait when buffers are full. */
   2625  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   2626  1.16.2.2  christos 		for (;;) {
   2627  1.16.2.2  christos 			bool full;
   2628  1.16.2.2  christos 			audio_track_lock_enter(track);
   2629  1.16.2.2  christos 			full = (usrbuf->used >= track->usrbuf_usedhigh &&
   2630  1.16.2.2  christos 			    outbuf->used >= outbuf->capacity);
   2631  1.16.2.2  christos 			audio_track_lock_exit(track);
   2632  1.16.2.2  christos 			if (!full)
   2633  1.16.2.2  christos 				break;
   2634  1.16.2.2  christos 
   2635  1.16.2.2  christos 			if ((ioflag & IO_NDELAY)) {
   2636  1.16.2.2  christos 				error = EWOULDBLOCK;
   2637  1.16.2.2  christos 				mutex_exit(sc->sc_lock);
   2638  1.16.2.2  christos 				goto abort;
   2639  1.16.2.2  christos 			}
   2640  1.16.2.2  christos 
   2641  1.16.2.2  christos 			TRACET(3, track, "sleep usrbuf=%d/H%d",
   2642  1.16.2.2  christos 			    usrbuf->used, track->usrbuf_usedhigh);
   2643  1.16.2.2  christos 			error = audio_track_waitio(sc, track);
   2644  1.16.2.2  christos 			if (error) {
   2645  1.16.2.2  christos 				mutex_exit(sc->sc_lock);
   2646  1.16.2.2  christos 				goto abort;
   2647  1.16.2.2  christos 			}
   2648  1.16.2.2  christos 		}
   2649  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   2650  1.16.2.2  christos 
   2651  1.16.2.2  christos 		audio_track_lock_enter(track);
   2652  1.16.2.2  christos 
   2653  1.16.2.2  christos 		/* uiomove to usrbuf as much as possible. */
   2654  1.16.2.2  christos 		bytes = uimin(track->usrbuf_usedhigh - usrbuf->used,
   2655  1.16.2.2  christos 		    uio->uio_resid);
   2656  1.16.2.2  christos 		while (bytes > 0) {
   2657  1.16.2.2  christos 			int tail = auring_tail(usrbuf);
   2658  1.16.2.2  christos 			int len = uimin(bytes, usrbuf->capacity - tail);
   2659  1.16.2.2  christos 			error = uiomove((uint8_t *)usrbuf->mem + tail, len,
   2660  1.16.2.2  christos 			    uio);
   2661  1.16.2.2  christos 			if (error) {
   2662  1.16.2.2  christos 				audio_track_lock_exit(track);
   2663  1.16.2.2  christos 				device_printf(sc->sc_dev,
   2664  1.16.2.2  christos 				    "uiomove(len=%d) failed with %d\n",
   2665  1.16.2.2  christos 				    len, error);
   2666  1.16.2.2  christos 				goto abort;
   2667  1.16.2.2  christos 			}
   2668  1.16.2.2  christos 			auring_push(usrbuf, len);
   2669  1.16.2.2  christos 			track->useriobytes += len;
   2670  1.16.2.2  christos 			TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
   2671  1.16.2.2  christos 			    len,
   2672  1.16.2.2  christos 			    usrbuf->head, usrbuf->used, usrbuf->capacity);
   2673  1.16.2.2  christos 			bytes -= len;
   2674  1.16.2.2  christos 		}
   2675  1.16.2.2  christos 
   2676  1.16.2.2  christos 		/* Convert them as much as possible. */
   2677  1.16.2.2  christos 		while (usrbuf->used >= track->usrbuf_blksize &&
   2678  1.16.2.2  christos 		    outbuf->used < outbuf->capacity) {
   2679  1.16.2.2  christos 			audio_track_play(track);
   2680  1.16.2.2  christos 		}
   2681  1.16.2.2  christos 
   2682  1.16.2.2  christos 		audio_track_lock_exit(track);
   2683  1.16.2.2  christos 	}
   2684  1.16.2.2  christos 
   2685  1.16.2.2  christos abort:
   2686  1.16.2.2  christos 	TRACET(3, track, "done error=%d", error);
   2687  1.16.2.2  christos 	return error;
   2688  1.16.2.2  christos }
   2689  1.16.2.2  christos 
   2690  1.16.2.3    martin /*
   2691  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   2692  1.16.2.3    martin  */
   2693  1.16.2.2  christos int
   2694  1.16.2.2  christos audio_ioctl(dev_t dev, struct audio_softc *sc, u_long cmd, void *addr, int flag,
   2695  1.16.2.2  christos 	struct lwp *l, audio_file_t *file)
   2696  1.16.2.2  christos {
   2697  1.16.2.2  christos 	struct audio_offset *ao;
   2698  1.16.2.2  christos 	struct audio_info ai;
   2699  1.16.2.2  christos 	audio_track_t *track;
   2700  1.16.2.2  christos 	audio_encoding_t *ae;
   2701  1.16.2.2  christos 	audio_format_query_t *query;
   2702  1.16.2.2  christos 	u_int stamp;
   2703  1.16.2.2  christos 	u_int offs;
   2704  1.16.2.2  christos 	int fd;
   2705  1.16.2.2  christos 	int index;
   2706  1.16.2.2  christos 	int error;
   2707  1.16.2.2  christos 
   2708  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   2709  1.16.2.2  christos 	const char *ioctlnames[] = {
   2710  1.16.2.2  christos 		" AUDIO_GETINFO",	/* 21 */
   2711  1.16.2.2  christos 		" AUDIO_SETINFO",	/* 22 */
   2712  1.16.2.2  christos 		" AUDIO_DRAIN",		/* 23 */
   2713  1.16.2.2  christos 		" AUDIO_FLUSH",		/* 24 */
   2714  1.16.2.2  christos 		" AUDIO_WSEEK",		/* 25 */
   2715  1.16.2.2  christos 		" AUDIO_RERROR",	/* 26 */
   2716  1.16.2.2  christos 		" AUDIO_GETDEV",	/* 27 */
   2717  1.16.2.2  christos 		" AUDIO_GETENC",	/* 28 */
   2718  1.16.2.2  christos 		" AUDIO_GETFD",		/* 29 */
   2719  1.16.2.2  christos 		" AUDIO_SETFD",		/* 30 */
   2720  1.16.2.2  christos 		" AUDIO_PERROR",	/* 31 */
   2721  1.16.2.2  christos 		" AUDIO_GETIOFFS",	/* 32 */
   2722  1.16.2.2  christos 		" AUDIO_GETOOFFS",	/* 33 */
   2723  1.16.2.2  christos 		" AUDIO_GETPROPS",	/* 34 */
   2724  1.16.2.2  christos 		" AUDIO_GETBUFINFO",	/* 35 */
   2725  1.16.2.2  christos 		" AUDIO_SETCHAN",	/* 36 */
   2726  1.16.2.2  christos 		" AUDIO_GETCHAN",	/* 37 */
   2727  1.16.2.2  christos 		" AUDIO_QUERYFORMAT",	/* 38 */
   2728  1.16.2.2  christos 		" AUDIO_GETFORMAT",	/* 39 */
   2729  1.16.2.2  christos 		" AUDIO_SETFORMAT",	/* 40 */
   2730  1.16.2.2  christos 	};
   2731  1.16.2.2  christos 	int nameidx = (cmd & 0xff);
   2732  1.16.2.2  christos 	const char *ioctlname = "";
   2733  1.16.2.2  christos 	if (21 <= nameidx && nameidx <= 21 + __arraycount(ioctlnames))
   2734  1.16.2.2  christos 		ioctlname = ioctlnames[nameidx - 21];
   2735  1.16.2.2  christos 	TRACEF(2, file, "(%lu,'%c',%lu)%s pid=%d.%d",
   2736  1.16.2.2  christos 	    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, ioctlname,
   2737  1.16.2.2  christos 	    (int)curproc->p_pid, (int)l->l_lid);
   2738  1.16.2.2  christos #endif
   2739  1.16.2.2  christos 
   2740  1.16.2.2  christos 	error = 0;
   2741  1.16.2.2  christos 	switch (cmd) {
   2742  1.16.2.2  christos 	case FIONBIO:
   2743  1.16.2.2  christos 		/* All handled in the upper FS layer. */
   2744  1.16.2.2  christos 		break;
   2745  1.16.2.2  christos 
   2746  1.16.2.2  christos 	case FIONREAD:
   2747  1.16.2.2  christos 		/* Get the number of bytes that can be read. */
   2748  1.16.2.2  christos 		if (file->rtrack) {
   2749  1.16.2.2  christos 			*(int *)addr = audio_track_readablebytes(file->rtrack);
   2750  1.16.2.2  christos 		} else {
   2751  1.16.2.2  christos 			*(int *)addr = 0;
   2752  1.16.2.2  christos 		}
   2753  1.16.2.2  christos 		break;
   2754  1.16.2.2  christos 
   2755  1.16.2.2  christos 	case FIOASYNC:
   2756  1.16.2.2  christos 		/* Set/Clear ASYNC I/O. */
   2757  1.16.2.2  christos 		if (*(int *)addr) {
   2758  1.16.2.2  christos 			file->async_audio = curproc->p_pid;
   2759  1.16.2.2  christos 			TRACEF(2, file, "FIOASYNC pid %d", file->async_audio);
   2760  1.16.2.2  christos 		} else {
   2761  1.16.2.2  christos 			file->async_audio = 0;
   2762  1.16.2.2  christos 			TRACEF(2, file, "FIOASYNC off");
   2763  1.16.2.2  christos 		}
   2764  1.16.2.2  christos 		break;
   2765  1.16.2.2  christos 
   2766  1.16.2.2  christos 	case AUDIO_FLUSH:
   2767  1.16.2.2  christos 		/* XXX TODO: clear errors and restart? */
   2768  1.16.2.2  christos 		audio_file_clear(sc, file);
   2769  1.16.2.2  christos 		break;
   2770  1.16.2.2  christos 
   2771  1.16.2.2  christos 	case AUDIO_RERROR:
   2772  1.16.2.2  christos 		/*
   2773  1.16.2.2  christos 		 * Number of read bytes dropped.  We don't know where
   2774  1.16.2.2  christos 		 * or when they were dropped (including conversion stage).
   2775  1.16.2.2  christos 		 * Therefore, the number of accurate bytes or samples is
   2776  1.16.2.2  christos 		 * also unknown.
   2777  1.16.2.2  christos 		 */
   2778  1.16.2.2  christos 		track = file->rtrack;
   2779  1.16.2.2  christos 		if (track) {
   2780  1.16.2.2  christos 			*(int *)addr = frametobyte(&track->usrbuf.fmt,
   2781  1.16.2.2  christos 			    track->dropframes);
   2782  1.16.2.2  christos 		}
   2783  1.16.2.2  christos 		break;
   2784  1.16.2.2  christos 
   2785  1.16.2.2  christos 	case AUDIO_PERROR:
   2786  1.16.2.2  christos 		/*
   2787  1.16.2.2  christos 		 * Number of write bytes dropped.  We don't know where
   2788  1.16.2.2  christos 		 * or when they were dropped (including conversion stage).
   2789  1.16.2.2  christos 		 * Therefore, the number of accurate bytes or samples is
   2790  1.16.2.2  christos 		 * also unknown.
   2791  1.16.2.2  christos 		 */
   2792  1.16.2.2  christos 		track = file->ptrack;
   2793  1.16.2.2  christos 		if (track) {
   2794  1.16.2.2  christos 			*(int *)addr = frametobyte(&track->usrbuf.fmt,
   2795  1.16.2.2  christos 			    track->dropframes);
   2796  1.16.2.2  christos 		}
   2797  1.16.2.2  christos 		break;
   2798  1.16.2.2  christos 
   2799  1.16.2.2  christos 	case AUDIO_GETIOFFS:
   2800  1.16.2.2  christos 		/* XXX TODO */
   2801  1.16.2.2  christos 		ao = (struct audio_offset *)addr;
   2802  1.16.2.2  christos 		ao->samples = 0;
   2803  1.16.2.2  christos 		ao->deltablks = 0;
   2804  1.16.2.2  christos 		ao->offset = 0;
   2805  1.16.2.2  christos 		break;
   2806  1.16.2.2  christos 
   2807  1.16.2.2  christos 	case AUDIO_GETOOFFS:
   2808  1.16.2.2  christos 		ao = (struct audio_offset *)addr;
   2809  1.16.2.2  christos 		track = file->ptrack;
   2810  1.16.2.2  christos 		if (track == NULL) {
   2811  1.16.2.2  christos 			ao->samples = 0;
   2812  1.16.2.2  christos 			ao->deltablks = 0;
   2813  1.16.2.2  christos 			ao->offset = 0;
   2814  1.16.2.2  christos 			break;
   2815  1.16.2.2  christos 		}
   2816  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   2817  1.16.2.2  christos 		mutex_enter(sc->sc_intr_lock);
   2818  1.16.2.2  christos 		/* figure out where next DMA will start */
   2819  1.16.2.2  christos 		stamp = track->usrbuf_stamp;
   2820  1.16.2.2  christos 		offs = track->usrbuf.head;
   2821  1.16.2.2  christos 		mutex_exit(sc->sc_intr_lock);
   2822  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   2823  1.16.2.2  christos 
   2824  1.16.2.2  christos 		ao->samples = stamp;
   2825  1.16.2.2  christos 		ao->deltablks = (stamp / track->usrbuf_blksize) -
   2826  1.16.2.2  christos 		    (track->usrbuf_stamp_last / track->usrbuf_blksize);
   2827  1.16.2.2  christos 		track->usrbuf_stamp_last = stamp;
   2828  1.16.2.2  christos 		offs = rounddown(offs, track->usrbuf_blksize)
   2829  1.16.2.2  christos 		    + track->usrbuf_blksize;
   2830  1.16.2.2  christos 		if (offs >= track->usrbuf.capacity)
   2831  1.16.2.2  christos 			offs -= track->usrbuf.capacity;
   2832  1.16.2.2  christos 		ao->offset = offs;
   2833  1.16.2.2  christos 
   2834  1.16.2.2  christos 		TRACET(3, track, "GETOOFFS: samples=%u deltablks=%u offset=%u",
   2835  1.16.2.2  christos 		    ao->samples, ao->deltablks, ao->offset);
   2836  1.16.2.2  christos 		break;
   2837  1.16.2.2  christos 
   2838  1.16.2.2  christos 	case AUDIO_WSEEK:
   2839  1.16.2.2  christos 		/* XXX return value does not include outbuf one. */
   2840  1.16.2.2  christos 		if (file->ptrack)
   2841  1.16.2.2  christos 			*(u_long *)addr = file->ptrack->usrbuf.used;
   2842  1.16.2.2  christos 		break;
   2843  1.16.2.2  christos 
   2844  1.16.2.2  christos 	case AUDIO_SETINFO:
   2845  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2846  1.16.2.2  christos 		if (error)
   2847  1.16.2.2  christos 			break;
   2848  1.16.2.2  christos 		error = audio_file_setinfo(sc, file, (struct audio_info *)addr);
   2849  1.16.2.2  christos 		if (error) {
   2850  1.16.2.3    martin 			audio_exlock_exit(sc);
   2851  1.16.2.2  christos 			break;
   2852  1.16.2.2  christos 		}
   2853  1.16.2.2  christos 		/* XXX TODO: update last_ai if /dev/sound ? */
   2854  1.16.2.2  christos 		if (ISDEVSOUND(dev))
   2855  1.16.2.2  christos 			error = audiogetinfo(sc, &sc->sc_ai, 0, file);
   2856  1.16.2.3    martin 		audio_exlock_exit(sc);
   2857  1.16.2.2  christos 		break;
   2858  1.16.2.2  christos 
   2859  1.16.2.2  christos 	case AUDIO_GETINFO:
   2860  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2861  1.16.2.2  christos 		if (error)
   2862  1.16.2.2  christos 			break;
   2863  1.16.2.2  christos 		error = audiogetinfo(sc, (struct audio_info *)addr, 1, file);
   2864  1.16.2.3    martin 		audio_exlock_exit(sc);
   2865  1.16.2.2  christos 		break;
   2866  1.16.2.2  christos 
   2867  1.16.2.2  christos 	case AUDIO_GETBUFINFO:
   2868  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2869  1.16.2.3    martin 		if (error)
   2870  1.16.2.3    martin 			break;
   2871  1.16.2.2  christos 		error = audiogetinfo(sc, (struct audio_info *)addr, 0, file);
   2872  1.16.2.3    martin 		audio_exlock_exit(sc);
   2873  1.16.2.2  christos 		break;
   2874  1.16.2.2  christos 
   2875  1.16.2.2  christos 	case AUDIO_DRAIN:
   2876  1.16.2.2  christos 		if (file->ptrack) {
   2877  1.16.2.2  christos 			mutex_enter(sc->sc_lock);
   2878  1.16.2.2  christos 			error = audio_track_drain(sc, file->ptrack);
   2879  1.16.2.2  christos 			mutex_exit(sc->sc_lock);
   2880  1.16.2.2  christos 		}
   2881  1.16.2.2  christos 		break;
   2882  1.16.2.2  christos 
   2883  1.16.2.2  christos 	case AUDIO_GETDEV:
   2884  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   2885  1.16.2.2  christos 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
   2886  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   2887  1.16.2.2  christos 		break;
   2888  1.16.2.2  christos 
   2889  1.16.2.2  christos 	case AUDIO_GETENC:
   2890  1.16.2.2  christos 		ae = (audio_encoding_t *)addr;
   2891  1.16.2.2  christos 		index = ae->index;
   2892  1.16.2.2  christos 		if (index < 0 || index >= __arraycount(audio_encodings)) {
   2893  1.16.2.2  christos 			error = EINVAL;
   2894  1.16.2.2  christos 			break;
   2895  1.16.2.2  christos 		}
   2896  1.16.2.2  christos 		*ae = audio_encodings[index];
   2897  1.16.2.2  christos 		ae->index = index;
   2898  1.16.2.2  christos 		/*
   2899  1.16.2.2  christos 		 * EMULATED always.
   2900  1.16.2.2  christos 		 * EMULATED flag at that time used to mean that it could
   2901  1.16.2.2  christos 		 * not be passed directly to the hardware as-is.  But
   2902  1.16.2.2  christos 		 * currently, all formats including hardware native is not
   2903  1.16.2.2  christos 		 * passed directly to the hardware.  So I set EMULATED
   2904  1.16.2.2  christos 		 * flag for all formats.
   2905  1.16.2.2  christos 		 */
   2906  1.16.2.2  christos 		ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
   2907  1.16.2.2  christos 		break;
   2908  1.16.2.2  christos 
   2909  1.16.2.2  christos 	case AUDIO_GETFD:
   2910  1.16.2.2  christos 		/*
   2911  1.16.2.2  christos 		 * Returns the current setting of full duplex mode.
   2912  1.16.2.2  christos 		 * If HW has full duplex mode and there are two mixers,
   2913  1.16.2.2  christos 		 * it is full duplex.  Otherwise half duplex.
   2914  1.16.2.2  christos 		 */
   2915  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2916  1.16.2.3    martin 		if (error)
   2917  1.16.2.3    martin 			break;
   2918  1.16.2.3    martin 		fd = (sc->sc_props & AUDIO_PROP_FULLDUPLEX)
   2919  1.16.2.2  christos 		    && (sc->sc_pmixer && sc->sc_rmixer);
   2920  1.16.2.3    martin 		audio_exlock_exit(sc);
   2921  1.16.2.2  christos 		*(int *)addr = fd;
   2922  1.16.2.2  christos 		break;
   2923  1.16.2.2  christos 
   2924  1.16.2.2  christos 	case AUDIO_GETPROPS:
   2925  1.16.2.3    martin 		*(int *)addr = sc->sc_props;
   2926  1.16.2.2  christos 		break;
   2927  1.16.2.2  christos 
   2928  1.16.2.2  christos 	case AUDIO_QUERYFORMAT:
   2929  1.16.2.2  christos 		query = (audio_format_query_t *)addr;
   2930  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   2931  1.16.2.3    martin 		error = sc->hw_if->query_format(sc->hw_hdl, query);
   2932  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   2933  1.16.2.3    martin 		/* Hide internal infomations */
   2934  1.16.2.3    martin 		query->fmt.driver_data = NULL;
   2935  1.16.2.2  christos 		break;
   2936  1.16.2.2  christos 
   2937  1.16.2.2  christos 	case AUDIO_GETFORMAT:
   2938  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2939  1.16.2.3    martin 		if (error)
   2940  1.16.2.3    martin 			break;
   2941  1.16.2.2  christos 		audio_mixers_get_format(sc, (struct audio_info *)addr);
   2942  1.16.2.3    martin 		audio_exlock_exit(sc);
   2943  1.16.2.2  christos 		break;
   2944  1.16.2.2  christos 
   2945  1.16.2.2  christos 	case AUDIO_SETFORMAT:
   2946  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   2947  1.16.2.2  christos 		audio_mixers_get_format(sc, &ai);
   2948  1.16.2.2  christos 		error = audio_mixers_set_format(sc, (struct audio_info *)addr);
   2949  1.16.2.2  christos 		if (error) {
   2950  1.16.2.2  christos 			/* Rollback */
   2951  1.16.2.2  christos 			audio_mixers_set_format(sc, &ai);
   2952  1.16.2.2  christos 		}
   2953  1.16.2.3    martin 		audio_exlock_exit(sc);
   2954  1.16.2.2  christos 		break;
   2955  1.16.2.2  christos 
   2956  1.16.2.2  christos 	case AUDIO_SETFD:
   2957  1.16.2.2  christos 	case AUDIO_SETCHAN:
   2958  1.16.2.2  christos 	case AUDIO_GETCHAN:
   2959  1.16.2.2  christos 		/* Obsoleted */
   2960  1.16.2.2  christos 		break;
   2961  1.16.2.2  christos 
   2962  1.16.2.2  christos 	default:
   2963  1.16.2.2  christos 		if (sc->hw_if->dev_ioctl) {
   2964  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   2965  1.16.2.2  christos 			error = sc->hw_if->dev_ioctl(sc->hw_hdl,
   2966  1.16.2.2  christos 			    cmd, addr, flag, l);
   2967  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   2968  1.16.2.2  christos 		} else {
   2969  1.16.2.2  christos 			TRACEF(2, file, "unknown ioctl");
   2970  1.16.2.2  christos 			error = EINVAL;
   2971  1.16.2.2  christos 		}
   2972  1.16.2.2  christos 		break;
   2973  1.16.2.2  christos 	}
   2974  1.16.2.2  christos 	TRACEF(2, file, "(%lu,'%c',%lu)%s result %d",
   2975  1.16.2.2  christos 	    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, ioctlname,
   2976  1.16.2.2  christos 	    error);
   2977  1.16.2.2  christos 	return error;
   2978  1.16.2.2  christos }
   2979  1.16.2.2  christos 
   2980  1.16.2.2  christos /*
   2981  1.16.2.2  christos  * Returns the number of bytes that can be read on recording buffer.
   2982  1.16.2.2  christos  */
   2983  1.16.2.2  christos static __inline int
   2984  1.16.2.2  christos audio_track_readablebytes(const audio_track_t *track)
   2985  1.16.2.2  christos {
   2986  1.16.2.2  christos 	int bytes;
   2987  1.16.2.2  christos 
   2988  1.16.2.2  christos 	KASSERT(track);
   2989  1.16.2.2  christos 	KASSERT(track->mode == AUMODE_RECORD);
   2990  1.16.2.2  christos 
   2991  1.16.2.2  christos 	/*
   2992  1.16.2.2  christos 	 * Although usrbuf is primarily readable data, recorded data
   2993  1.16.2.2  christos 	 * also stays in track->input until reading.  So it is necessary
   2994  1.16.2.2  christos 	 * to add it.  track->input is in frame, usrbuf is in byte.
   2995  1.16.2.2  christos 	 */
   2996  1.16.2.2  christos 	bytes = track->usrbuf.used +
   2997  1.16.2.2  christos 	    track->input->used * frametobyte(&track->usrbuf.fmt, 1);
   2998  1.16.2.2  christos 	return bytes;
   2999  1.16.2.2  christos }
   3000  1.16.2.2  christos 
   3001  1.16.2.3    martin /*
   3002  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   3003  1.16.2.3    martin  */
   3004  1.16.2.2  christos int
   3005  1.16.2.2  christos audio_poll(struct audio_softc *sc, int events, struct lwp *l,
   3006  1.16.2.2  christos 	audio_file_t *file)
   3007  1.16.2.2  christos {
   3008  1.16.2.2  christos 	audio_track_t *track;
   3009  1.16.2.2  christos 	int revents;
   3010  1.16.2.2  christos 	bool in_is_valid;
   3011  1.16.2.2  christos 	bool out_is_valid;
   3012  1.16.2.2  christos 
   3013  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   3014  1.16.2.2  christos #define POLLEV_BITMAP "\177\020" \
   3015  1.16.2.2  christos 	    "b\10WRBAND\0" \
   3016  1.16.2.2  christos 	    "b\7RDBAND\0" "b\6RDNORM\0" "b\5NVAL\0" "b\4HUP\0" \
   3017  1.16.2.2  christos 	    "b\3ERR\0" "b\2OUT\0" "b\1PRI\0" "b\0IN\0"
   3018  1.16.2.2  christos 	char evbuf[64];
   3019  1.16.2.2  christos 	snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, events);
   3020  1.16.2.2  christos 	TRACEF(2, file, "pid=%d.%d events=%s",
   3021  1.16.2.2  christos 	    (int)curproc->p_pid, (int)l->l_lid, evbuf);
   3022  1.16.2.2  christos #endif
   3023  1.16.2.2  christos 
   3024  1.16.2.2  christos 	revents = 0;
   3025  1.16.2.2  christos 	in_is_valid = false;
   3026  1.16.2.2  christos 	out_is_valid = false;
   3027  1.16.2.2  christos 	if (events & (POLLIN | POLLRDNORM)) {
   3028  1.16.2.2  christos 		track = file->rtrack;
   3029  1.16.2.2  christos 		if (track) {
   3030  1.16.2.2  christos 			int used;
   3031  1.16.2.2  christos 			in_is_valid = true;
   3032  1.16.2.2  christos 			used = audio_track_readablebytes(track);
   3033  1.16.2.2  christos 			if (used > 0)
   3034  1.16.2.2  christos 				revents |= events & (POLLIN | POLLRDNORM);
   3035  1.16.2.2  christos 		}
   3036  1.16.2.2  christos 	}
   3037  1.16.2.2  christos 	if (events & (POLLOUT | POLLWRNORM)) {
   3038  1.16.2.2  christos 		track = file->ptrack;
   3039  1.16.2.2  christos 		if (track) {
   3040  1.16.2.2  christos 			out_is_valid = true;
   3041  1.16.2.2  christos 			if (track->usrbuf.used <= track->usrbuf_usedlow)
   3042  1.16.2.2  christos 				revents |= events & (POLLOUT | POLLWRNORM);
   3043  1.16.2.2  christos 		}
   3044  1.16.2.2  christos 	}
   3045  1.16.2.2  christos 
   3046  1.16.2.2  christos 	if (revents == 0) {
   3047  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   3048  1.16.2.2  christos 		if (in_is_valid) {
   3049  1.16.2.2  christos 			TRACEF(3, file, "selrecord rsel");
   3050  1.16.2.2  christos 			selrecord(l, &sc->sc_rsel);
   3051  1.16.2.2  christos 		}
   3052  1.16.2.2  christos 		if (out_is_valid) {
   3053  1.16.2.2  christos 			TRACEF(3, file, "selrecord wsel");
   3054  1.16.2.2  christos 			selrecord(l, &sc->sc_wsel);
   3055  1.16.2.2  christos 		}
   3056  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   3057  1.16.2.2  christos 	}
   3058  1.16.2.2  christos 
   3059  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   3060  1.16.2.2  christos 	snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, revents);
   3061  1.16.2.2  christos 	TRACEF(2, file, "revents=%s", evbuf);
   3062  1.16.2.2  christos #endif
   3063  1.16.2.2  christos 	return revents;
   3064  1.16.2.2  christos }
   3065  1.16.2.2  christos 
   3066  1.16.2.2  christos static const struct filterops audioread_filtops = {
   3067  1.16.2.2  christos 	.f_isfd = 1,
   3068  1.16.2.2  christos 	.f_attach = NULL,
   3069  1.16.2.2  christos 	.f_detach = filt_audioread_detach,
   3070  1.16.2.2  christos 	.f_event = filt_audioread_event,
   3071  1.16.2.2  christos };
   3072  1.16.2.2  christos 
   3073  1.16.2.2  christos static void
   3074  1.16.2.2  christos filt_audioread_detach(struct knote *kn)
   3075  1.16.2.2  christos {
   3076  1.16.2.2  christos 	struct audio_softc *sc;
   3077  1.16.2.2  christos 	audio_file_t *file;
   3078  1.16.2.2  christos 
   3079  1.16.2.2  christos 	file = kn->kn_hook;
   3080  1.16.2.2  christos 	sc = file->sc;
   3081  1.16.2.2  christos 	TRACEF(3, file, "");
   3082  1.16.2.2  christos 
   3083  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   3084  1.16.2.2  christos 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
   3085  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   3086  1.16.2.2  christos }
   3087  1.16.2.2  christos 
   3088  1.16.2.2  christos static int
   3089  1.16.2.2  christos filt_audioread_event(struct knote *kn, long hint)
   3090  1.16.2.2  christos {
   3091  1.16.2.2  christos 	audio_file_t *file;
   3092  1.16.2.2  christos 	audio_track_t *track;
   3093  1.16.2.2  christos 
   3094  1.16.2.2  christos 	file = kn->kn_hook;
   3095  1.16.2.2  christos 	track = file->rtrack;
   3096  1.16.2.2  christos 
   3097  1.16.2.2  christos 	/*
   3098  1.16.2.2  christos 	 * kn_data must contain the number of bytes can be read.
   3099  1.16.2.2  christos 	 * The return value indicates whether the event occurs or not.
   3100  1.16.2.2  christos 	 */
   3101  1.16.2.2  christos 
   3102  1.16.2.2  christos 	if (track == NULL) {
   3103  1.16.2.2  christos 		/* can not read with this descriptor. */
   3104  1.16.2.2  christos 		kn->kn_data = 0;
   3105  1.16.2.2  christos 		return 0;
   3106  1.16.2.2  christos 	}
   3107  1.16.2.2  christos 
   3108  1.16.2.2  christos 	kn->kn_data = audio_track_readablebytes(track);
   3109  1.16.2.2  christos 	TRACEF(3, file, "data=%" PRId64, kn->kn_data);
   3110  1.16.2.2  christos 	return kn->kn_data > 0;
   3111  1.16.2.2  christos }
   3112  1.16.2.2  christos 
   3113  1.16.2.2  christos static const struct filterops audiowrite_filtops = {
   3114  1.16.2.2  christos 	.f_isfd = 1,
   3115  1.16.2.2  christos 	.f_attach = NULL,
   3116  1.16.2.2  christos 	.f_detach = filt_audiowrite_detach,
   3117  1.16.2.2  christos 	.f_event = filt_audiowrite_event,
   3118  1.16.2.2  christos };
   3119  1.16.2.2  christos 
   3120  1.16.2.2  christos static void
   3121  1.16.2.2  christos filt_audiowrite_detach(struct knote *kn)
   3122  1.16.2.2  christos {
   3123  1.16.2.2  christos 	struct audio_softc *sc;
   3124  1.16.2.2  christos 	audio_file_t *file;
   3125  1.16.2.2  christos 
   3126  1.16.2.2  christos 	file = kn->kn_hook;
   3127  1.16.2.2  christos 	sc = file->sc;
   3128  1.16.2.2  christos 	TRACEF(3, file, "");
   3129  1.16.2.2  christos 
   3130  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   3131  1.16.2.2  christos 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
   3132  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   3133  1.16.2.2  christos }
   3134  1.16.2.2  christos 
   3135  1.16.2.2  christos static int
   3136  1.16.2.2  christos filt_audiowrite_event(struct knote *kn, long hint)
   3137  1.16.2.2  christos {
   3138  1.16.2.2  christos 	audio_file_t *file;
   3139  1.16.2.2  christos 	audio_track_t *track;
   3140  1.16.2.2  christos 
   3141  1.16.2.2  christos 	file = kn->kn_hook;
   3142  1.16.2.2  christos 	track = file->ptrack;
   3143  1.16.2.2  christos 
   3144  1.16.2.2  christos 	/*
   3145  1.16.2.2  christos 	 * kn_data must contain the number of bytes can be write.
   3146  1.16.2.2  christos 	 * The return value indicates whether the event occurs or not.
   3147  1.16.2.2  christos 	 */
   3148  1.16.2.2  christos 
   3149  1.16.2.2  christos 	if (track == NULL) {
   3150  1.16.2.2  christos 		/* can not write with this descriptor. */
   3151  1.16.2.2  christos 		kn->kn_data = 0;
   3152  1.16.2.2  christos 		return 0;
   3153  1.16.2.2  christos 	}
   3154  1.16.2.2  christos 
   3155  1.16.2.2  christos 	kn->kn_data = track->usrbuf_usedhigh - track->usrbuf.used;
   3156  1.16.2.2  christos 	TRACEF(3, file, "data=%" PRId64, kn->kn_data);
   3157  1.16.2.2  christos 	return (track->usrbuf.used < track->usrbuf_usedlow);
   3158  1.16.2.2  christos }
   3159  1.16.2.2  christos 
   3160  1.16.2.3    martin /*
   3161  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   3162  1.16.2.3    martin  */
   3163  1.16.2.2  christos int
   3164  1.16.2.2  christos audio_kqfilter(struct audio_softc *sc, audio_file_t *file, struct knote *kn)
   3165  1.16.2.2  christos {
   3166  1.16.2.2  christos 	struct klist *klist;
   3167  1.16.2.2  christos 
   3168  1.16.2.2  christos 	TRACEF(3, file, "kn=%p kn_filter=%x", kn, (int)kn->kn_filter);
   3169  1.16.2.2  christos 
   3170  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   3171  1.16.2.2  christos 	switch (kn->kn_filter) {
   3172  1.16.2.2  christos 	case EVFILT_READ:
   3173  1.16.2.2  christos 		klist = &sc->sc_rsel.sel_klist;
   3174  1.16.2.2  christos 		kn->kn_fop = &audioread_filtops;
   3175  1.16.2.2  christos 		break;
   3176  1.16.2.2  christos 
   3177  1.16.2.2  christos 	case EVFILT_WRITE:
   3178  1.16.2.2  christos 		klist = &sc->sc_wsel.sel_klist;
   3179  1.16.2.2  christos 		kn->kn_fop = &audiowrite_filtops;
   3180  1.16.2.2  christos 		break;
   3181  1.16.2.2  christos 
   3182  1.16.2.2  christos 	default:
   3183  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   3184  1.16.2.2  christos 		return EINVAL;
   3185  1.16.2.2  christos 	}
   3186  1.16.2.2  christos 
   3187  1.16.2.2  christos 	kn->kn_hook = file;
   3188  1.16.2.2  christos 
   3189  1.16.2.2  christos 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   3190  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   3191  1.16.2.2  christos 
   3192  1.16.2.2  christos 	return 0;
   3193  1.16.2.2  christos }
   3194  1.16.2.2  christos 
   3195  1.16.2.3    martin /*
   3196  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   3197  1.16.2.3    martin  */
   3198  1.16.2.2  christos int
   3199  1.16.2.2  christos audio_mmap(struct audio_softc *sc, off_t *offp, size_t len, int prot,
   3200  1.16.2.2  christos 	int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp,
   3201  1.16.2.2  christos 	audio_file_t *file)
   3202  1.16.2.2  christos {
   3203  1.16.2.2  christos 	audio_track_t *track;
   3204  1.16.2.2  christos 	vsize_t vsize;
   3205  1.16.2.2  christos 	int error;
   3206  1.16.2.2  christos 
   3207  1.16.2.2  christos 	TRACEF(2, file, "off=%lld, prot=%d", (long long)(*offp), prot);
   3208  1.16.2.2  christos 
   3209  1.16.2.2  christos 	if (*offp < 0)
   3210  1.16.2.2  christos 		return EINVAL;
   3211  1.16.2.2  christos 
   3212  1.16.2.2  christos #if 0
   3213  1.16.2.2  christos 	/* XXX
   3214  1.16.2.2  christos 	 * The idea here was to use the protection to determine if
   3215  1.16.2.2  christos 	 * we are mapping the read or write buffer, but it fails.
   3216  1.16.2.2  christos 	 * The VM system is broken in (at least) two ways.
   3217  1.16.2.2  christos 	 * 1) If you map memory VM_PROT_WRITE you SIGSEGV
   3218  1.16.2.2  christos 	 *    when writing to it, so VM_PROT_READ|VM_PROT_WRITE
   3219  1.16.2.2  christos 	 *    has to be used for mmapping the play buffer.
   3220  1.16.2.2  christos 	 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
   3221  1.16.2.2  christos 	 *    audio_mmap will get called at some point with VM_PROT_READ
   3222  1.16.2.2  christos 	 *    only.
   3223  1.16.2.2  christos 	 * So, alas, we always map the play buffer for now.
   3224  1.16.2.2  christos 	 */
   3225  1.16.2.2  christos 	if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
   3226  1.16.2.2  christos 	    prot == VM_PROT_WRITE)
   3227  1.16.2.2  christos 		track = file->ptrack;
   3228  1.16.2.2  christos 	else if (prot == VM_PROT_READ)
   3229  1.16.2.2  christos 		track = file->rtrack;
   3230  1.16.2.2  christos 	else
   3231  1.16.2.2  christos 		return EINVAL;
   3232  1.16.2.2  christos #else
   3233  1.16.2.2  christos 	track = file->ptrack;
   3234  1.16.2.2  christos #endif
   3235  1.16.2.2  christos 	if (track == NULL)
   3236  1.16.2.2  christos 		return EACCES;
   3237  1.16.2.2  christos 
   3238  1.16.2.2  christos 	vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
   3239  1.16.2.2  christos 	if (len > vsize)
   3240  1.16.2.2  christos 		return EOVERFLOW;
   3241  1.16.2.2  christos 	if (*offp > (uint)(vsize - len))
   3242  1.16.2.2  christos 		return EOVERFLOW;
   3243  1.16.2.2  christos 
   3244  1.16.2.2  christos 	/* XXX TODO: what happens when mmap twice. */
   3245  1.16.2.2  christos 	if (!track->mmapped) {
   3246  1.16.2.2  christos 		track->mmapped = true;
   3247  1.16.2.2  christos 
   3248  1.16.2.2  christos 		if (!track->is_pause) {
   3249  1.16.2.3    martin 			error = audio_exlock_mutex_enter(sc);
   3250  1.16.2.2  christos 			if (error)
   3251  1.16.2.2  christos 				return error;
   3252  1.16.2.2  christos 			if (sc->sc_pbusy == false)
   3253  1.16.2.2  christos 				audio_pmixer_start(sc, true);
   3254  1.16.2.3    martin 			audio_exlock_mutex_exit(sc);
   3255  1.16.2.2  christos 		}
   3256  1.16.2.2  christos 		/* XXX mmapping record buffer is not supported */
   3257  1.16.2.2  christos 	}
   3258  1.16.2.2  christos 
   3259  1.16.2.2  christos 	/* get ringbuffer */
   3260  1.16.2.2  christos 	*uobjp = track->uobj;
   3261  1.16.2.2  christos 
   3262  1.16.2.2  christos 	/* Acquire a reference for the mmap.  munmap will release. */
   3263  1.16.2.2  christos 	uao_reference(*uobjp);
   3264  1.16.2.2  christos 	*maxprotp = prot;
   3265  1.16.2.2  christos 	*advicep = UVM_ADV_RANDOM;
   3266  1.16.2.2  christos 	*flagsp = MAP_SHARED;
   3267  1.16.2.2  christos 	return 0;
   3268  1.16.2.2  christos }
   3269  1.16.2.2  christos 
   3270  1.16.2.2  christos /*
   3271  1.16.2.2  christos  * /dev/audioctl has to be able to open at any time without interference
   3272  1.16.2.2  christos  * with any /dev/audio or /dev/sound.
   3273  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   3274  1.16.2.2  christos  */
   3275  1.16.2.2  christos static int
   3276  1.16.2.2  christos audioctl_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   3277  1.16.2.2  christos 	struct lwp *l)
   3278  1.16.2.2  christos {
   3279  1.16.2.2  christos 	struct file *fp;
   3280  1.16.2.2  christos 	audio_file_t *af;
   3281  1.16.2.2  christos 	int fd;
   3282  1.16.2.2  christos 	int error;
   3283  1.16.2.2  christos 
   3284  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   3285  1.16.2.2  christos 
   3286  1.16.2.2  christos 	TRACE(1, "");
   3287  1.16.2.2  christos 
   3288  1.16.2.2  christos 	error = fd_allocfile(&fp, &fd);
   3289  1.16.2.2  christos 	if (error)
   3290  1.16.2.2  christos 		return error;
   3291  1.16.2.2  christos 
   3292  1.16.2.2  christos 	af = kmem_zalloc(sizeof(audio_file_t), KM_SLEEP);
   3293  1.16.2.2  christos 	af->sc = sc;
   3294  1.16.2.2  christos 	af->dev = dev;
   3295  1.16.2.2  christos 
   3296  1.16.2.2  christos 	/* Not necessary to insert sc_files. */
   3297  1.16.2.2  christos 
   3298  1.16.2.2  christos 	error = fd_clone(fp, fd, flags, &audio_fileops, af);
   3299  1.16.2.3    martin 	KASSERTMSG(error == EMOVEFD, "error=%d", error);
   3300  1.16.2.2  christos 
   3301  1.16.2.2  christos 	return error;
   3302  1.16.2.2  christos }
   3303  1.16.2.2  christos 
   3304  1.16.2.2  christos /*
   3305  1.16.2.2  christos  * Free 'mem' if available, and initialize the pointer.
   3306  1.16.2.2  christos  * For this reason, this is implemented as macro.
   3307  1.16.2.2  christos  */
   3308  1.16.2.2  christos #define audio_free(mem)	do {	\
   3309  1.16.2.2  christos 	if (mem != NULL) {	\
   3310  1.16.2.2  christos 		kern_free(mem);	\
   3311  1.16.2.2  christos 		mem = NULL;	\
   3312  1.16.2.2  christos 	}	\
   3313  1.16.2.2  christos } while (0)
   3314  1.16.2.2  christos 
   3315  1.16.2.2  christos /*
   3316  1.16.2.3    martin  * (Re)allocate 'memblock' with specified 'bytes'.
   3317  1.16.2.3    martin  * bytes must not be 0.
   3318  1.16.2.3    martin  * This function never returns NULL.
   3319  1.16.2.3    martin  */
   3320  1.16.2.3    martin static void *
   3321  1.16.2.3    martin audio_realloc(void *memblock, size_t bytes)
   3322  1.16.2.3    martin {
   3323  1.16.2.3    martin 
   3324  1.16.2.3    martin 	KASSERT(bytes != 0);
   3325  1.16.2.3    martin 	audio_free(memblock);
   3326  1.16.2.3    martin 	return kern_malloc(bytes, M_WAITOK);
   3327  1.16.2.3    martin }
   3328  1.16.2.3    martin 
   3329  1.16.2.3    martin /*
   3330  1.16.2.2  christos  * (Re)allocate usrbuf with 'newbufsize' bytes.
   3331  1.16.2.2  christos  * Use this function for usrbuf because only usrbuf can be mmapped.
   3332  1.16.2.2  christos  * If successful, it updates track->usrbuf.mem, track->usrbuf.capacity and
   3333  1.16.2.2  christos  * returns 0.  Otherwise, it clears track->usrbuf.mem, track->usrbuf.capacity
   3334  1.16.2.2  christos  * and returns errno.
   3335  1.16.2.2  christos  * It must be called before updating usrbuf.capacity.
   3336  1.16.2.2  christos  */
   3337  1.16.2.2  christos static int
   3338  1.16.2.2  christos audio_realloc_usrbuf(audio_track_t *track, int newbufsize)
   3339  1.16.2.2  christos {
   3340  1.16.2.2  christos 	struct audio_softc *sc;
   3341  1.16.2.2  christos 	vaddr_t vstart;
   3342  1.16.2.2  christos 	vsize_t oldvsize;
   3343  1.16.2.2  christos 	vsize_t newvsize;
   3344  1.16.2.2  christos 	int error;
   3345  1.16.2.2  christos 
   3346  1.16.2.2  christos 	KASSERT(newbufsize > 0);
   3347  1.16.2.2  christos 	sc = track->mixer->sc;
   3348  1.16.2.2  christos 
   3349  1.16.2.2  christos 	/* Get a nonzero multiple of PAGE_SIZE */
   3350  1.16.2.2  christos 	newvsize = roundup2(MAX(newbufsize, PAGE_SIZE), PAGE_SIZE);
   3351  1.16.2.2  christos 
   3352  1.16.2.2  christos 	if (track->usrbuf.mem != NULL) {
   3353  1.16.2.2  christos 		oldvsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE),
   3354  1.16.2.2  christos 		    PAGE_SIZE);
   3355  1.16.2.2  christos 		if (oldvsize == newvsize) {
   3356  1.16.2.2  christos 			track->usrbuf.capacity = newbufsize;
   3357  1.16.2.2  christos 			return 0;
   3358  1.16.2.2  christos 		}
   3359  1.16.2.2  christos 		vstart = (vaddr_t)track->usrbuf.mem;
   3360  1.16.2.2  christos 		uvm_unmap(kernel_map, vstart, vstart + oldvsize);
   3361  1.16.2.2  christos 		/* uvm_unmap also detach uobj */
   3362  1.16.2.2  christos 		track->uobj = NULL;		/* paranoia */
   3363  1.16.2.2  christos 		track->usrbuf.mem = NULL;
   3364  1.16.2.2  christos 	}
   3365  1.16.2.2  christos 
   3366  1.16.2.2  christos 	/* Create a uvm anonymous object */
   3367  1.16.2.2  christos 	track->uobj = uao_create(newvsize, 0);
   3368  1.16.2.2  christos 
   3369  1.16.2.2  christos 	/* Map it into the kernel virtual address space */
   3370  1.16.2.2  christos 	vstart = 0;
   3371  1.16.2.2  christos 	error = uvm_map(kernel_map, &vstart, newvsize, track->uobj, 0, 0,
   3372  1.16.2.2  christos 	    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
   3373  1.16.2.2  christos 	    UVM_ADV_RANDOM, 0));
   3374  1.16.2.2  christos 	if (error) {
   3375  1.16.2.2  christos 		device_printf(sc->sc_dev, "uvm_map failed with %d\n", error);
   3376  1.16.2.2  christos 		uao_detach(track->uobj);	/* release reference */
   3377  1.16.2.2  christos 		goto abort;
   3378  1.16.2.2  christos 	}
   3379  1.16.2.2  christos 
   3380  1.16.2.2  christos 	error = uvm_map_pageable(kernel_map, vstart, vstart + newvsize,
   3381  1.16.2.2  christos 	    false, 0);
   3382  1.16.2.2  christos 	if (error) {
   3383  1.16.2.2  christos 		device_printf(sc->sc_dev, "uvm_map_pageable failed with %d\n",
   3384  1.16.2.2  christos 		    error);
   3385  1.16.2.2  christos 		uvm_unmap(kernel_map, vstart, vstart + newvsize);
   3386  1.16.2.2  christos 		/* uvm_unmap also detach uobj */
   3387  1.16.2.2  christos 		goto abort;
   3388  1.16.2.2  christos 	}
   3389  1.16.2.2  christos 
   3390  1.16.2.2  christos 	track->usrbuf.mem = (void *)vstart;
   3391  1.16.2.2  christos 	track->usrbuf.capacity = newbufsize;
   3392  1.16.2.2  christos 	memset(track->usrbuf.mem, 0, newvsize);
   3393  1.16.2.2  christos 	return 0;
   3394  1.16.2.2  christos 
   3395  1.16.2.2  christos 	/* failure */
   3396  1.16.2.2  christos abort:
   3397  1.16.2.2  christos 	track->uobj = NULL;		/* paranoia */
   3398  1.16.2.2  christos 	track->usrbuf.mem = NULL;
   3399  1.16.2.2  christos 	track->usrbuf.capacity = 0;
   3400  1.16.2.2  christos 	return error;
   3401  1.16.2.2  christos }
   3402  1.16.2.2  christos 
   3403  1.16.2.2  christos /*
   3404  1.16.2.2  christos  * Free usrbuf (if available).
   3405  1.16.2.2  christos  */
   3406  1.16.2.2  christos static void
   3407  1.16.2.2  christos audio_free_usrbuf(audio_track_t *track)
   3408  1.16.2.2  christos {
   3409  1.16.2.2  christos 	vaddr_t vstart;
   3410  1.16.2.2  christos 	vsize_t vsize;
   3411  1.16.2.2  christos 
   3412  1.16.2.2  christos 	vstart = (vaddr_t)track->usrbuf.mem;
   3413  1.16.2.2  christos 	vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
   3414  1.16.2.2  christos 	if (track->usrbuf.mem != NULL) {
   3415  1.16.2.2  christos 		/*
   3416  1.16.2.2  christos 		 * Unmap the kernel mapping.  uvm_unmap releases the
   3417  1.16.2.2  christos 		 * reference to the uvm object, and this should be the
   3418  1.16.2.2  christos 		 * last virtual mapping of the uvm object, so no need
   3419  1.16.2.2  christos 		 * to explicitly release (`detach') the object.
   3420  1.16.2.2  christos 		 */
   3421  1.16.2.2  christos 		uvm_unmap(kernel_map, vstart, vstart + vsize);
   3422  1.16.2.2  christos 
   3423  1.16.2.2  christos 		track->uobj = NULL;
   3424  1.16.2.2  christos 		track->usrbuf.mem = NULL;
   3425  1.16.2.2  christos 		track->usrbuf.capacity = 0;
   3426  1.16.2.2  christos 	}
   3427  1.16.2.2  christos }
   3428  1.16.2.2  christos 
   3429  1.16.2.2  christos /*
   3430  1.16.2.2  christos  * This filter changes the volume for each channel.
   3431  1.16.2.2  christos  * arg->context points track->ch_volume[].
   3432  1.16.2.2  christos  */
   3433  1.16.2.2  christos static void
   3434  1.16.2.2  christos audio_track_chvol(audio_filter_arg_t *arg)
   3435  1.16.2.2  christos {
   3436  1.16.2.2  christos 	int16_t *ch_volume;
   3437  1.16.2.2  christos 	const aint_t *s;
   3438  1.16.2.2  christos 	aint_t *d;
   3439  1.16.2.2  christos 	u_int i;
   3440  1.16.2.2  christos 	u_int ch;
   3441  1.16.2.2  christos 	u_int channels;
   3442  1.16.2.2  christos 
   3443  1.16.2.2  christos 	DIAGNOSTIC_filter_arg(arg);
   3444  1.16.2.3    martin 	KASSERTMSG(arg->srcfmt->channels == arg->dstfmt->channels,
   3445  1.16.2.3    martin 	    "arg->srcfmt->channels=%d, arg->dstfmt->channels=%d",
   3446  1.16.2.3    martin 	    arg->srcfmt->channels, arg->dstfmt->channels);
   3447  1.16.2.2  christos 	KASSERT(arg->context != NULL);
   3448  1.16.2.3    martin 	KASSERTMSG(arg->srcfmt->channels <= AUDIO_MAX_CHANNELS,
   3449  1.16.2.3    martin 	    "arg->srcfmt->channels=%d", arg->srcfmt->channels);
   3450  1.16.2.2  christos 
   3451  1.16.2.2  christos 	s = arg->src;
   3452  1.16.2.2  christos 	d = arg->dst;
   3453  1.16.2.2  christos 	ch_volume = arg->context;
   3454  1.16.2.2  christos 
   3455  1.16.2.2  christos 	channels = arg->srcfmt->channels;
   3456  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3457  1.16.2.2  christos 		for (ch = 0; ch < channels; ch++) {
   3458  1.16.2.2  christos 			aint2_t val;
   3459  1.16.2.2  christos 			val = *s++;
   3460  1.16.2.3    martin 			val = AUDIO_SCALEDOWN(val * ch_volume[ch], 8);
   3461  1.16.2.2  christos 			*d++ = (aint_t)val;
   3462  1.16.2.2  christos 		}
   3463  1.16.2.2  christos 	}
   3464  1.16.2.2  christos }
   3465  1.16.2.2  christos 
   3466  1.16.2.2  christos /*
   3467  1.16.2.2  christos  * This filter performs conversion from stereo (or more channels) to mono.
   3468  1.16.2.2  christos  */
   3469  1.16.2.2  christos static void
   3470  1.16.2.2  christos audio_track_chmix_mixLR(audio_filter_arg_t *arg)
   3471  1.16.2.2  christos {
   3472  1.16.2.2  christos 	const aint_t *s;
   3473  1.16.2.2  christos 	aint_t *d;
   3474  1.16.2.2  christos 	u_int i;
   3475  1.16.2.2  christos 
   3476  1.16.2.2  christos 	DIAGNOSTIC_filter_arg(arg);
   3477  1.16.2.2  christos 
   3478  1.16.2.2  christos 	s = arg->src;
   3479  1.16.2.2  christos 	d = arg->dst;
   3480  1.16.2.2  christos 
   3481  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3482  1.16.2.3    martin 		*d++ = AUDIO_SCALEDOWN(s[0], 1) + AUDIO_SCALEDOWN(s[1], 1);
   3483  1.16.2.2  christos 		s += arg->srcfmt->channels;
   3484  1.16.2.2  christos 	}
   3485  1.16.2.2  christos }
   3486  1.16.2.2  christos 
   3487  1.16.2.2  christos /*
   3488  1.16.2.2  christos  * This filter performs conversion from mono to stereo (or more channels).
   3489  1.16.2.2  christos  */
   3490  1.16.2.2  christos static void
   3491  1.16.2.2  christos audio_track_chmix_dupLR(audio_filter_arg_t *arg)
   3492  1.16.2.2  christos {
   3493  1.16.2.2  christos 	const aint_t *s;
   3494  1.16.2.2  christos 	aint_t *d;
   3495  1.16.2.2  christos 	u_int i;
   3496  1.16.2.2  christos 	u_int ch;
   3497  1.16.2.2  christos 	u_int dstchannels;
   3498  1.16.2.2  christos 
   3499  1.16.2.2  christos 	DIAGNOSTIC_filter_arg(arg);
   3500  1.16.2.2  christos 
   3501  1.16.2.2  christos 	s = arg->src;
   3502  1.16.2.2  christos 	d = arg->dst;
   3503  1.16.2.2  christos 	dstchannels = arg->dstfmt->channels;
   3504  1.16.2.2  christos 
   3505  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3506  1.16.2.2  christos 		d[0] = s[0];
   3507  1.16.2.2  christos 		d[1] = s[0];
   3508  1.16.2.2  christos 		s++;
   3509  1.16.2.2  christos 		d += dstchannels;
   3510  1.16.2.2  christos 	}
   3511  1.16.2.2  christos 	if (dstchannels > 2) {
   3512  1.16.2.2  christos 		d = arg->dst;
   3513  1.16.2.2  christos 		for (i = 0; i < arg->count; i++) {
   3514  1.16.2.2  christos 			for (ch = 2; ch < dstchannels; ch++) {
   3515  1.16.2.2  christos 				d[ch] = 0;
   3516  1.16.2.2  christos 			}
   3517  1.16.2.2  christos 			d += dstchannels;
   3518  1.16.2.2  christos 		}
   3519  1.16.2.2  christos 	}
   3520  1.16.2.2  christos }
   3521  1.16.2.2  christos 
   3522  1.16.2.2  christos /*
   3523  1.16.2.2  christos  * This filter shrinks M channels into N channels.
   3524  1.16.2.2  christos  * Extra channels are discarded.
   3525  1.16.2.2  christos  */
   3526  1.16.2.2  christos static void
   3527  1.16.2.2  christos audio_track_chmix_shrink(audio_filter_arg_t *arg)
   3528  1.16.2.2  christos {
   3529  1.16.2.2  christos 	const aint_t *s;
   3530  1.16.2.2  christos 	aint_t *d;
   3531  1.16.2.2  christos 	u_int i;
   3532  1.16.2.2  christos 	u_int ch;
   3533  1.16.2.2  christos 
   3534  1.16.2.2  christos 	DIAGNOSTIC_filter_arg(arg);
   3535  1.16.2.2  christos 
   3536  1.16.2.2  christos 	s = arg->src;
   3537  1.16.2.2  christos 	d = arg->dst;
   3538  1.16.2.2  christos 
   3539  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3540  1.16.2.2  christos 		for (ch = 0; ch < arg->dstfmt->channels; ch++) {
   3541  1.16.2.2  christos 			*d++ = s[ch];
   3542  1.16.2.2  christos 		}
   3543  1.16.2.2  christos 		s += arg->srcfmt->channels;
   3544  1.16.2.2  christos 	}
   3545  1.16.2.2  christos }
   3546  1.16.2.2  christos 
   3547  1.16.2.2  christos /*
   3548  1.16.2.2  christos  * This filter expands M channels into N channels.
   3549  1.16.2.2  christos  * Silence is inserted for missing channels.
   3550  1.16.2.2  christos  */
   3551  1.16.2.2  christos static void
   3552  1.16.2.2  christos audio_track_chmix_expand(audio_filter_arg_t *arg)
   3553  1.16.2.2  christos {
   3554  1.16.2.2  christos 	const aint_t *s;
   3555  1.16.2.2  christos 	aint_t *d;
   3556  1.16.2.2  christos 	u_int i;
   3557  1.16.2.2  christos 	u_int ch;
   3558  1.16.2.2  christos 	u_int srcchannels;
   3559  1.16.2.2  christos 	u_int dstchannels;
   3560  1.16.2.2  christos 
   3561  1.16.2.2  christos 	DIAGNOSTIC_filter_arg(arg);
   3562  1.16.2.2  christos 
   3563  1.16.2.2  christos 	s = arg->src;
   3564  1.16.2.2  christos 	d = arg->dst;
   3565  1.16.2.2  christos 
   3566  1.16.2.2  christos 	srcchannels = arg->srcfmt->channels;
   3567  1.16.2.2  christos 	dstchannels = arg->dstfmt->channels;
   3568  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3569  1.16.2.2  christos 		for (ch = 0; ch < srcchannels; ch++) {
   3570  1.16.2.2  christos 			*d++ = *s++;
   3571  1.16.2.2  christos 		}
   3572  1.16.2.2  christos 		for (; ch < dstchannels; ch++) {
   3573  1.16.2.2  christos 			*d++ = 0;
   3574  1.16.2.2  christos 		}
   3575  1.16.2.2  christos 	}
   3576  1.16.2.2  christos }
   3577  1.16.2.2  christos 
   3578  1.16.2.2  christos /*
   3579  1.16.2.2  christos  * This filter performs frequency conversion (up sampling).
   3580  1.16.2.2  christos  * It uses linear interpolation.
   3581  1.16.2.2  christos  */
   3582  1.16.2.2  christos static void
   3583  1.16.2.2  christos audio_track_freq_up(audio_filter_arg_t *arg)
   3584  1.16.2.2  christos {
   3585  1.16.2.2  christos 	audio_track_t *track;
   3586  1.16.2.2  christos 	audio_ring_t *src;
   3587  1.16.2.2  christos 	audio_ring_t *dst;
   3588  1.16.2.2  christos 	const aint_t *s;
   3589  1.16.2.2  christos 	aint_t *d;
   3590  1.16.2.2  christos 	aint_t prev[AUDIO_MAX_CHANNELS];
   3591  1.16.2.2  christos 	aint_t curr[AUDIO_MAX_CHANNELS];
   3592  1.16.2.2  christos 	aint_t grad[AUDIO_MAX_CHANNELS];
   3593  1.16.2.2  christos 	u_int i;
   3594  1.16.2.2  christos 	u_int t;
   3595  1.16.2.2  christos 	u_int step;
   3596  1.16.2.2  christos 	u_int channels;
   3597  1.16.2.2  christos 	u_int ch;
   3598  1.16.2.2  christos 	int srcused;
   3599  1.16.2.2  christos 
   3600  1.16.2.2  christos 	track = arg->context;
   3601  1.16.2.2  christos 	KASSERT(track);
   3602  1.16.2.2  christos 	src = &track->freq.srcbuf;
   3603  1.16.2.2  christos 	dst = track->freq.dst;
   3604  1.16.2.2  christos 	DIAGNOSTIC_ring(dst);
   3605  1.16.2.2  christos 	DIAGNOSTIC_ring(src);
   3606  1.16.2.2  christos 	KASSERT(src->used > 0);
   3607  1.16.2.3    martin 	KASSERTMSG(src->fmt.channels == dst->fmt.channels,
   3608  1.16.2.3    martin 	    "src->fmt.channels=%d dst->fmt.channels=%d",
   3609  1.16.2.3    martin 	    src->fmt.channels, dst->fmt.channels);
   3610  1.16.2.3    martin 	KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
   3611  1.16.2.3    martin 	    "src->head=%d track->mixer->frames_per_block=%d",
   3612  1.16.2.3    martin 	    src->head, track->mixer->frames_per_block);
   3613  1.16.2.2  christos 
   3614  1.16.2.2  christos 	s = arg->src;
   3615  1.16.2.2  christos 	d = arg->dst;
   3616  1.16.2.2  christos 
   3617  1.16.2.2  christos 	/*
   3618  1.16.2.2  christos 	 * In order to faciliate interpolation for each block, slide (delay)
   3619  1.16.2.2  christos 	 * input by one sample.  As a result, strictly speaking, the output
   3620  1.16.2.2  christos 	 * phase is delayed by 1/dstfreq.  However, I believe there is no
   3621  1.16.2.2  christos 	 * observable impact.
   3622  1.16.2.2  christos 	 *
   3623  1.16.2.2  christos 	 * Example)
   3624  1.16.2.2  christos 	 * srcfreq:dstfreq = 1:3
   3625  1.16.2.2  christos 	 *
   3626  1.16.2.2  christos 	 *  A - -
   3627  1.16.2.2  christos 	 *  |
   3628  1.16.2.2  christos 	 *  |
   3629  1.16.2.2  christos 	 *  |     B - -
   3630  1.16.2.2  christos 	 *  +-----+-----> input timeframe
   3631  1.16.2.2  christos 	 *  0     1
   3632  1.16.2.2  christos 	 *
   3633  1.16.2.2  christos 	 *  0     1
   3634  1.16.2.2  christos 	 *  +-----+-----> input timeframe
   3635  1.16.2.2  christos 	 *  |     A
   3636  1.16.2.2  christos 	 *  |   x   x
   3637  1.16.2.2  christos 	 *  | x       x
   3638  1.16.2.2  christos 	 *  x          (B)
   3639  1.16.2.2  christos 	 *  +-+-+-+-+-+-> output timeframe
   3640  1.16.2.2  christos 	 *  0 1 2 3 4 5
   3641  1.16.2.2  christos 	 */
   3642  1.16.2.2  christos 
   3643  1.16.2.2  christos 	/* Last samples in previous block */
   3644  1.16.2.2  christos 	channels = src->fmt.channels;
   3645  1.16.2.2  christos 	for (ch = 0; ch < channels; ch++) {
   3646  1.16.2.2  christos 		prev[ch] = track->freq_prev[ch];
   3647  1.16.2.2  christos 		curr[ch] = track->freq_curr[ch];
   3648  1.16.2.2  christos 		grad[ch] = curr[ch] - prev[ch];
   3649  1.16.2.2  christos 	}
   3650  1.16.2.2  christos 
   3651  1.16.2.2  christos 	step = track->freq_step;
   3652  1.16.2.2  christos 	t = track->freq_current;
   3653  1.16.2.2  christos //#define FREQ_DEBUG
   3654  1.16.2.2  christos #if defined(FREQ_DEBUG)
   3655  1.16.2.2  christos #define PRINTF(fmt...)	printf(fmt)
   3656  1.16.2.2  christos #else
   3657  1.16.2.2  christos #define PRINTF(fmt...)	do { } while (0)
   3658  1.16.2.2  christos #endif
   3659  1.16.2.2  christos 	srcused = src->used;
   3660  1.16.2.2  christos 	PRINTF("upstart step=%d leap=%d", step, track->freq_leap);
   3661  1.16.2.2  christos 	PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
   3662  1.16.2.2  christos 	PRINTF(" prev=%d curr=%d grad=%d", prev[0], curr[0], grad[0]);
   3663  1.16.2.2  christos 	PRINTF(" t=%d\n", t);
   3664  1.16.2.2  christos 
   3665  1.16.2.2  christos 	for (i = 0; i < arg->count; i++) {
   3666  1.16.2.2  christos 		PRINTF("i=%d t=%5d", i, t);
   3667  1.16.2.2  christos 		if (t >= 65536) {
   3668  1.16.2.2  christos 			for (ch = 0; ch < channels; ch++) {
   3669  1.16.2.2  christos 				prev[ch] = curr[ch];
   3670  1.16.2.2  christos 				curr[ch] = *s++;
   3671  1.16.2.2  christos 				grad[ch] = curr[ch] - prev[ch];
   3672  1.16.2.2  christos 			}
   3673  1.16.2.2  christos 			PRINTF(" prev=%d s[%d]=%d",
   3674  1.16.2.2  christos 			    prev[0], src->used - srcused, curr[0]);
   3675  1.16.2.2  christos 
   3676  1.16.2.2  christos 			/* Update */
   3677  1.16.2.2  christos 			t -= 65536;
   3678  1.16.2.2  christos 			srcused--;
   3679  1.16.2.2  christos 			if (srcused < 0) {
   3680  1.16.2.2  christos 				PRINTF(" break\n");
   3681  1.16.2.2  christos 				break;
   3682  1.16.2.2  christos 			}
   3683  1.16.2.2  christos 		}
   3684  1.16.2.2  christos 
   3685  1.16.2.2  christos 		for (ch = 0; ch < channels; ch++) {
   3686  1.16.2.2  christos 			*d++ = prev[ch] + (aint2_t)grad[ch] * t / 65536;
   3687  1.16.2.2  christos #if defined(FREQ_DEBUG)
   3688  1.16.2.2  christos 			if (ch == 0)
   3689  1.16.2.2  christos 				printf(" t=%5d *d=%d", t, d[-1]);
   3690  1.16.2.2  christos #endif
   3691  1.16.2.2  christos 		}
   3692  1.16.2.2  christos 		t += step;
   3693  1.16.2.2  christos 
   3694  1.16.2.2  christos 		PRINTF("\n");
   3695  1.16.2.2  christos 	}
   3696  1.16.2.2  christos 	PRINTF("end prev=%d curr=%d\n", prev[0], curr[0]);
   3697  1.16.2.2  christos 
   3698  1.16.2.2  christos 	auring_take(src, src->used);
   3699  1.16.2.2  christos 	auring_push(dst, i);
   3700  1.16.2.2  christos 
   3701  1.16.2.2  christos 	/* Adjust */
   3702  1.16.2.2  christos 	t += track->freq_leap;
   3703  1.16.2.2  christos 
   3704  1.16.2.2  christos 	track->freq_current = t;
   3705  1.16.2.2  christos 	for (ch = 0; ch < channels; ch++) {
   3706  1.16.2.2  christos 		track->freq_prev[ch] = prev[ch];
   3707  1.16.2.2  christos 		track->freq_curr[ch] = curr[ch];
   3708  1.16.2.2  christos 	}
   3709  1.16.2.2  christos }
   3710  1.16.2.2  christos 
   3711  1.16.2.2  christos /*
   3712  1.16.2.2  christos  * This filter performs frequency conversion (down sampling).
   3713  1.16.2.2  christos  * It uses simple thinning.
   3714  1.16.2.2  christos  */
   3715  1.16.2.2  christos static void
   3716  1.16.2.2  christos audio_track_freq_down(audio_filter_arg_t *arg)
   3717  1.16.2.2  christos {
   3718  1.16.2.2  christos 	audio_track_t *track;
   3719  1.16.2.2  christos 	audio_ring_t *src;
   3720  1.16.2.2  christos 	audio_ring_t *dst;
   3721  1.16.2.2  christos 	const aint_t *s0;
   3722  1.16.2.2  christos 	aint_t *d;
   3723  1.16.2.2  christos 	u_int i;
   3724  1.16.2.2  christos 	u_int t;
   3725  1.16.2.2  christos 	u_int step;
   3726  1.16.2.2  christos 	u_int ch;
   3727  1.16.2.2  christos 	u_int channels;
   3728  1.16.2.2  christos 
   3729  1.16.2.2  christos 	track = arg->context;
   3730  1.16.2.2  christos 	KASSERT(track);
   3731  1.16.2.2  christos 	src = &track->freq.srcbuf;
   3732  1.16.2.2  christos 	dst = track->freq.dst;
   3733  1.16.2.2  christos 
   3734  1.16.2.2  christos 	DIAGNOSTIC_ring(dst);
   3735  1.16.2.2  christos 	DIAGNOSTIC_ring(src);
   3736  1.16.2.2  christos 	KASSERT(src->used > 0);
   3737  1.16.2.3    martin 	KASSERTMSG(src->fmt.channels == dst->fmt.channels,
   3738  1.16.2.3    martin 	    "src->fmt.channels=%d dst->fmt.channels=%d",
   3739  1.16.2.3    martin 	    src->fmt.channels, dst->fmt.channels);
   3740  1.16.2.2  christos 	KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
   3741  1.16.2.3    martin 	    "src->head=%d track->mixer->frames_per_block=%d",
   3742  1.16.2.2  christos 	    src->head, track->mixer->frames_per_block);
   3743  1.16.2.2  christos 
   3744  1.16.2.2  christos 	s0 = arg->src;
   3745  1.16.2.2  christos 	d = arg->dst;
   3746  1.16.2.2  christos 	t = track->freq_current;
   3747  1.16.2.2  christos 	step = track->freq_step;
   3748  1.16.2.2  christos 	channels = dst->fmt.channels;
   3749  1.16.2.2  christos 	PRINTF("downstart step=%d leap=%d", step, track->freq_leap);
   3750  1.16.2.2  christos 	PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
   3751  1.16.2.2  christos 	PRINTF(" t=%d\n", t);
   3752  1.16.2.2  christos 
   3753  1.16.2.2  christos 	for (i = 0; i < arg->count && t / 65536 < src->used; i++) {
   3754  1.16.2.2  christos 		const aint_t *s;
   3755  1.16.2.2  christos 		PRINTF("i=%4d t=%10d", i, t);
   3756  1.16.2.2  christos 		s = s0 + (t / 65536) * channels;
   3757  1.16.2.2  christos 		PRINTF(" s=%5ld", (s - s0) / channels);
   3758  1.16.2.2  christos 		for (ch = 0; ch < channels; ch++) {
   3759  1.16.2.2  christos 			if (ch == 0) PRINTF(" *s=%d", s[ch]);
   3760  1.16.2.2  christos 			*d++ = s[ch];
   3761  1.16.2.2  christos 		}
   3762  1.16.2.2  christos 		PRINTF("\n");
   3763  1.16.2.2  christos 		t += step;
   3764  1.16.2.2  christos 	}
   3765  1.16.2.2  christos 	t += track->freq_leap;
   3766  1.16.2.2  christos 	PRINTF("end t=%d\n", t);
   3767  1.16.2.2  christos 	auring_take(src, src->used);
   3768  1.16.2.2  christos 	auring_push(dst, i);
   3769  1.16.2.2  christos 	track->freq_current = t % 65536;
   3770  1.16.2.2  christos }
   3771  1.16.2.2  christos 
   3772  1.16.2.2  christos /*
   3773  1.16.2.2  christos  * Creates track and returns it.
   3774  1.16.2.3    martin  * Must be called without sc_lock held.
   3775  1.16.2.2  christos  */
   3776  1.16.2.2  christos audio_track_t *
   3777  1.16.2.2  christos audio_track_create(struct audio_softc *sc, audio_trackmixer_t *mixer)
   3778  1.16.2.2  christos {
   3779  1.16.2.2  christos 	audio_track_t *track;
   3780  1.16.2.2  christos 	static int newid = 0;
   3781  1.16.2.2  christos 
   3782  1.16.2.2  christos 	track = kmem_zalloc(sizeof(*track), KM_SLEEP);
   3783  1.16.2.2  christos 
   3784  1.16.2.2  christos 	track->id = newid++;
   3785  1.16.2.2  christos 	track->mixer = mixer;
   3786  1.16.2.2  christos 	track->mode = mixer->mode;
   3787  1.16.2.2  christos 
   3788  1.16.2.2  christos 	/* Do TRACE after id is assigned. */
   3789  1.16.2.2  christos 	TRACET(3, track, "for %s",
   3790  1.16.2.2  christos 	    mixer->mode == AUMODE_PLAY ? "playback" : "recording");
   3791  1.16.2.2  christos 
   3792  1.16.2.2  christos #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   3793  1.16.2.2  christos 	track->volume = 256;
   3794  1.16.2.2  christos #endif
   3795  1.16.2.2  christos 	for (int i = 0; i < AUDIO_MAX_CHANNELS; i++) {
   3796  1.16.2.2  christos 		track->ch_volume[i] = 256;
   3797  1.16.2.2  christos 	}
   3798  1.16.2.2  christos 
   3799  1.16.2.2  christos 	return track;
   3800  1.16.2.2  christos }
   3801  1.16.2.2  christos 
   3802  1.16.2.2  christos /*
   3803  1.16.2.2  christos  * Release all resources of the track and track itself.
   3804  1.16.2.2  christos  * track must not be NULL.  Don't specify the track within the file
   3805  1.16.2.2  christos  * structure linked from sc->sc_files.
   3806  1.16.2.2  christos  */
   3807  1.16.2.2  christos static void
   3808  1.16.2.2  christos audio_track_destroy(audio_track_t *track)
   3809  1.16.2.2  christos {
   3810  1.16.2.2  christos 
   3811  1.16.2.2  christos 	KASSERT(track);
   3812  1.16.2.2  christos 
   3813  1.16.2.2  christos 	audio_free_usrbuf(track);
   3814  1.16.2.2  christos 	audio_free(track->codec.srcbuf.mem);
   3815  1.16.2.2  christos 	audio_free(track->chvol.srcbuf.mem);
   3816  1.16.2.2  christos 	audio_free(track->chmix.srcbuf.mem);
   3817  1.16.2.2  christos 	audio_free(track->freq.srcbuf.mem);
   3818  1.16.2.2  christos 	audio_free(track->outbuf.mem);
   3819  1.16.2.2  christos 
   3820  1.16.2.2  christos 	kmem_free(track, sizeof(*track));
   3821  1.16.2.2  christos }
   3822  1.16.2.2  christos 
   3823  1.16.2.2  christos /*
   3824  1.16.2.2  christos  * It returns encoding conversion filter according to src and dst format.
   3825  1.16.2.2  christos  * If it is not a convertible pair, it returns NULL.  Either src or dst
   3826  1.16.2.2  christos  * must be internal format.
   3827  1.16.2.2  christos  */
   3828  1.16.2.2  christos static audio_filter_t
   3829  1.16.2.2  christos audio_track_get_codec(audio_track_t *track, const audio_format2_t *src,
   3830  1.16.2.2  christos 	const audio_format2_t *dst)
   3831  1.16.2.2  christos {
   3832  1.16.2.2  christos 
   3833  1.16.2.2  christos 	if (audio_format2_is_internal(src)) {
   3834  1.16.2.2  christos 		if (dst->encoding == AUDIO_ENCODING_ULAW) {
   3835  1.16.2.2  christos 			return audio_internal_to_mulaw;
   3836  1.16.2.2  christos 		} else if (dst->encoding == AUDIO_ENCODING_ALAW) {
   3837  1.16.2.2  christos 			return audio_internal_to_alaw;
   3838  1.16.2.2  christos 		} else if (audio_format2_is_linear(dst)) {
   3839  1.16.2.2  christos 			switch (dst->stride) {
   3840  1.16.2.2  christos 			case 8:
   3841  1.16.2.2  christos 				return audio_internal_to_linear8;
   3842  1.16.2.2  christos 			case 16:
   3843  1.16.2.2  christos 				return audio_internal_to_linear16;
   3844  1.16.2.2  christos #if defined(AUDIO_SUPPORT_LINEAR24)
   3845  1.16.2.2  christos 			case 24:
   3846  1.16.2.2  christos 				return audio_internal_to_linear24;
   3847  1.16.2.2  christos #endif
   3848  1.16.2.2  christos 			case 32:
   3849  1.16.2.2  christos 				return audio_internal_to_linear32;
   3850  1.16.2.2  christos 			default:
   3851  1.16.2.2  christos 				TRACET(1, track, "unsupported %s stride %d",
   3852  1.16.2.2  christos 				    "dst", dst->stride);
   3853  1.16.2.2  christos 				goto abort;
   3854  1.16.2.2  christos 			}
   3855  1.16.2.2  christos 		}
   3856  1.16.2.2  christos 	} else if (audio_format2_is_internal(dst)) {
   3857  1.16.2.2  christos 		if (src->encoding == AUDIO_ENCODING_ULAW) {
   3858  1.16.2.2  christos 			return audio_mulaw_to_internal;
   3859  1.16.2.2  christos 		} else if (src->encoding == AUDIO_ENCODING_ALAW) {
   3860  1.16.2.2  christos 			return audio_alaw_to_internal;
   3861  1.16.2.2  christos 		} else if (audio_format2_is_linear(src)) {
   3862  1.16.2.2  christos 			switch (src->stride) {
   3863  1.16.2.2  christos 			case 8:
   3864  1.16.2.2  christos 				return audio_linear8_to_internal;
   3865  1.16.2.2  christos 			case 16:
   3866  1.16.2.2  christos 				return audio_linear16_to_internal;
   3867  1.16.2.2  christos #if defined(AUDIO_SUPPORT_LINEAR24)
   3868  1.16.2.2  christos 			case 24:
   3869  1.16.2.2  christos 				return audio_linear24_to_internal;
   3870  1.16.2.2  christos #endif
   3871  1.16.2.2  christos 			case 32:
   3872  1.16.2.2  christos 				return audio_linear32_to_internal;
   3873  1.16.2.2  christos 			default:
   3874  1.16.2.2  christos 				TRACET(1, track, "unsupported %s stride %d",
   3875  1.16.2.2  christos 				    "src", src->stride);
   3876  1.16.2.2  christos 				goto abort;
   3877  1.16.2.2  christos 			}
   3878  1.16.2.2  christos 		}
   3879  1.16.2.2  christos 	}
   3880  1.16.2.2  christos 
   3881  1.16.2.2  christos 	TRACET(1, track, "unsupported encoding");
   3882  1.16.2.2  christos abort:
   3883  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   3884  1.16.2.2  christos 	if (audiodebug >= 2) {
   3885  1.16.2.2  christos 		char buf[100];
   3886  1.16.2.2  christos 		audio_format2_tostr(buf, sizeof(buf), src);
   3887  1.16.2.2  christos 		TRACET(2, track, "src %s", buf);
   3888  1.16.2.2  christos 		audio_format2_tostr(buf, sizeof(buf), dst);
   3889  1.16.2.2  christos 		TRACET(2, track, "dst %s", buf);
   3890  1.16.2.2  christos 	}
   3891  1.16.2.2  christos #endif
   3892  1.16.2.2  christos 	return NULL;
   3893  1.16.2.2  christos }
   3894  1.16.2.2  christos 
   3895  1.16.2.2  christos /*
   3896  1.16.2.2  christos  * Initialize the codec stage of this track as necessary.
   3897  1.16.2.2  christos  * If successful, it initializes the codec stage as necessary, stores updated
   3898  1.16.2.2  christos  * last_dst in *last_dstp in any case, and returns 0.
   3899  1.16.2.2  christos  * Otherwise, it returns errno without modifying *last_dstp.
   3900  1.16.2.2  christos  */
   3901  1.16.2.2  christos static int
   3902  1.16.2.2  christos audio_track_init_codec(audio_track_t *track, audio_ring_t **last_dstp)
   3903  1.16.2.2  christos {
   3904  1.16.2.2  christos 	audio_ring_t *last_dst;
   3905  1.16.2.2  christos 	audio_ring_t *srcbuf;
   3906  1.16.2.2  christos 	audio_format2_t *srcfmt;
   3907  1.16.2.2  christos 	audio_format2_t *dstfmt;
   3908  1.16.2.2  christos 	audio_filter_arg_t *arg;
   3909  1.16.2.2  christos 	u_int len;
   3910  1.16.2.2  christos 	int error;
   3911  1.16.2.2  christos 
   3912  1.16.2.2  christos 	KASSERT(track);
   3913  1.16.2.2  christos 
   3914  1.16.2.2  christos 	last_dst = *last_dstp;
   3915  1.16.2.2  christos 	dstfmt = &last_dst->fmt;
   3916  1.16.2.2  christos 	srcfmt = &track->inputfmt;
   3917  1.16.2.2  christos 	srcbuf = &track->codec.srcbuf;
   3918  1.16.2.2  christos 	error = 0;
   3919  1.16.2.2  christos 
   3920  1.16.2.2  christos 	if (srcfmt->encoding != dstfmt->encoding
   3921  1.16.2.2  christos 	 || srcfmt->precision != dstfmt->precision
   3922  1.16.2.2  christos 	 || srcfmt->stride != dstfmt->stride) {
   3923  1.16.2.2  christos 		track->codec.dst = last_dst;
   3924  1.16.2.2  christos 
   3925  1.16.2.2  christos 		srcbuf->fmt = *dstfmt;
   3926  1.16.2.2  christos 		srcbuf->fmt.encoding = srcfmt->encoding;
   3927  1.16.2.2  christos 		srcbuf->fmt.precision = srcfmt->precision;
   3928  1.16.2.2  christos 		srcbuf->fmt.stride = srcfmt->stride;
   3929  1.16.2.2  christos 
   3930  1.16.2.2  christos 		track->codec.filter = audio_track_get_codec(track,
   3931  1.16.2.2  christos 		    &srcbuf->fmt, dstfmt);
   3932  1.16.2.2  christos 		if (track->codec.filter == NULL) {
   3933  1.16.2.2  christos 			error = EINVAL;
   3934  1.16.2.2  christos 			goto abort;
   3935  1.16.2.2  christos 		}
   3936  1.16.2.2  christos 
   3937  1.16.2.2  christos 		srcbuf->head = 0;
   3938  1.16.2.2  christos 		srcbuf->used = 0;
   3939  1.16.2.2  christos 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   3940  1.16.2.2  christos 		len = auring_bytelen(srcbuf);
   3941  1.16.2.2  christos 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   3942  1.16.2.2  christos 
   3943  1.16.2.2  christos 		arg = &track->codec.arg;
   3944  1.16.2.2  christos 		arg->srcfmt = &srcbuf->fmt;
   3945  1.16.2.2  christos 		arg->dstfmt = dstfmt;
   3946  1.16.2.2  christos 		arg->context = NULL;
   3947  1.16.2.2  christos 
   3948  1.16.2.2  christos 		*last_dstp = srcbuf;
   3949  1.16.2.2  christos 		return 0;
   3950  1.16.2.2  christos 	}
   3951  1.16.2.2  christos 
   3952  1.16.2.2  christos abort:
   3953  1.16.2.2  christos 	track->codec.filter = NULL;
   3954  1.16.2.2  christos 	audio_free(srcbuf->mem);
   3955  1.16.2.2  christos 	return error;
   3956  1.16.2.2  christos }
   3957  1.16.2.2  christos 
   3958  1.16.2.2  christos /*
   3959  1.16.2.2  christos  * Initialize the chvol stage of this track as necessary.
   3960  1.16.2.2  christos  * If successful, it initializes the chvol stage as necessary, stores updated
   3961  1.16.2.2  christos  * last_dst in *last_dstp in any case, and returns 0.
   3962  1.16.2.2  christos  * Otherwise, it returns errno without modifying *last_dstp.
   3963  1.16.2.2  christos  */
   3964  1.16.2.2  christos static int
   3965  1.16.2.2  christos audio_track_init_chvol(audio_track_t *track, audio_ring_t **last_dstp)
   3966  1.16.2.2  christos {
   3967  1.16.2.2  christos 	audio_ring_t *last_dst;
   3968  1.16.2.2  christos 	audio_ring_t *srcbuf;
   3969  1.16.2.2  christos 	audio_format2_t *srcfmt;
   3970  1.16.2.2  christos 	audio_format2_t *dstfmt;
   3971  1.16.2.2  christos 	audio_filter_arg_t *arg;
   3972  1.16.2.2  christos 	u_int len;
   3973  1.16.2.2  christos 	int error;
   3974  1.16.2.2  christos 
   3975  1.16.2.2  christos 	KASSERT(track);
   3976  1.16.2.2  christos 
   3977  1.16.2.2  christos 	last_dst = *last_dstp;
   3978  1.16.2.2  christos 	dstfmt = &last_dst->fmt;
   3979  1.16.2.2  christos 	srcfmt = &track->inputfmt;
   3980  1.16.2.2  christos 	srcbuf = &track->chvol.srcbuf;
   3981  1.16.2.2  christos 	error = 0;
   3982  1.16.2.2  christos 
   3983  1.16.2.2  christos 	/* Check whether channel volume conversion is necessary. */
   3984  1.16.2.2  christos 	bool use_chvol = false;
   3985  1.16.2.2  christos 	for (int ch = 0; ch < srcfmt->channels; ch++) {
   3986  1.16.2.2  christos 		if (track->ch_volume[ch] != 256) {
   3987  1.16.2.2  christos 			use_chvol = true;
   3988  1.16.2.2  christos 			break;
   3989  1.16.2.2  christos 		}
   3990  1.16.2.2  christos 	}
   3991  1.16.2.2  christos 
   3992  1.16.2.2  christos 	if (use_chvol == true) {
   3993  1.16.2.2  christos 		track->chvol.dst = last_dst;
   3994  1.16.2.2  christos 		track->chvol.filter = audio_track_chvol;
   3995  1.16.2.2  christos 
   3996  1.16.2.2  christos 		srcbuf->fmt = *dstfmt;
   3997  1.16.2.2  christos 		/* no format conversion occurs */
   3998  1.16.2.2  christos 
   3999  1.16.2.2  christos 		srcbuf->head = 0;
   4000  1.16.2.2  christos 		srcbuf->used = 0;
   4001  1.16.2.2  christos 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4002  1.16.2.2  christos 		len = auring_bytelen(srcbuf);
   4003  1.16.2.2  christos 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4004  1.16.2.2  christos 
   4005  1.16.2.2  christos 		arg = &track->chvol.arg;
   4006  1.16.2.2  christos 		arg->srcfmt = &srcbuf->fmt;
   4007  1.16.2.2  christos 		arg->dstfmt = dstfmt;
   4008  1.16.2.2  christos 		arg->context = track->ch_volume;
   4009  1.16.2.2  christos 
   4010  1.16.2.2  christos 		*last_dstp = srcbuf;
   4011  1.16.2.2  christos 		return 0;
   4012  1.16.2.2  christos 	}
   4013  1.16.2.2  christos 
   4014  1.16.2.2  christos 	track->chvol.filter = NULL;
   4015  1.16.2.2  christos 	audio_free(srcbuf->mem);
   4016  1.16.2.2  christos 	return error;
   4017  1.16.2.2  christos }
   4018  1.16.2.2  christos 
   4019  1.16.2.2  christos /*
   4020  1.16.2.2  christos  * Initialize the chmix stage of this track as necessary.
   4021  1.16.2.2  christos  * If successful, it initializes the chmix stage as necessary, stores updated
   4022  1.16.2.2  christos  * last_dst in *last_dstp in any case, and returns 0.
   4023  1.16.2.2  christos  * Otherwise, it returns errno without modifying *last_dstp.
   4024  1.16.2.2  christos  */
   4025  1.16.2.2  christos static int
   4026  1.16.2.2  christos audio_track_init_chmix(audio_track_t *track, audio_ring_t **last_dstp)
   4027  1.16.2.2  christos {
   4028  1.16.2.2  christos 	audio_ring_t *last_dst;
   4029  1.16.2.2  christos 	audio_ring_t *srcbuf;
   4030  1.16.2.2  christos 	audio_format2_t *srcfmt;
   4031  1.16.2.2  christos 	audio_format2_t *dstfmt;
   4032  1.16.2.2  christos 	audio_filter_arg_t *arg;
   4033  1.16.2.2  christos 	u_int srcch;
   4034  1.16.2.2  christos 	u_int dstch;
   4035  1.16.2.2  christos 	u_int len;
   4036  1.16.2.2  christos 	int error;
   4037  1.16.2.2  christos 
   4038  1.16.2.2  christos 	KASSERT(track);
   4039  1.16.2.2  christos 
   4040  1.16.2.2  christos 	last_dst = *last_dstp;
   4041  1.16.2.2  christos 	dstfmt = &last_dst->fmt;
   4042  1.16.2.2  christos 	srcfmt = &track->inputfmt;
   4043  1.16.2.2  christos 	srcbuf = &track->chmix.srcbuf;
   4044  1.16.2.2  christos 	error = 0;
   4045  1.16.2.2  christos 
   4046  1.16.2.2  christos 	srcch = srcfmt->channels;
   4047  1.16.2.2  christos 	dstch = dstfmt->channels;
   4048  1.16.2.2  christos 	if (srcch != dstch) {
   4049  1.16.2.2  christos 		track->chmix.dst = last_dst;
   4050  1.16.2.2  christos 
   4051  1.16.2.2  christos 		if (srcch >= 2 && dstch == 1) {
   4052  1.16.2.2  christos 			track->chmix.filter = audio_track_chmix_mixLR;
   4053  1.16.2.2  christos 		} else if (srcch == 1 && dstch >= 2) {
   4054  1.16.2.2  christos 			track->chmix.filter = audio_track_chmix_dupLR;
   4055  1.16.2.2  christos 		} else if (srcch > dstch) {
   4056  1.16.2.2  christos 			track->chmix.filter = audio_track_chmix_shrink;
   4057  1.16.2.2  christos 		} else {
   4058  1.16.2.2  christos 			track->chmix.filter = audio_track_chmix_expand;
   4059  1.16.2.2  christos 		}
   4060  1.16.2.2  christos 
   4061  1.16.2.2  christos 		srcbuf->fmt = *dstfmt;
   4062  1.16.2.2  christos 		srcbuf->fmt.channels = srcch;
   4063  1.16.2.2  christos 
   4064  1.16.2.2  christos 		srcbuf->head = 0;
   4065  1.16.2.2  christos 		srcbuf->used = 0;
   4066  1.16.2.2  christos 		/* XXX The buffer size should be able to calculate. */
   4067  1.16.2.2  christos 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4068  1.16.2.2  christos 		len = auring_bytelen(srcbuf);
   4069  1.16.2.2  christos 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4070  1.16.2.2  christos 
   4071  1.16.2.2  christos 		arg = &track->chmix.arg;
   4072  1.16.2.2  christos 		arg->srcfmt = &srcbuf->fmt;
   4073  1.16.2.2  christos 		arg->dstfmt = dstfmt;
   4074  1.16.2.2  christos 		arg->context = NULL;
   4075  1.16.2.2  christos 
   4076  1.16.2.2  christos 		*last_dstp = srcbuf;
   4077  1.16.2.2  christos 		return 0;
   4078  1.16.2.2  christos 	}
   4079  1.16.2.2  christos 
   4080  1.16.2.2  christos 	track->chmix.filter = NULL;
   4081  1.16.2.2  christos 	audio_free(srcbuf->mem);
   4082  1.16.2.2  christos 	return error;
   4083  1.16.2.2  christos }
   4084  1.16.2.2  christos 
   4085  1.16.2.2  christos /*
   4086  1.16.2.2  christos  * Initialize the freq stage of this track as necessary.
   4087  1.16.2.2  christos  * If successful, it initializes the freq stage as necessary, stores updated
   4088  1.16.2.2  christos  * last_dst in *last_dstp in any case, and returns 0.
   4089  1.16.2.2  christos  * Otherwise, it returns errno without modifying *last_dstp.
   4090  1.16.2.2  christos  */
   4091  1.16.2.2  christos static int
   4092  1.16.2.2  christos audio_track_init_freq(audio_track_t *track, audio_ring_t **last_dstp)
   4093  1.16.2.2  christos {
   4094  1.16.2.2  christos 	audio_ring_t *last_dst;
   4095  1.16.2.2  christos 	audio_ring_t *srcbuf;
   4096  1.16.2.2  christos 	audio_format2_t *srcfmt;
   4097  1.16.2.2  christos 	audio_format2_t *dstfmt;
   4098  1.16.2.2  christos 	audio_filter_arg_t *arg;
   4099  1.16.2.2  christos 	uint32_t srcfreq;
   4100  1.16.2.2  christos 	uint32_t dstfreq;
   4101  1.16.2.2  christos 	u_int dst_capacity;
   4102  1.16.2.2  christos 	u_int mod;
   4103  1.16.2.2  christos 	u_int len;
   4104  1.16.2.2  christos 	int error;
   4105  1.16.2.2  christos 
   4106  1.16.2.2  christos 	KASSERT(track);
   4107  1.16.2.2  christos 
   4108  1.16.2.2  christos 	last_dst = *last_dstp;
   4109  1.16.2.2  christos 	dstfmt = &last_dst->fmt;
   4110  1.16.2.2  christos 	srcfmt = &track->inputfmt;
   4111  1.16.2.2  christos 	srcbuf = &track->freq.srcbuf;
   4112  1.16.2.2  christos 	error = 0;
   4113  1.16.2.2  christos 
   4114  1.16.2.2  christos 	srcfreq = srcfmt->sample_rate;
   4115  1.16.2.2  christos 	dstfreq = dstfmt->sample_rate;
   4116  1.16.2.2  christos 	if (srcfreq != dstfreq) {
   4117  1.16.2.2  christos 		track->freq.dst = last_dst;
   4118  1.16.2.2  christos 
   4119  1.16.2.2  christos 		memset(track->freq_prev, 0, sizeof(track->freq_prev));
   4120  1.16.2.2  christos 		memset(track->freq_curr, 0, sizeof(track->freq_curr));
   4121  1.16.2.2  christos 
   4122  1.16.2.2  christos 		/* freq_step is the ratio of src/dst when let dst 65536. */
   4123  1.16.2.2  christos 		track->freq_step = (uint64_t)srcfreq * 65536 / dstfreq;
   4124  1.16.2.2  christos 
   4125  1.16.2.2  christos 		dst_capacity = frame_per_block(track->mixer, dstfmt);
   4126  1.16.2.2  christos 		mod = (uint64_t)srcfreq * 65536 % dstfreq;
   4127  1.16.2.2  christos 		track->freq_leap = (mod * dst_capacity + dstfreq / 2) / dstfreq;
   4128  1.16.2.2  christos 
   4129  1.16.2.2  christos 		if (track->freq_step < 65536) {
   4130  1.16.2.2  christos 			track->freq.filter = audio_track_freq_up;
   4131  1.16.2.2  christos 			/* In order to carry at the first time. */
   4132  1.16.2.2  christos 			track->freq_current = 65536;
   4133  1.16.2.2  christos 		} else {
   4134  1.16.2.2  christos 			track->freq.filter = audio_track_freq_down;
   4135  1.16.2.2  christos 			track->freq_current = 0;
   4136  1.16.2.2  christos 		}
   4137  1.16.2.2  christos 
   4138  1.16.2.2  christos 		srcbuf->fmt = *dstfmt;
   4139  1.16.2.2  christos 		srcbuf->fmt.sample_rate = srcfreq;
   4140  1.16.2.2  christos 
   4141  1.16.2.2  christos 		srcbuf->head = 0;
   4142  1.16.2.2  christos 		srcbuf->used = 0;
   4143  1.16.2.2  christos 		srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
   4144  1.16.2.2  christos 		len = auring_bytelen(srcbuf);
   4145  1.16.2.2  christos 		srcbuf->mem = audio_realloc(srcbuf->mem, len);
   4146  1.16.2.2  christos 
   4147  1.16.2.2  christos 		arg = &track->freq.arg;
   4148  1.16.2.2  christos 		arg->srcfmt = &srcbuf->fmt;
   4149  1.16.2.2  christos 		arg->dstfmt = dstfmt;/*&last_dst->fmt;*/
   4150  1.16.2.2  christos 		arg->context = track;
   4151  1.16.2.2  christos 
   4152  1.16.2.2  christos 		*last_dstp = srcbuf;
   4153  1.16.2.2  christos 		return 0;
   4154  1.16.2.2  christos 	}
   4155  1.16.2.2  christos 
   4156  1.16.2.2  christos 	track->freq.filter = NULL;
   4157  1.16.2.2  christos 	audio_free(srcbuf->mem);
   4158  1.16.2.2  christos 	return error;
   4159  1.16.2.2  christos }
   4160  1.16.2.2  christos 
   4161  1.16.2.2  christos /*
   4162  1.16.2.2  christos  * When playing back: (e.g. if codec and freq stage are valid)
   4163  1.16.2.2  christos  *
   4164  1.16.2.2  christos  *               write
   4165  1.16.2.2  christos  *                | uiomove
   4166  1.16.2.2  christos  *                v
   4167  1.16.2.2  christos  *  usrbuf      [...............]  byte ring buffer (mmap-able)
   4168  1.16.2.2  christos  *                | memcpy
   4169  1.16.2.2  christos  *                v
   4170  1.16.2.2  christos  *  codec.srcbuf[....]             1 block (ring) buffer   <-- stage input
   4171  1.16.2.2  christos  *       .dst ----+
   4172  1.16.2.2  christos  *                | convert
   4173  1.16.2.2  christos  *                v
   4174  1.16.2.2  christos  *  freq.srcbuf [....]             1 block (ring) buffer
   4175  1.16.2.2  christos  *      .dst  ----+
   4176  1.16.2.2  christos  *                | convert
   4177  1.16.2.2  christos  *                v
   4178  1.16.2.2  christos  *  outbuf      [...............]  NBLKOUT blocks ring buffer
   4179  1.16.2.2  christos  *
   4180  1.16.2.2  christos  *
   4181  1.16.2.2  christos  * When recording:
   4182  1.16.2.2  christos  *
   4183  1.16.2.2  christos  *  freq.srcbuf [...............]  NBLKOUT blocks ring buffer <-- stage input
   4184  1.16.2.2  christos  *      .dst  ----+
   4185  1.16.2.2  christos  *                | convert
   4186  1.16.2.2  christos  *                v
   4187  1.16.2.2  christos  *  codec.srcbuf[.....]            1 block (ring) buffer
   4188  1.16.2.2  christos  *       .dst ----+
   4189  1.16.2.2  christos  *                | convert
   4190  1.16.2.2  christos  *                v
   4191  1.16.2.2  christos  *  outbuf      [.....]            1 block (ring) buffer
   4192  1.16.2.2  christos  *                | memcpy
   4193  1.16.2.2  christos  *                v
   4194  1.16.2.2  christos  *  usrbuf      [...............]  byte ring buffer (mmap-able *)
   4195  1.16.2.2  christos  *                | uiomove
   4196  1.16.2.2  christos  *                v
   4197  1.16.2.2  christos  *               read
   4198  1.16.2.2  christos  *
   4199  1.16.2.2  christos  *    *: usrbuf for recording is also mmap-able due to symmetry with
   4200  1.16.2.2  christos  *       playback buffer, but for now mmap will never happen for recording.
   4201  1.16.2.2  christos  */
   4202  1.16.2.2  christos 
   4203  1.16.2.2  christos /*
   4204  1.16.2.2  christos  * Set the userland format of this track.
   4205  1.16.2.2  christos  * usrfmt argument should be parameter verified with audio_check_params().
   4206  1.16.2.2  christos  * It will release and reallocate all internal conversion buffers.
   4207  1.16.2.2  christos  * It returns 0 if successful.  Otherwise it returns errno with clearing all
   4208  1.16.2.2  christos  * internal buffers.
   4209  1.16.2.2  christos  * It must be called without sc_intr_lock since uvm_* routines require non
   4210  1.16.2.2  christos  * intr_lock state.
   4211  1.16.2.2  christos  * It must be called with track lock held since it may release and reallocate
   4212  1.16.2.2  christos  * outbuf.
   4213  1.16.2.2  christos  */
   4214  1.16.2.2  christos static int
   4215  1.16.2.2  christos audio_track_set_format(audio_track_t *track, audio_format2_t *usrfmt)
   4216  1.16.2.2  christos {
   4217  1.16.2.2  christos 	struct audio_softc *sc;
   4218  1.16.2.2  christos 	u_int newbufsize;
   4219  1.16.2.2  christos 	u_int oldblksize;
   4220  1.16.2.2  christos 	u_int len;
   4221  1.16.2.2  christos 	int error;
   4222  1.16.2.2  christos 
   4223  1.16.2.2  christos 	KASSERT(track);
   4224  1.16.2.2  christos 	sc = track->mixer->sc;
   4225  1.16.2.2  christos 
   4226  1.16.2.2  christos 	/* usrbuf is the closest buffer to the userland. */
   4227  1.16.2.2  christos 	track->usrbuf.fmt = *usrfmt;
   4228  1.16.2.2  christos 
   4229  1.16.2.2  christos 	/*
   4230  1.16.2.2  christos 	 * For references, one block size (in 40msec) is:
   4231  1.16.2.2  christos 	 *  320 bytes    = 204 blocks/64KB for mulaw/8kHz/1ch
   4232  1.16.2.2  christos 	 *  7680 bytes   = 8 blocks/64KB for s16/48kHz/2ch
   4233  1.16.2.2  christos 	 *  30720 bytes  = 90 KB/3blocks for s16/48kHz/8ch
   4234  1.16.2.2  christos 	 *  61440 bytes  = 180 KB/3blocks for s16/96kHz/8ch
   4235  1.16.2.2  christos 	 *  245760 bytes = 720 KB/3blocks for s32/192kHz/8ch
   4236  1.16.2.2  christos 	 *
   4237  1.16.2.2  christos 	 * For example,
   4238  1.16.2.2  christos 	 * 1) If usrbuf_blksize = 7056 (s16/44.1k/2ch) and PAGE_SIZE = 8192,
   4239  1.16.2.2  christos 	 *     newbufsize = rounddown(65536 / 7056) = 63504
   4240  1.16.2.2  christos 	 *     newvsize = roundup2(63504, PAGE_SIZE) = 65536
   4241  1.16.2.2  christos 	 *    Therefore it maps 8 * 8K pages and usrbuf->capacity = 63504.
   4242  1.16.2.2  christos 	 *
   4243  1.16.2.2  christos 	 * 2) If usrbuf_blksize = 7680 (s16/48k/2ch) and PAGE_SIZE = 4096,
   4244  1.16.2.2  christos 	 *     newbufsize = rounddown(65536 / 7680) = 61440
   4245  1.16.2.2  christos 	 *     newvsize = roundup2(61440, PAGE_SIZE) = 61440 (= 15 pages)
   4246  1.16.2.2  christos 	 *    Therefore it maps 15 * 4K pages and usrbuf->capacity = 61440.
   4247  1.16.2.2  christos 	 */
   4248  1.16.2.2  christos 	oldblksize = track->usrbuf_blksize;
   4249  1.16.2.2  christos 	track->usrbuf_blksize = frametobyte(&track->usrbuf.fmt,
   4250  1.16.2.2  christos 	    frame_per_block(track->mixer, &track->usrbuf.fmt));
   4251  1.16.2.2  christos 	track->usrbuf.head = 0;
   4252  1.16.2.2  christos 	track->usrbuf.used = 0;
   4253  1.16.2.2  christos 	newbufsize = MAX(track->usrbuf_blksize * AUMINNOBLK, 65536);
   4254  1.16.2.2  christos 	newbufsize = rounddown(newbufsize, track->usrbuf_blksize);
   4255  1.16.2.2  christos 	error = audio_realloc_usrbuf(track, newbufsize);
   4256  1.16.2.2  christos 	if (error) {
   4257  1.16.2.2  christos 		device_printf(sc->sc_dev, "malloc usrbuf(%d) failed\n",
   4258  1.16.2.2  christos 		    newbufsize);
   4259  1.16.2.2  christos 		goto error;
   4260  1.16.2.2  christos 	}
   4261  1.16.2.2  christos 
   4262  1.16.2.2  christos 	/* Recalc water mark. */
   4263  1.16.2.2  christos 	if (track->usrbuf_blksize != oldblksize) {
   4264  1.16.2.2  christos 		if (audio_track_is_playback(track)) {
   4265  1.16.2.2  christos 			/* Set high at 100%, low at 75%.  */
   4266  1.16.2.2  christos 			track->usrbuf_usedhigh = track->usrbuf.capacity;
   4267  1.16.2.2  christos 			track->usrbuf_usedlow = track->usrbuf.capacity * 3 / 4;
   4268  1.16.2.2  christos 		} else {
   4269  1.16.2.2  christos 			/* Set high at 100% minus 1block(?), low at 0% */
   4270  1.16.2.2  christos 			track->usrbuf_usedhigh = track->usrbuf.capacity -
   4271  1.16.2.2  christos 			    track->usrbuf_blksize;
   4272  1.16.2.2  christos 			track->usrbuf_usedlow = 0;
   4273  1.16.2.2  christos 		}
   4274  1.16.2.2  christos 	}
   4275  1.16.2.2  christos 
   4276  1.16.2.2  christos 	/* Stage buffer */
   4277  1.16.2.2  christos 	audio_ring_t *last_dst = &track->outbuf;
   4278  1.16.2.2  christos 	if (audio_track_is_playback(track)) {
   4279  1.16.2.2  christos 		/* On playback, initialize from the mixer side in order. */
   4280  1.16.2.2  christos 		track->inputfmt = *usrfmt;
   4281  1.16.2.2  christos 		track->outbuf.fmt =  track->mixer->track_fmt;
   4282  1.16.2.2  christos 
   4283  1.16.2.2  christos 		if ((error = audio_track_init_freq(track, &last_dst)) != 0)
   4284  1.16.2.2  christos 			goto error;
   4285  1.16.2.2  christos 		if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
   4286  1.16.2.2  christos 			goto error;
   4287  1.16.2.2  christos 		if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
   4288  1.16.2.2  christos 			goto error;
   4289  1.16.2.2  christos 		if ((error = audio_track_init_codec(track, &last_dst)) != 0)
   4290  1.16.2.2  christos 			goto error;
   4291  1.16.2.2  christos 	} else {
   4292  1.16.2.2  christos 		/* On recording, initialize from userland side in order. */
   4293  1.16.2.2  christos 		track->inputfmt = track->mixer->track_fmt;
   4294  1.16.2.2  christos 		track->outbuf.fmt = *usrfmt;
   4295  1.16.2.2  christos 
   4296  1.16.2.2  christos 		if ((error = audio_track_init_codec(track, &last_dst)) != 0)
   4297  1.16.2.2  christos 			goto error;
   4298  1.16.2.2  christos 		if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
   4299  1.16.2.2  christos 			goto error;
   4300  1.16.2.2  christos 		if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
   4301  1.16.2.2  christos 			goto error;
   4302  1.16.2.2  christos 		if ((error = audio_track_init_freq(track, &last_dst)) != 0)
   4303  1.16.2.2  christos 			goto error;
   4304  1.16.2.2  christos 	}
   4305  1.16.2.2  christos #if 0
   4306  1.16.2.2  christos 	/* debug */
   4307  1.16.2.2  christos 	if (track->freq.filter) {
   4308  1.16.2.2  christos 		audio_print_format2("freq src", &track->freq.srcbuf.fmt);
   4309  1.16.2.2  christos 		audio_print_format2("freq dst", &track->freq.dst->fmt);
   4310  1.16.2.2  christos 	}
   4311  1.16.2.2  christos 	if (track->chmix.filter) {
   4312  1.16.2.2  christos 		audio_print_format2("chmix src", &track->chmix.srcbuf.fmt);
   4313  1.16.2.2  christos 		audio_print_format2("chmix dst", &track->chmix.dst->fmt);
   4314  1.16.2.2  christos 	}
   4315  1.16.2.2  christos 	if (track->chvol.filter) {
   4316  1.16.2.2  christos 		audio_print_format2("chvol src", &track->chvol.srcbuf.fmt);
   4317  1.16.2.2  christos 		audio_print_format2("chvol dst", &track->chvol.dst->fmt);
   4318  1.16.2.2  christos 	}
   4319  1.16.2.2  christos 	if (track->codec.filter) {
   4320  1.16.2.2  christos 		audio_print_format2("codec src", &track->codec.srcbuf.fmt);
   4321  1.16.2.2  christos 		audio_print_format2("codec dst", &track->codec.dst->fmt);
   4322  1.16.2.2  christos 	}
   4323  1.16.2.2  christos #endif
   4324  1.16.2.2  christos 
   4325  1.16.2.2  christos 	/* Stage input buffer */
   4326  1.16.2.2  christos 	track->input = last_dst;
   4327  1.16.2.2  christos 
   4328  1.16.2.2  christos 	/*
   4329  1.16.2.2  christos 	 * On the recording track, make the first stage a ring buffer.
   4330  1.16.2.2  christos 	 * XXX is there a better way?
   4331  1.16.2.2  christos 	 */
   4332  1.16.2.2  christos 	if (audio_track_is_record(track)) {
   4333  1.16.2.2  christos 		track->input->capacity = NBLKOUT *
   4334  1.16.2.2  christos 		    frame_per_block(track->mixer, &track->input->fmt);
   4335  1.16.2.2  christos 		len = auring_bytelen(track->input);
   4336  1.16.2.2  christos 		track->input->mem = audio_realloc(track->input->mem, len);
   4337  1.16.2.2  christos 	}
   4338  1.16.2.2  christos 
   4339  1.16.2.2  christos 	/*
   4340  1.16.2.2  christos 	 * Output buffer.
   4341  1.16.2.2  christos 	 * On the playback track, its capacity is NBLKOUT blocks.
   4342  1.16.2.2  christos 	 * On the recording track, its capacity is 1 block.
   4343  1.16.2.2  christos 	 */
   4344  1.16.2.2  christos 	track->outbuf.head = 0;
   4345  1.16.2.2  christos 	track->outbuf.used = 0;
   4346  1.16.2.2  christos 	track->outbuf.capacity = frame_per_block(track->mixer,
   4347  1.16.2.2  christos 	    &track->outbuf.fmt);
   4348  1.16.2.2  christos 	if (audio_track_is_playback(track))
   4349  1.16.2.2  christos 		track->outbuf.capacity *= NBLKOUT;
   4350  1.16.2.2  christos 	len = auring_bytelen(&track->outbuf);
   4351  1.16.2.2  christos 	track->outbuf.mem = audio_realloc(track->outbuf.mem, len);
   4352  1.16.2.2  christos 	if (track->outbuf.mem == NULL) {
   4353  1.16.2.2  christos 		device_printf(sc->sc_dev, "malloc outbuf(%d) failed\n", len);
   4354  1.16.2.2  christos 		error = ENOMEM;
   4355  1.16.2.2  christos 		goto error;
   4356  1.16.2.2  christos 	}
   4357  1.16.2.2  christos 
   4358  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   4359  1.16.2.2  christos 	if (audiodebug >= 3) {
   4360  1.16.2.2  christos 		struct audio_track_debugbuf m;
   4361  1.16.2.2  christos 
   4362  1.16.2.2  christos 		memset(&m, 0, sizeof(m));
   4363  1.16.2.2  christos 		snprintf(m.outbuf, sizeof(m.outbuf), " out=%d",
   4364  1.16.2.2  christos 		    track->outbuf.capacity * frametobyte(&track->outbuf.fmt,1));
   4365  1.16.2.2  christos 		if (track->freq.filter)
   4366  1.16.2.2  christos 			snprintf(m.freq, sizeof(m.freq), " freq=%d",
   4367  1.16.2.2  christos 			    track->freq.srcbuf.capacity *
   4368  1.16.2.2  christos 			    frametobyte(&track->freq.srcbuf.fmt, 1));
   4369  1.16.2.2  christos 		if (track->chmix.filter)
   4370  1.16.2.2  christos 			snprintf(m.chmix, sizeof(m.chmix), " chmix=%d",
   4371  1.16.2.2  christos 			    track->chmix.srcbuf.capacity *
   4372  1.16.2.2  christos 			    frametobyte(&track->chmix.srcbuf.fmt, 1));
   4373  1.16.2.2  christos 		if (track->chvol.filter)
   4374  1.16.2.2  christos 			snprintf(m.chvol, sizeof(m.chvol), " chvol=%d",
   4375  1.16.2.2  christos 			    track->chvol.srcbuf.capacity *
   4376  1.16.2.2  christos 			    frametobyte(&track->chvol.srcbuf.fmt, 1));
   4377  1.16.2.2  christos 		if (track->codec.filter)
   4378  1.16.2.2  christos 			snprintf(m.codec, sizeof(m.codec), " codec=%d",
   4379  1.16.2.2  christos 			    track->codec.srcbuf.capacity *
   4380  1.16.2.2  christos 			    frametobyte(&track->codec.srcbuf.fmt, 1));
   4381  1.16.2.2  christos 		snprintf(m.usrbuf, sizeof(m.usrbuf),
   4382  1.16.2.2  christos 		    " usr=%d", track->usrbuf.capacity);
   4383  1.16.2.2  christos 
   4384  1.16.2.2  christos 		if (audio_track_is_playback(track)) {
   4385  1.16.2.2  christos 			TRACET(0, track, "bufsize%s%s%s%s%s%s",
   4386  1.16.2.2  christos 			    m.outbuf, m.freq, m.chmix,
   4387  1.16.2.2  christos 			    m.chvol, m.codec, m.usrbuf);
   4388  1.16.2.2  christos 		} else {
   4389  1.16.2.2  christos 			TRACET(0, track, "bufsize%s%s%s%s%s%s",
   4390  1.16.2.2  christos 			    m.freq, m.chmix, m.chvol,
   4391  1.16.2.2  christos 			    m.codec, m.outbuf, m.usrbuf);
   4392  1.16.2.2  christos 		}
   4393  1.16.2.2  christos 	}
   4394  1.16.2.2  christos #endif
   4395  1.16.2.2  christos 	return 0;
   4396  1.16.2.2  christos 
   4397  1.16.2.2  christos error:
   4398  1.16.2.2  christos 	audio_free_usrbuf(track);
   4399  1.16.2.2  christos 	audio_free(track->codec.srcbuf.mem);
   4400  1.16.2.2  christos 	audio_free(track->chvol.srcbuf.mem);
   4401  1.16.2.2  christos 	audio_free(track->chmix.srcbuf.mem);
   4402  1.16.2.2  christos 	audio_free(track->freq.srcbuf.mem);
   4403  1.16.2.2  christos 	audio_free(track->outbuf.mem);
   4404  1.16.2.2  christos 	return error;
   4405  1.16.2.2  christos }
   4406  1.16.2.2  christos 
   4407  1.16.2.2  christos /*
   4408  1.16.2.2  christos  * Fill silence frames (as the internal format) up to 1 block
   4409  1.16.2.2  christos  * if the ring is not empty and less than 1 block.
   4410  1.16.2.2  christos  * It returns the number of appended frames.
   4411  1.16.2.2  christos  */
   4412  1.16.2.2  christos static int
   4413  1.16.2.2  christos audio_append_silence(audio_track_t *track, audio_ring_t *ring)
   4414  1.16.2.2  christos {
   4415  1.16.2.2  christos 	int fpb;
   4416  1.16.2.2  christos 	int n;
   4417  1.16.2.2  christos 
   4418  1.16.2.2  christos 	KASSERT(track);
   4419  1.16.2.2  christos 	KASSERT(audio_format2_is_internal(&ring->fmt));
   4420  1.16.2.2  christos 
   4421  1.16.2.2  christos 	/* XXX is n correct? */
   4422  1.16.2.2  christos 	/* XXX memset uses frametobyte()? */
   4423  1.16.2.2  christos 
   4424  1.16.2.2  christos 	if (ring->used == 0)
   4425  1.16.2.2  christos 		return 0;
   4426  1.16.2.2  christos 
   4427  1.16.2.2  christos 	fpb = frame_per_block(track->mixer, &ring->fmt);
   4428  1.16.2.2  christos 	if (ring->used >= fpb)
   4429  1.16.2.2  christos 		return 0;
   4430  1.16.2.2  christos 
   4431  1.16.2.2  christos 	n = (ring->capacity - ring->used) % fpb;
   4432  1.16.2.2  christos 
   4433  1.16.2.3    martin 	KASSERTMSG(auring_get_contig_free(ring) >= n,
   4434  1.16.2.3    martin 	    "auring_get_contig_free(ring)=%d n=%d",
   4435  1.16.2.3    martin 	    auring_get_contig_free(ring), n);
   4436  1.16.2.2  christos 
   4437  1.16.2.2  christos 	memset(auring_tailptr_aint(ring), 0,
   4438  1.16.2.2  christos 	    n * ring->fmt.channels * sizeof(aint_t));
   4439  1.16.2.2  christos 	auring_push(ring, n);
   4440  1.16.2.2  christos 	return n;
   4441  1.16.2.2  christos }
   4442  1.16.2.2  christos 
   4443  1.16.2.2  christos /*
   4444  1.16.2.2  christos  * Execute the conversion stage.
   4445  1.16.2.2  christos  * It prepares arg from this stage and executes stage->filter.
   4446  1.16.2.2  christos  * It must be called only if stage->filter is not NULL.
   4447  1.16.2.2  christos  *
   4448  1.16.2.2  christos  * For stages other than frequency conversion, the function increments
   4449  1.16.2.2  christos  * src and dst counters here.  For frequency conversion stage, on the
   4450  1.16.2.2  christos  * other hand, the function does not touch src and dst counters and
   4451  1.16.2.2  christos  * filter side has to increment them.
   4452  1.16.2.2  christos  */
   4453  1.16.2.2  christos static void
   4454  1.16.2.2  christos audio_apply_stage(audio_track_t *track, audio_stage_t *stage, bool isfreq)
   4455  1.16.2.2  christos {
   4456  1.16.2.2  christos 	audio_filter_arg_t *arg;
   4457  1.16.2.2  christos 	int srccount;
   4458  1.16.2.2  christos 	int dstcount;
   4459  1.16.2.2  christos 	int count;
   4460  1.16.2.2  christos 
   4461  1.16.2.2  christos 	KASSERT(track);
   4462  1.16.2.2  christos 	KASSERT(stage->filter);
   4463  1.16.2.2  christos 
   4464  1.16.2.2  christos 	srccount = auring_get_contig_used(&stage->srcbuf);
   4465  1.16.2.2  christos 	dstcount = auring_get_contig_free(stage->dst);
   4466  1.16.2.2  christos 
   4467  1.16.2.2  christos 	if (isfreq) {
   4468  1.16.2.3    martin 		KASSERTMSG(srccount > 0, "freq but srccount=%d", srccount);
   4469  1.16.2.2  christos 		count = uimin(dstcount, track->mixer->frames_per_block);
   4470  1.16.2.2  christos 	} else {
   4471  1.16.2.2  christos 		count = uimin(srccount, dstcount);
   4472  1.16.2.2  christos 	}
   4473  1.16.2.2  christos 
   4474  1.16.2.2  christos 	if (count > 0) {
   4475  1.16.2.2  christos 		arg = &stage->arg;
   4476  1.16.2.2  christos 		arg->src = auring_headptr(&stage->srcbuf);
   4477  1.16.2.2  christos 		arg->dst = auring_tailptr(stage->dst);
   4478  1.16.2.2  christos 		arg->count = count;
   4479  1.16.2.2  christos 
   4480  1.16.2.2  christos 		stage->filter(arg);
   4481  1.16.2.2  christos 
   4482  1.16.2.2  christos 		if (!isfreq) {
   4483  1.16.2.2  christos 			auring_take(&stage->srcbuf, count);
   4484  1.16.2.2  christos 			auring_push(stage->dst, count);
   4485  1.16.2.2  christos 		}
   4486  1.16.2.2  christos 	}
   4487  1.16.2.2  christos }
   4488  1.16.2.2  christos 
   4489  1.16.2.2  christos /*
   4490  1.16.2.2  christos  * Produce output buffer for playback from user input buffer.
   4491  1.16.2.2  christos  * It must be called only if usrbuf is not empty and outbuf is
   4492  1.16.2.2  christos  * available at least one free block.
   4493  1.16.2.2  christos  */
   4494  1.16.2.2  christos static void
   4495  1.16.2.2  christos audio_track_play(audio_track_t *track)
   4496  1.16.2.2  christos {
   4497  1.16.2.2  christos 	audio_ring_t *usrbuf;
   4498  1.16.2.2  christos 	audio_ring_t *input;
   4499  1.16.2.2  christos 	int count;
   4500  1.16.2.2  christos 	int framesize;
   4501  1.16.2.2  christos 	int bytes;
   4502  1.16.2.2  christos 
   4503  1.16.2.2  christos 	KASSERT(track);
   4504  1.16.2.2  christos 	KASSERT(track->lock);
   4505  1.16.2.2  christos 	TRACET(4, track, "start pstate=%d", track->pstate);
   4506  1.16.2.2  christos 
   4507  1.16.2.2  christos 	/* At this point usrbuf must not be empty. */
   4508  1.16.2.2  christos 	KASSERT(track->usrbuf.used > 0);
   4509  1.16.2.2  christos 	/* Also, outbuf must be available at least one block. */
   4510  1.16.2.2  christos 	count = auring_get_contig_free(&track->outbuf);
   4511  1.16.2.2  christos 	KASSERTMSG(count >= frame_per_block(track->mixer, &track->outbuf.fmt),
   4512  1.16.2.2  christos 	    "count=%d fpb=%d",
   4513  1.16.2.2  christos 	    count, frame_per_block(track->mixer, &track->outbuf.fmt));
   4514  1.16.2.2  christos 
   4515  1.16.2.2  christos 	/* XXX TODO: is this necessary for now? */
   4516  1.16.2.2  christos 	int track_count_0 = track->outbuf.used;
   4517  1.16.2.2  christos 
   4518  1.16.2.2  christos 	usrbuf = &track->usrbuf;
   4519  1.16.2.2  christos 	input = track->input;
   4520  1.16.2.2  christos 
   4521  1.16.2.2  christos 	/*
   4522  1.16.2.2  christos 	 * framesize is always 1 byte or more since all formats supported as
   4523  1.16.2.2  christos 	 * usrfmt(=input) have 8bit or more stride.
   4524  1.16.2.2  christos 	 */
   4525  1.16.2.2  christos 	framesize = frametobyte(&input->fmt, 1);
   4526  1.16.2.2  christos 	KASSERT(framesize >= 1);
   4527  1.16.2.2  christos 
   4528  1.16.2.2  christos 	/* The next stage of usrbuf (=input) must be available. */
   4529  1.16.2.2  christos 	KASSERT(auring_get_contig_free(input) > 0);
   4530  1.16.2.2  christos 
   4531  1.16.2.2  christos 	/*
   4532  1.16.2.2  christos 	 * Copy usrbuf up to 1block to input buffer.
   4533  1.16.2.2  christos 	 * count is the number of frames to copy from usrbuf.
   4534  1.16.2.2  christos 	 * bytes is the number of bytes to copy from usrbuf.  However it is
   4535  1.16.2.2  christos 	 * not copied less than one frame.
   4536  1.16.2.2  christos 	 */
   4537  1.16.2.2  christos 	count = uimin(usrbuf->used, track->usrbuf_blksize) / framesize;
   4538  1.16.2.2  christos 	bytes = count * framesize;
   4539  1.16.2.2  christos 
   4540  1.16.2.2  christos 	track->usrbuf_stamp += bytes;
   4541  1.16.2.2  christos 
   4542  1.16.2.2  christos 	if (usrbuf->head + bytes < usrbuf->capacity) {
   4543  1.16.2.2  christos 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   4544  1.16.2.2  christos 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   4545  1.16.2.2  christos 		    bytes);
   4546  1.16.2.2  christos 		auring_push(input, count);
   4547  1.16.2.2  christos 		auring_take(usrbuf, bytes);
   4548  1.16.2.2  christos 	} else {
   4549  1.16.2.2  christos 		int bytes1;
   4550  1.16.2.2  christos 		int bytes2;
   4551  1.16.2.2  christos 
   4552  1.16.2.2  christos 		bytes1 = auring_get_contig_used(usrbuf);
   4553  1.16.2.3    martin 		KASSERTMSG(bytes1 % framesize == 0,
   4554  1.16.2.3    martin 		    "bytes1=%d framesize=%d", bytes1, framesize);
   4555  1.16.2.2  christos 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   4556  1.16.2.2  christos 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   4557  1.16.2.2  christos 		    bytes1);
   4558  1.16.2.2  christos 		auring_push(input, bytes1 / framesize);
   4559  1.16.2.2  christos 		auring_take(usrbuf, bytes1);
   4560  1.16.2.2  christos 
   4561  1.16.2.2  christos 		bytes2 = bytes - bytes1;
   4562  1.16.2.2  christos 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
   4563  1.16.2.2  christos 		    (uint8_t *)usrbuf->mem + usrbuf->head,
   4564  1.16.2.2  christos 		    bytes2);
   4565  1.16.2.2  christos 		auring_push(input, bytes2 / framesize);
   4566  1.16.2.2  christos 		auring_take(usrbuf, bytes2);
   4567  1.16.2.2  christos 	}
   4568  1.16.2.2  christos 
   4569  1.16.2.2  christos 	/* Encoding conversion */
   4570  1.16.2.2  christos 	if (track->codec.filter)
   4571  1.16.2.2  christos 		audio_apply_stage(track, &track->codec, false);
   4572  1.16.2.2  christos 
   4573  1.16.2.2  christos 	/* Channel volume */
   4574  1.16.2.2  christos 	if (track->chvol.filter)
   4575  1.16.2.2  christos 		audio_apply_stage(track, &track->chvol, false);
   4576  1.16.2.2  christos 
   4577  1.16.2.2  christos 	/* Channel mix */
   4578  1.16.2.2  christos 	if (track->chmix.filter)
   4579  1.16.2.2  christos 		audio_apply_stage(track, &track->chmix, false);
   4580  1.16.2.2  christos 
   4581  1.16.2.2  christos 	/* Frequency conversion */
   4582  1.16.2.2  christos 	/*
   4583  1.16.2.2  christos 	 * Since the frequency conversion needs correction for each block,
   4584  1.16.2.2  christos 	 * it rounds up to 1 block.
   4585  1.16.2.2  christos 	 */
   4586  1.16.2.2  christos 	if (track->freq.filter) {
   4587  1.16.2.2  christos 		int n;
   4588  1.16.2.2  christos 		n = audio_append_silence(track, &track->freq.srcbuf);
   4589  1.16.2.2  christos 		if (n > 0) {
   4590  1.16.2.2  christos 			TRACET(4, track,
   4591  1.16.2.2  christos 			    "freq.srcbuf add silence %d -> %d/%d/%d",
   4592  1.16.2.2  christos 			    n,
   4593  1.16.2.2  christos 			    track->freq.srcbuf.head,
   4594  1.16.2.2  christos 			    track->freq.srcbuf.used,
   4595  1.16.2.2  christos 			    track->freq.srcbuf.capacity);
   4596  1.16.2.2  christos 		}
   4597  1.16.2.2  christos 		if (track->freq.srcbuf.used > 0) {
   4598  1.16.2.2  christos 			audio_apply_stage(track, &track->freq, true);
   4599  1.16.2.2  christos 		}
   4600  1.16.2.2  christos 	}
   4601  1.16.2.2  christos 
   4602  1.16.2.3    martin 	if (bytes < track->usrbuf_blksize) {
   4603  1.16.2.2  christos 		/*
   4604  1.16.2.2  christos 		 * Clear all conversion buffer pointer if the conversion was
   4605  1.16.2.2  christos 		 * not exactly one block.  These conversion stage buffers are
   4606  1.16.2.2  christos 		 * certainly circular buffers because of symmetry with the
   4607  1.16.2.2  christos 		 * previous and next stage buffer.  However, since they are
   4608  1.16.2.2  christos 		 * treated as simple contiguous buffers in operation, so head
   4609  1.16.2.2  christos 		 * always should point 0.  This may happen during drain-age.
   4610  1.16.2.2  christos 		 */
   4611  1.16.2.2  christos 		TRACET(4, track, "reset stage");
   4612  1.16.2.2  christos 		if (track->codec.filter) {
   4613  1.16.2.2  christos 			KASSERT(track->codec.srcbuf.used == 0);
   4614  1.16.2.2  christos 			track->codec.srcbuf.head = 0;
   4615  1.16.2.2  christos 		}
   4616  1.16.2.2  christos 		if (track->chvol.filter) {
   4617  1.16.2.2  christos 			KASSERT(track->chvol.srcbuf.used == 0);
   4618  1.16.2.2  christos 			track->chvol.srcbuf.head = 0;
   4619  1.16.2.2  christos 		}
   4620  1.16.2.2  christos 		if (track->chmix.filter) {
   4621  1.16.2.2  christos 			KASSERT(track->chmix.srcbuf.used == 0);
   4622  1.16.2.2  christos 			track->chmix.srcbuf.head = 0;
   4623  1.16.2.2  christos 		}
   4624  1.16.2.2  christos 		if (track->freq.filter) {
   4625  1.16.2.2  christos 			KASSERT(track->freq.srcbuf.used == 0);
   4626  1.16.2.2  christos 			track->freq.srcbuf.head = 0;
   4627  1.16.2.2  christos 		}
   4628  1.16.2.2  christos 	}
   4629  1.16.2.2  christos 
   4630  1.16.2.2  christos 	if (track->input == &track->outbuf) {
   4631  1.16.2.2  christos 		track->outputcounter = track->inputcounter;
   4632  1.16.2.2  christos 	} else {
   4633  1.16.2.2  christos 		track->outputcounter += track->outbuf.used - track_count_0;
   4634  1.16.2.2  christos 	}
   4635  1.16.2.2  christos 
   4636  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   4637  1.16.2.2  christos 	if (audiodebug >= 3) {
   4638  1.16.2.2  christos 		struct audio_track_debugbuf m;
   4639  1.16.2.2  christos 		audio_track_bufstat(track, &m);
   4640  1.16.2.2  christos 		TRACET(0, track, "end%s%s%s%s%s%s",
   4641  1.16.2.2  christos 		    m.outbuf, m.freq, m.chvol, m.chmix, m.codec, m.usrbuf);
   4642  1.16.2.2  christos 	}
   4643  1.16.2.2  christos #endif
   4644  1.16.2.2  christos }
   4645  1.16.2.2  christos 
   4646  1.16.2.2  christos /*
   4647  1.16.2.2  christos  * Produce user output buffer for recording from input buffer.
   4648  1.16.2.2  christos  */
   4649  1.16.2.2  christos static void
   4650  1.16.2.2  christos audio_track_record(audio_track_t *track)
   4651  1.16.2.2  christos {
   4652  1.16.2.2  christos 	audio_ring_t *outbuf;
   4653  1.16.2.2  christos 	audio_ring_t *usrbuf;
   4654  1.16.2.2  christos 	int count;
   4655  1.16.2.2  christos 	int bytes;
   4656  1.16.2.2  christos 	int framesize;
   4657  1.16.2.2  christos 
   4658  1.16.2.2  christos 	KASSERT(track);
   4659  1.16.2.2  christos 	KASSERT(track->lock);
   4660  1.16.2.2  christos 
   4661  1.16.2.2  christos 	/* Number of frames to process */
   4662  1.16.2.2  christos 	count = auring_get_contig_used(track->input);
   4663  1.16.2.2  christos 	count = uimin(count, track->mixer->frames_per_block);
   4664  1.16.2.2  christos 	if (count == 0) {
   4665  1.16.2.2  christos 		TRACET(4, track, "count == 0");
   4666  1.16.2.2  christos 		return;
   4667  1.16.2.2  christos 	}
   4668  1.16.2.2  christos 
   4669  1.16.2.2  christos 	/* Frequency conversion */
   4670  1.16.2.2  christos 	if (track->freq.filter) {
   4671  1.16.2.2  christos 		if (track->freq.srcbuf.used > 0) {
   4672  1.16.2.2  christos 			audio_apply_stage(track, &track->freq, true);
   4673  1.16.2.2  christos 			/* XXX should input of freq be from beginning of buf? */
   4674  1.16.2.2  christos 		}
   4675  1.16.2.2  christos 	}
   4676  1.16.2.2  christos 
   4677  1.16.2.2  christos 	/* Channel mix */
   4678  1.16.2.2  christos 	if (track->chmix.filter)
   4679  1.16.2.2  christos 		audio_apply_stage(track, &track->chmix, false);
   4680  1.16.2.2  christos 
   4681  1.16.2.2  christos 	/* Channel volume */
   4682  1.16.2.2  christos 	if (track->chvol.filter)
   4683  1.16.2.2  christos 		audio_apply_stage(track, &track->chvol, false);
   4684  1.16.2.2  christos 
   4685  1.16.2.2  christos 	/* Encoding conversion */
   4686  1.16.2.2  christos 	if (track->codec.filter)
   4687  1.16.2.2  christos 		audio_apply_stage(track, &track->codec, false);
   4688  1.16.2.2  christos 
   4689  1.16.2.2  christos 	/* Copy outbuf to usrbuf */
   4690  1.16.2.2  christos 	outbuf = &track->outbuf;
   4691  1.16.2.2  christos 	usrbuf = &track->usrbuf;
   4692  1.16.2.2  christos 	/*
   4693  1.16.2.2  christos 	 * framesize is always 1 byte or more since all formats supported
   4694  1.16.2.2  christos 	 * as usrfmt(=output) have 8bit or more stride.
   4695  1.16.2.2  christos 	 */
   4696  1.16.2.2  christos 	framesize = frametobyte(&outbuf->fmt, 1);
   4697  1.16.2.2  christos 	KASSERT(framesize >= 1);
   4698  1.16.2.2  christos 	/*
   4699  1.16.2.2  christos 	 * count is the number of frames to copy to usrbuf.
   4700  1.16.2.2  christos 	 * bytes is the number of bytes to copy to usrbuf.
   4701  1.16.2.2  christos 	 */
   4702  1.16.2.2  christos 	count = outbuf->used;
   4703  1.16.2.2  christos 	count = uimin(count,
   4704  1.16.2.2  christos 	    (track->usrbuf_usedhigh - usrbuf->used) / framesize);
   4705  1.16.2.2  christos 	bytes = count * framesize;
   4706  1.16.2.2  christos 	if (auring_tail(usrbuf) + bytes < usrbuf->capacity) {
   4707  1.16.2.2  christos 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   4708  1.16.2.2  christos 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   4709  1.16.2.2  christos 		    bytes);
   4710  1.16.2.2  christos 		auring_push(usrbuf, bytes);
   4711  1.16.2.2  christos 		auring_take(outbuf, count);
   4712  1.16.2.2  christos 	} else {
   4713  1.16.2.2  christos 		int bytes1;
   4714  1.16.2.2  christos 		int bytes2;
   4715  1.16.2.2  christos 
   4716  1.16.2.3    martin 		bytes1 = auring_get_contig_free(usrbuf);
   4717  1.16.2.3    martin 		KASSERTMSG(bytes1 % framesize == 0,
   4718  1.16.2.3    martin 		    "bytes1=%d framesize=%d", bytes1, framesize);
   4719  1.16.2.2  christos 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   4720  1.16.2.2  christos 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   4721  1.16.2.2  christos 		    bytes1);
   4722  1.16.2.2  christos 		auring_push(usrbuf, bytes1);
   4723  1.16.2.2  christos 		auring_take(outbuf, bytes1 / framesize);
   4724  1.16.2.2  christos 
   4725  1.16.2.2  christos 		bytes2 = bytes - bytes1;
   4726  1.16.2.2  christos 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
   4727  1.16.2.2  christos 		    (uint8_t *)outbuf->mem + outbuf->head * framesize,
   4728  1.16.2.2  christos 		    bytes2);
   4729  1.16.2.2  christos 		auring_push(usrbuf, bytes2);
   4730  1.16.2.2  christos 		auring_take(outbuf, bytes2 / framesize);
   4731  1.16.2.2  christos 	}
   4732  1.16.2.2  christos 
   4733  1.16.2.2  christos 	/* XXX TODO: any counters here? */
   4734  1.16.2.2  christos 
   4735  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   4736  1.16.2.2  christos 	if (audiodebug >= 3) {
   4737  1.16.2.2  christos 		struct audio_track_debugbuf m;
   4738  1.16.2.2  christos 		audio_track_bufstat(track, &m);
   4739  1.16.2.2  christos 		TRACET(0, track, "end%s%s%s%s%s%s",
   4740  1.16.2.2  christos 		    m.freq, m.chvol, m.chmix, m.codec, m.outbuf, m.usrbuf);
   4741  1.16.2.2  christos 	}
   4742  1.16.2.2  christos #endif
   4743  1.16.2.2  christos }
   4744  1.16.2.2  christos 
   4745  1.16.2.2  christos /*
   4746  1.16.2.2  christos  * Calcurate blktime [msec] from mixer(.hwbuf.fmt).
   4747  1.16.2.3    martin  * Must be called with sc_exlock held.
   4748  1.16.2.2  christos  */
   4749  1.16.2.2  christos static u_int
   4750  1.16.2.2  christos audio_mixer_calc_blktime(struct audio_softc *sc, audio_trackmixer_t *mixer)
   4751  1.16.2.2  christos {
   4752  1.16.2.2  christos 	audio_format2_t *fmt;
   4753  1.16.2.2  christos 	u_int blktime;
   4754  1.16.2.2  christos 	u_int frames_per_block;
   4755  1.16.2.2  christos 
   4756  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   4757  1.16.2.2  christos 
   4758  1.16.2.2  christos 	fmt = &mixer->hwbuf.fmt;
   4759  1.16.2.2  christos 	blktime = sc->sc_blk_ms;
   4760  1.16.2.2  christos 
   4761  1.16.2.2  christos 	/*
   4762  1.16.2.2  christos 	 * If stride is not multiples of 8, special treatment is necessary.
   4763  1.16.2.2  christos 	 * For now, it is only x68k's vs(4), 4 bit/sample ADPCM.
   4764  1.16.2.2  christos 	 */
   4765  1.16.2.2  christos 	if (fmt->stride == 4) {
   4766  1.16.2.2  christos 		frames_per_block = fmt->sample_rate * blktime / 1000;
   4767  1.16.2.2  christos 		if ((frames_per_block & 1) != 0)
   4768  1.16.2.2  christos 			blktime *= 2;
   4769  1.16.2.2  christos 	}
   4770  1.16.2.2  christos #ifdef DIAGNOSTIC
   4771  1.16.2.2  christos 	else if (fmt->stride % NBBY != 0) {
   4772  1.16.2.2  christos 		panic("unsupported HW stride %d", fmt->stride);
   4773  1.16.2.2  christos 	}
   4774  1.16.2.2  christos #endif
   4775  1.16.2.2  christos 
   4776  1.16.2.2  christos 	return blktime;
   4777  1.16.2.2  christos }
   4778  1.16.2.2  christos 
   4779  1.16.2.2  christos /*
   4780  1.16.2.2  christos  * Initialize the mixer corresponding to the mode.
   4781  1.16.2.2  christos  * Set AUMODE_PLAY to the 'mode' for playback or AUMODE_RECORD for recording.
   4782  1.16.2.2  christos  * sc->sc_[pr]mixer (corresponding to the 'mode') must be zero-filled.
   4783  1.16.2.3    martin  * This function returns 0 on successful.  Otherwise returns errno.
   4784  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   4785  1.16.2.2  christos  */
   4786  1.16.2.2  christos static int
   4787  1.16.2.2  christos audio_mixer_init(struct audio_softc *sc, int mode,
   4788  1.16.2.2  christos 	const audio_format2_t *hwfmt, const audio_filter_reg_t *reg)
   4789  1.16.2.2  christos {
   4790  1.16.2.2  christos 	char codecbuf[64];
   4791  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   4792  1.16.2.2  christos 	void (*softint_handler)(void *);
   4793  1.16.2.2  christos 	int len;
   4794  1.16.2.2  christos 	int blksize;
   4795  1.16.2.2  christos 	int capacity;
   4796  1.16.2.2  christos 	size_t bufsize;
   4797  1.16.2.2  christos 	int hwblks;
   4798  1.16.2.2  christos 	int blkms;
   4799  1.16.2.2  christos 	int error;
   4800  1.16.2.2  christos 
   4801  1.16.2.2  christos 	KASSERT(hwfmt != NULL);
   4802  1.16.2.2  christos 	KASSERT(reg != NULL);
   4803  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   4804  1.16.2.2  christos 
   4805  1.16.2.2  christos 	error = 0;
   4806  1.16.2.2  christos 	if (mode == AUMODE_PLAY)
   4807  1.16.2.2  christos 		mixer = sc->sc_pmixer;
   4808  1.16.2.2  christos 	else
   4809  1.16.2.2  christos 		mixer = sc->sc_rmixer;
   4810  1.16.2.2  christos 
   4811  1.16.2.2  christos 	mixer->sc = sc;
   4812  1.16.2.2  christos 	mixer->mode = mode;
   4813  1.16.2.2  christos 
   4814  1.16.2.2  christos 	mixer->hwbuf.fmt = *hwfmt;
   4815  1.16.2.2  christos 	mixer->volume = 256;
   4816  1.16.2.2  christos 	mixer->blktime_d = 1000;
   4817  1.16.2.2  christos 	mixer->blktime_n = audio_mixer_calc_blktime(sc, mixer);
   4818  1.16.2.2  christos 	sc->sc_blk_ms = mixer->blktime_n;
   4819  1.16.2.2  christos 	hwblks = NBLKHW;
   4820  1.16.2.2  christos 
   4821  1.16.2.2  christos 	mixer->frames_per_block = frame_per_block(mixer, &mixer->hwbuf.fmt);
   4822  1.16.2.2  christos 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   4823  1.16.2.2  christos 	if (sc->hw_if->round_blocksize) {
   4824  1.16.2.2  christos 		int rounded;
   4825  1.16.2.2  christos 		audio_params_t p = format2_to_params(&mixer->hwbuf.fmt);
   4826  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   4827  1.16.2.2  christos 		rounded = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
   4828  1.16.2.2  christos 		    mode, &p);
   4829  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   4830  1.16.2.3    martin 		TRACE(1, "round_blocksize %d -> %d", blksize, rounded);
   4831  1.16.2.2  christos 		if (rounded != blksize) {
   4832  1.16.2.2  christos 			if ((rounded * NBBY) % (mixer->hwbuf.fmt.stride *
   4833  1.16.2.2  christos 			    mixer->hwbuf.fmt.channels) != 0) {
   4834  1.16.2.2  christos 				device_printf(sc->sc_dev,
   4835  1.16.2.3    martin 				    "round_blocksize must return blocksize "
   4836  1.16.2.3    martin 				    "divisible by framesize: "
   4837  1.16.2.3    martin 				    "blksize=%d rounded=%d "
   4838  1.16.2.3    martin 				    "stride=%ubit channels=%u\n",
   4839  1.16.2.3    martin 				    blksize, rounded,
   4840  1.16.2.3    martin 				    mixer->hwbuf.fmt.stride,
   4841  1.16.2.3    martin 				    mixer->hwbuf.fmt.channels);
   4842  1.16.2.2  christos 				return EINVAL;
   4843  1.16.2.2  christos 			}
   4844  1.16.2.2  christos 			/* Recalculation */
   4845  1.16.2.2  christos 			blksize = rounded;
   4846  1.16.2.2  christos 			mixer->frames_per_block = blksize * NBBY /
   4847  1.16.2.2  christos 			    (mixer->hwbuf.fmt.stride *
   4848  1.16.2.2  christos 			     mixer->hwbuf.fmt.channels);
   4849  1.16.2.2  christos 		}
   4850  1.16.2.2  christos 	}
   4851  1.16.2.2  christos 	mixer->blktime_n = mixer->frames_per_block;
   4852  1.16.2.2  christos 	mixer->blktime_d = mixer->hwbuf.fmt.sample_rate;
   4853  1.16.2.2  christos 
   4854  1.16.2.2  christos 	capacity = mixer->frames_per_block * hwblks;
   4855  1.16.2.2  christos 	bufsize = frametobyte(&mixer->hwbuf.fmt, capacity);
   4856  1.16.2.2  christos 	if (sc->hw_if->round_buffersize) {
   4857  1.16.2.2  christos 		size_t rounded;
   4858  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   4859  1.16.2.2  christos 		rounded = sc->hw_if->round_buffersize(sc->hw_hdl, mode,
   4860  1.16.2.2  christos 		    bufsize);
   4861  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   4862  1.16.2.3    martin 		TRACE(1, "round_buffersize %zd -> %zd", bufsize, rounded);
   4863  1.16.2.2  christos 		if (rounded < bufsize) {
   4864  1.16.2.2  christos 			/* buffersize needs NBLKHW blocks at least. */
   4865  1.16.2.2  christos 			device_printf(sc->sc_dev,
   4866  1.16.2.2  christos 			    "buffersize too small: buffersize=%zd blksize=%d\n",
   4867  1.16.2.2  christos 			    rounded, blksize);
   4868  1.16.2.2  christos 			return EINVAL;
   4869  1.16.2.2  christos 		}
   4870  1.16.2.2  christos 		if (rounded % blksize != 0) {
   4871  1.16.2.2  christos 			/* buffersize/blksize constraint mismatch? */
   4872  1.16.2.2  christos 			device_printf(sc->sc_dev,
   4873  1.16.2.2  christos 			    "buffersize must be multiple of blksize: "
   4874  1.16.2.2  christos 			    "buffersize=%zu blksize=%d\n",
   4875  1.16.2.2  christos 			    rounded, blksize);
   4876  1.16.2.2  christos 			return EINVAL;
   4877  1.16.2.2  christos 		}
   4878  1.16.2.2  christos 		if (rounded != bufsize) {
   4879  1.16.2.2  christos 			/* Recalcuration */
   4880  1.16.2.2  christos 			bufsize = rounded;
   4881  1.16.2.2  christos 			hwblks = bufsize / blksize;
   4882  1.16.2.2  christos 			capacity = mixer->frames_per_block * hwblks;
   4883  1.16.2.2  christos 		}
   4884  1.16.2.2  christos 	}
   4885  1.16.2.3    martin 	TRACE(1, "buffersize for %s = %zu",
   4886  1.16.2.2  christos 	    (mode == AUMODE_PLAY) ? "playback" : "recording",
   4887  1.16.2.2  christos 	    bufsize);
   4888  1.16.2.2  christos 	mixer->hwbuf.capacity = capacity;
   4889  1.16.2.2  christos 
   4890  1.16.2.2  christos 	if (sc->hw_if->allocm) {
   4891  1.16.2.3    martin 		/* sc_lock is not necessary for allocm */
   4892  1.16.2.2  christos 		mixer->hwbuf.mem = sc->hw_if->allocm(sc->hw_hdl, mode, bufsize);
   4893  1.16.2.2  christos 		if (mixer->hwbuf.mem == NULL) {
   4894  1.16.2.2  christos 			device_printf(sc->sc_dev, "%s: allocm(%zu) failed\n",
   4895  1.16.2.2  christos 			    __func__, bufsize);
   4896  1.16.2.2  christos 			return ENOMEM;
   4897  1.16.2.2  christos 		}
   4898  1.16.2.2  christos 	} else {
   4899  1.16.2.3    martin 		mixer->hwbuf.mem = kmem_alloc(bufsize, KM_SLEEP);
   4900  1.16.2.2  christos 	}
   4901  1.16.2.2  christos 
   4902  1.16.2.2  christos 	/* From here, audio_mixer_destroy is necessary to exit. */
   4903  1.16.2.2  christos 	if (mode == AUMODE_PLAY) {
   4904  1.16.2.2  christos 		cv_init(&mixer->outcv, "audiowr");
   4905  1.16.2.2  christos 	} else {
   4906  1.16.2.2  christos 		cv_init(&mixer->outcv, "audiord");
   4907  1.16.2.2  christos 	}
   4908  1.16.2.2  christos 
   4909  1.16.2.2  christos 	if (mode == AUMODE_PLAY) {
   4910  1.16.2.2  christos 		softint_handler = audio_softintr_wr;
   4911  1.16.2.2  christos 	} else {
   4912  1.16.2.2  christos 		softint_handler = audio_softintr_rd;
   4913  1.16.2.2  christos 	}
   4914  1.16.2.2  christos 	mixer->sih = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
   4915  1.16.2.2  christos 	    softint_handler, sc);
   4916  1.16.2.2  christos 	if (mixer->sih == NULL) {
   4917  1.16.2.2  christos 		device_printf(sc->sc_dev, "softint_establish failed\n");
   4918  1.16.2.2  christos 		goto abort;
   4919  1.16.2.2  christos 	}
   4920  1.16.2.2  christos 
   4921  1.16.2.2  christos 	mixer->track_fmt.encoding = AUDIO_ENCODING_SLINEAR_NE;
   4922  1.16.2.2  christos 	mixer->track_fmt.precision = AUDIO_INTERNAL_BITS;
   4923  1.16.2.2  christos 	mixer->track_fmt.stride = AUDIO_INTERNAL_BITS;
   4924  1.16.2.2  christos 	mixer->track_fmt.channels = mixer->hwbuf.fmt.channels;
   4925  1.16.2.2  christos 	mixer->track_fmt.sample_rate = mixer->hwbuf.fmt.sample_rate;
   4926  1.16.2.2  christos 
   4927  1.16.2.2  christos 	if (mixer->hwbuf.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
   4928  1.16.2.2  christos 	    mixer->hwbuf.fmt.precision == AUDIO_INTERNAL_BITS) {
   4929  1.16.2.2  christos 		mixer->swap_endian = true;
   4930  1.16.2.2  christos 		TRACE(1, "swap_endian");
   4931  1.16.2.2  christos 	}
   4932  1.16.2.2  christos 
   4933  1.16.2.2  christos 	if (mode == AUMODE_PLAY) {
   4934  1.16.2.2  christos 		/* Mixing buffer */
   4935  1.16.2.2  christos 		mixer->mixfmt = mixer->track_fmt;
   4936  1.16.2.2  christos 		mixer->mixfmt.precision *= 2;
   4937  1.16.2.2  christos 		mixer->mixfmt.stride *= 2;
   4938  1.16.2.2  christos 		/* XXX TODO: use some macros? */
   4939  1.16.2.2  christos 		len = mixer->frames_per_block * mixer->mixfmt.channels *
   4940  1.16.2.2  christos 		    mixer->mixfmt.stride / NBBY;
   4941  1.16.2.2  christos 		mixer->mixsample = audio_realloc(mixer->mixsample, len);
   4942  1.16.2.2  christos 	} else {
   4943  1.16.2.2  christos 		/* No mixing buffer for recording */
   4944  1.16.2.2  christos 	}
   4945  1.16.2.2  christos 
   4946  1.16.2.2  christos 	if (reg->codec) {
   4947  1.16.2.2  christos 		mixer->codec = reg->codec;
   4948  1.16.2.2  christos 		mixer->codecarg.context = reg->context;
   4949  1.16.2.2  christos 		if (mode == AUMODE_PLAY) {
   4950  1.16.2.2  christos 			mixer->codecarg.srcfmt = &mixer->track_fmt;
   4951  1.16.2.2  christos 			mixer->codecarg.dstfmt = &mixer->hwbuf.fmt;
   4952  1.16.2.2  christos 		} else {
   4953  1.16.2.2  christos 			mixer->codecarg.srcfmt = &mixer->hwbuf.fmt;
   4954  1.16.2.2  christos 			mixer->codecarg.dstfmt = &mixer->track_fmt;
   4955  1.16.2.2  christos 		}
   4956  1.16.2.2  christos 		mixer->codecbuf.fmt = mixer->track_fmt;
   4957  1.16.2.2  christos 		mixer->codecbuf.capacity = mixer->frames_per_block;
   4958  1.16.2.2  christos 		len = auring_bytelen(&mixer->codecbuf);
   4959  1.16.2.2  christos 		mixer->codecbuf.mem = audio_realloc(mixer->codecbuf.mem, len);
   4960  1.16.2.2  christos 		if (mixer->codecbuf.mem == NULL) {
   4961  1.16.2.2  christos 			device_printf(sc->sc_dev,
   4962  1.16.2.2  christos 			    "%s: malloc codecbuf(%d) failed\n",
   4963  1.16.2.2  christos 			    __func__, len);
   4964  1.16.2.2  christos 			error = ENOMEM;
   4965  1.16.2.2  christos 			goto abort;
   4966  1.16.2.2  christos 		}
   4967  1.16.2.2  christos 	}
   4968  1.16.2.2  christos 
   4969  1.16.2.2  christos 	/* Succeeded so display it. */
   4970  1.16.2.2  christos 	codecbuf[0] = '\0';
   4971  1.16.2.2  christos 	if (mixer->codec || mixer->swap_endian) {
   4972  1.16.2.2  christos 		snprintf(codecbuf, sizeof(codecbuf), " %s %s:%d",
   4973  1.16.2.2  christos 		    (mode == AUMODE_PLAY) ? "->" : "<-",
   4974  1.16.2.2  christos 		    audio_encoding_name(mixer->hwbuf.fmt.encoding),
   4975  1.16.2.2  christos 		    mixer->hwbuf.fmt.precision);
   4976  1.16.2.2  christos 	}
   4977  1.16.2.2  christos 	blkms = mixer->blktime_n * 1000 / mixer->blktime_d;
   4978  1.16.2.2  christos 	aprint_normal_dev(sc->sc_dev, "%s:%d%s %dch %dHz, blk %dms for %s\n",
   4979  1.16.2.2  christos 	    audio_encoding_name(mixer->track_fmt.encoding),
   4980  1.16.2.2  christos 	    mixer->track_fmt.precision,
   4981  1.16.2.2  christos 	    codecbuf,
   4982  1.16.2.2  christos 	    mixer->track_fmt.channels,
   4983  1.16.2.2  christos 	    mixer->track_fmt.sample_rate,
   4984  1.16.2.2  christos 	    blkms,
   4985  1.16.2.2  christos 	    (mode == AUMODE_PLAY) ? "playback" : "recording");
   4986  1.16.2.2  christos 
   4987  1.16.2.2  christos 	return 0;
   4988  1.16.2.2  christos 
   4989  1.16.2.2  christos abort:
   4990  1.16.2.2  christos 	audio_mixer_destroy(sc, mixer);
   4991  1.16.2.2  christos 	return error;
   4992  1.16.2.2  christos }
   4993  1.16.2.2  christos 
   4994  1.16.2.2  christos /*
   4995  1.16.2.2  christos  * Releases all resources of 'mixer'.
   4996  1.16.2.2  christos  * Note that it does not release the memory area of 'mixer' itself.
   4997  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   4998  1.16.2.2  christos  */
   4999  1.16.2.2  christos static void
   5000  1.16.2.2  christos audio_mixer_destroy(struct audio_softc *sc, audio_trackmixer_t *mixer)
   5001  1.16.2.2  christos {
   5002  1.16.2.3    martin 	int bufsize;
   5003  1.16.2.2  christos 
   5004  1.16.2.3    martin 	KASSERT(sc->sc_exlock == 1);
   5005  1.16.2.2  christos 
   5006  1.16.2.3    martin 	bufsize = frametobyte(&mixer->hwbuf.fmt, mixer->hwbuf.capacity);
   5007  1.16.2.2  christos 
   5008  1.16.2.2  christos 	if (mixer->hwbuf.mem != NULL) {
   5009  1.16.2.2  christos 		if (sc->hw_if->freem) {
   5010  1.16.2.3    martin 			/* sc_lock is not necessary for freem */
   5011  1.16.2.3    martin 			sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, bufsize);
   5012  1.16.2.2  christos 		} else {
   5013  1.16.2.3    martin 			kmem_free(mixer->hwbuf.mem, bufsize);
   5014  1.16.2.2  christos 		}
   5015  1.16.2.2  christos 		mixer->hwbuf.mem = NULL;
   5016  1.16.2.2  christos 	}
   5017  1.16.2.2  christos 
   5018  1.16.2.2  christos 	audio_free(mixer->codecbuf.mem);
   5019  1.16.2.2  christos 	audio_free(mixer->mixsample);
   5020  1.16.2.2  christos 
   5021  1.16.2.2  christos 	cv_destroy(&mixer->outcv);
   5022  1.16.2.2  christos 
   5023  1.16.2.2  christos 	if (mixer->sih) {
   5024  1.16.2.2  christos 		softint_disestablish(mixer->sih);
   5025  1.16.2.2  christos 		mixer->sih = NULL;
   5026  1.16.2.2  christos 	}
   5027  1.16.2.2  christos }
   5028  1.16.2.2  christos 
   5029  1.16.2.2  christos /*
   5030  1.16.2.2  christos  * Starts playback mixer.
   5031  1.16.2.2  christos  * Must be called only if sc_pbusy is false.
   5032  1.16.2.3    martin  * Must be called with sc_lock && sc_exlock held.
   5033  1.16.2.2  christos  * Must not be called from the interrupt context.
   5034  1.16.2.2  christos  */
   5035  1.16.2.2  christos static void
   5036  1.16.2.2  christos audio_pmixer_start(struct audio_softc *sc, bool force)
   5037  1.16.2.2  christos {
   5038  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5039  1.16.2.2  christos 	int minimum;
   5040  1.16.2.2  christos 
   5041  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   5042  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   5043  1.16.2.2  christos 	KASSERT(sc->sc_pbusy == false);
   5044  1.16.2.2  christos 
   5045  1.16.2.2  christos 	mutex_enter(sc->sc_intr_lock);
   5046  1.16.2.2  christos 
   5047  1.16.2.2  christos 	mixer = sc->sc_pmixer;
   5048  1.16.2.2  christos 	TRACE(2, "%smixseq=%d hwseq=%d hwbuf=%d/%d/%d%s",
   5049  1.16.2.2  christos 	    (audiodebug >= 3) ? "begin " : "",
   5050  1.16.2.2  christos 	    (int)mixer->mixseq, (int)mixer->hwseq,
   5051  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
   5052  1.16.2.2  christos 	    force ? " force" : "");
   5053  1.16.2.2  christos 
   5054  1.16.2.2  christos 	/* Need two blocks to start normally. */
   5055  1.16.2.2  christos 	minimum = (force) ? 1 : 2;
   5056  1.16.2.2  christos 	while (mixer->hwbuf.used < mixer->frames_per_block * minimum) {
   5057  1.16.2.2  christos 		audio_pmixer_process(sc);
   5058  1.16.2.2  christos 	}
   5059  1.16.2.2  christos 
   5060  1.16.2.2  christos 	/* Start output */
   5061  1.16.2.2  christos 	audio_pmixer_output(sc);
   5062  1.16.2.2  christos 	sc->sc_pbusy = true;
   5063  1.16.2.2  christos 
   5064  1.16.2.2  christos 	TRACE(3, "end   mixseq=%d hwseq=%d hwbuf=%d/%d/%d",
   5065  1.16.2.2  christos 	    (int)mixer->mixseq, (int)mixer->hwseq,
   5066  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5067  1.16.2.2  christos 
   5068  1.16.2.2  christos 	mutex_exit(sc->sc_intr_lock);
   5069  1.16.2.2  christos }
   5070  1.16.2.2  christos 
   5071  1.16.2.2  christos /*
   5072  1.16.2.2  christos  * When playing back with MD filter:
   5073  1.16.2.2  christos  *
   5074  1.16.2.2  christos  *           track track ...
   5075  1.16.2.2  christos  *               v v
   5076  1.16.2.2  christos  *                +  mix (with aint2_t)
   5077  1.16.2.2  christos  *                |  master volume (with aint2_t)
   5078  1.16.2.2  christos  *                v
   5079  1.16.2.2  christos  *    mixsample [::::]                  wide-int 1 block (ring) buffer
   5080  1.16.2.2  christos  *                |
   5081  1.16.2.2  christos  *                |  convert aint2_t -> aint_t
   5082  1.16.2.2  christos  *                v
   5083  1.16.2.2  christos  *    codecbuf  [....]                  1 block (ring) buffer
   5084  1.16.2.2  christos  *                |
   5085  1.16.2.2  christos  *                |  convert to hw format
   5086  1.16.2.2  christos  *                v
   5087  1.16.2.2  christos  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5088  1.16.2.2  christos  *
   5089  1.16.2.2  christos  * When playing back without MD filter:
   5090  1.16.2.2  christos  *
   5091  1.16.2.2  christos  *    mixsample [::::]                  wide-int 1 block (ring) buffer
   5092  1.16.2.2  christos  *                |
   5093  1.16.2.2  christos  *                |  convert aint2_t -> aint_t
   5094  1.16.2.2  christos  *                |  (with byte swap if necessary)
   5095  1.16.2.2  christos  *                v
   5096  1.16.2.2  christos  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5097  1.16.2.2  christos  *
   5098  1.16.2.2  christos  * mixsample: slinear_NE, wide internal precision, HW ch, HW freq.
   5099  1.16.2.2  christos  * codecbuf:  slinear_NE, internal precision,      HW ch, HW freq.
   5100  1.16.2.2  christos  * hwbuf:     HW encoding, HW precision,           HW ch, HW freq.
   5101  1.16.2.2  christos  */
   5102  1.16.2.2  christos 
   5103  1.16.2.2  christos /*
   5104  1.16.2.2  christos  * Performs track mixing and converts it to hwbuf.
   5105  1.16.2.2  christos  * Note that this function doesn't transfer hwbuf to hardware.
   5106  1.16.2.2  christos  * Must be called with sc_intr_lock held.
   5107  1.16.2.2  christos  */
   5108  1.16.2.2  christos static void
   5109  1.16.2.2  christos audio_pmixer_process(struct audio_softc *sc)
   5110  1.16.2.2  christos {
   5111  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5112  1.16.2.2  christos 	audio_file_t *f;
   5113  1.16.2.2  christos 	int frame_count;
   5114  1.16.2.2  christos 	int sample_count;
   5115  1.16.2.2  christos 	int mixed;
   5116  1.16.2.2  christos 	int i;
   5117  1.16.2.2  christos 	aint2_t *m;
   5118  1.16.2.2  christos 	aint_t *h;
   5119  1.16.2.2  christos 
   5120  1.16.2.2  christos 	mixer = sc->sc_pmixer;
   5121  1.16.2.2  christos 
   5122  1.16.2.2  christos 	frame_count = mixer->frames_per_block;
   5123  1.16.2.3    martin 	KASSERTMSG(auring_get_contig_free(&mixer->hwbuf) >= frame_count,
   5124  1.16.2.3    martin 	    "auring_get_contig_free()=%d frame_count=%d",
   5125  1.16.2.3    martin 	    auring_get_contig_free(&mixer->hwbuf), frame_count);
   5126  1.16.2.2  christos 	sample_count = frame_count * mixer->mixfmt.channels;
   5127  1.16.2.2  christos 
   5128  1.16.2.2  christos 	mixer->mixseq++;
   5129  1.16.2.2  christos 
   5130  1.16.2.2  christos 	/* Mix all tracks */
   5131  1.16.2.2  christos 	mixed = 0;
   5132  1.16.2.2  christos 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   5133  1.16.2.2  christos 		audio_track_t *track = f->ptrack;
   5134  1.16.2.2  christos 
   5135  1.16.2.2  christos 		if (track == NULL)
   5136  1.16.2.2  christos 			continue;
   5137  1.16.2.2  christos 
   5138  1.16.2.2  christos 		if (track->is_pause) {
   5139  1.16.2.2  christos 			TRACET(4, track, "skip; paused");
   5140  1.16.2.2  christos 			continue;
   5141  1.16.2.2  christos 		}
   5142  1.16.2.2  christos 
   5143  1.16.2.2  christos 		/* Skip if the track is used by process context. */
   5144  1.16.2.2  christos 		if (audio_track_lock_tryenter(track) == false) {
   5145  1.16.2.2  christos 			TRACET(4, track, "skip; in use");
   5146  1.16.2.2  christos 			continue;
   5147  1.16.2.2  christos 		}
   5148  1.16.2.2  christos 
   5149  1.16.2.2  christos 		/* Emulate mmap'ped track */
   5150  1.16.2.2  christos 		if (track->mmapped) {
   5151  1.16.2.2  christos 			auring_push(&track->usrbuf, track->usrbuf_blksize);
   5152  1.16.2.2  christos 			TRACET(4, track, "mmap; usr=%d/%d/C%d",
   5153  1.16.2.2  christos 			    track->usrbuf.head,
   5154  1.16.2.2  christos 			    track->usrbuf.used,
   5155  1.16.2.2  christos 			    track->usrbuf.capacity);
   5156  1.16.2.2  christos 		}
   5157  1.16.2.2  christos 
   5158  1.16.2.2  christos 		if (track->outbuf.used < mixer->frames_per_block &&
   5159  1.16.2.2  christos 		    track->usrbuf.used > 0) {
   5160  1.16.2.2  christos 			TRACET(4, track, "process");
   5161  1.16.2.2  christos 			audio_track_play(track);
   5162  1.16.2.2  christos 		}
   5163  1.16.2.2  christos 
   5164  1.16.2.2  christos 		if (track->outbuf.used > 0) {
   5165  1.16.2.2  christos 			mixed = audio_pmixer_mix_track(mixer, track, mixed);
   5166  1.16.2.2  christos 		} else {
   5167  1.16.2.2  christos 			TRACET(4, track, "skip; empty");
   5168  1.16.2.2  christos 		}
   5169  1.16.2.2  christos 
   5170  1.16.2.2  christos 		audio_track_lock_exit(track);
   5171  1.16.2.2  christos 	}
   5172  1.16.2.2  christos 
   5173  1.16.2.2  christos 	if (mixed == 0) {
   5174  1.16.2.2  christos 		/* Silence */
   5175  1.16.2.2  christos 		memset(mixer->mixsample, 0,
   5176  1.16.2.2  christos 		    frametobyte(&mixer->mixfmt, frame_count));
   5177  1.16.2.2  christos 	} else {
   5178  1.16.2.3    martin 		if (mixed > 1) {
   5179  1.16.2.3    martin 			/* If there are multiple tracks, do auto gain control */
   5180  1.16.2.3    martin 			audio_pmixer_agc(mixer, sample_count);
   5181  1.16.2.2  christos 		}
   5182  1.16.2.2  christos 
   5183  1.16.2.3    martin 		/* Apply master volume */
   5184  1.16.2.3    martin 		if (mixer->volume < 256) {
   5185  1.16.2.2  christos 			m = mixer->mixsample;
   5186  1.16.2.2  christos 			for (i = 0; i < sample_count; i++) {
   5187  1.16.2.3    martin 				*m = AUDIO_SCALEDOWN(*m * mixer->volume, 8);
   5188  1.16.2.2  christos 				m++;
   5189  1.16.2.2  christos 			}
   5190  1.16.2.3    martin 
   5191  1.16.2.3    martin 			/*
   5192  1.16.2.3    martin 			 * Recover the volume gradually at the pace of
   5193  1.16.2.3    martin 			 * several times per second.  If it's too fast, you
   5194  1.16.2.3    martin 			 * can recognize that the volume changes up and down
   5195  1.16.2.3    martin 			 * quickly and it's not so comfortable.
   5196  1.16.2.3    martin 			 */
   5197  1.16.2.3    martin 			mixer->voltimer += mixer->blktime_n;
   5198  1.16.2.3    martin 			if (mixer->voltimer * 4 >= mixer->blktime_d) {
   5199  1.16.2.3    martin 				mixer->volume++;
   5200  1.16.2.3    martin 				mixer->voltimer = 0;
   5201  1.16.2.3    martin #if defined(AUDIO_DEBUG_AGC)
   5202  1.16.2.3    martin 				TRACE(1, "volume recover: %d", mixer->volume);
   5203  1.16.2.3    martin #endif
   5204  1.16.2.3    martin 			}
   5205  1.16.2.2  christos 		}
   5206  1.16.2.2  christos 	}
   5207  1.16.2.2  christos 
   5208  1.16.2.2  christos 	/*
   5209  1.16.2.2  christos 	 * The rest is the hardware part.
   5210  1.16.2.2  christos 	 */
   5211  1.16.2.2  christos 
   5212  1.16.2.2  christos 	if (mixer->codec) {
   5213  1.16.2.2  christos 		h = auring_tailptr_aint(&mixer->codecbuf);
   5214  1.16.2.2  christos 	} else {
   5215  1.16.2.2  christos 		h = auring_tailptr_aint(&mixer->hwbuf);
   5216  1.16.2.2  christos 	}
   5217  1.16.2.2  christos 
   5218  1.16.2.2  christos 	m = mixer->mixsample;
   5219  1.16.2.2  christos 	if (mixer->swap_endian) {
   5220  1.16.2.2  christos 		for (i = 0; i < sample_count; i++) {
   5221  1.16.2.2  christos 			*h++ = bswap16(*m++);
   5222  1.16.2.2  christos 		}
   5223  1.16.2.2  christos 	} else {
   5224  1.16.2.2  christos 		for (i = 0; i < sample_count; i++) {
   5225  1.16.2.2  christos 			*h++ = *m++;
   5226  1.16.2.2  christos 		}
   5227  1.16.2.2  christos 	}
   5228  1.16.2.2  christos 
   5229  1.16.2.2  christos 	/* Hardware driver's codec */
   5230  1.16.2.2  christos 	if (mixer->codec) {
   5231  1.16.2.2  christos 		auring_push(&mixer->codecbuf, frame_count);
   5232  1.16.2.2  christos 		mixer->codecarg.src = auring_headptr(&mixer->codecbuf);
   5233  1.16.2.2  christos 		mixer->codecarg.dst = auring_tailptr(&mixer->hwbuf);
   5234  1.16.2.2  christos 		mixer->codecarg.count = frame_count;
   5235  1.16.2.2  christos 		mixer->codec(&mixer->codecarg);
   5236  1.16.2.2  christos 		auring_take(&mixer->codecbuf, mixer->codecarg.count);
   5237  1.16.2.2  christos 	}
   5238  1.16.2.2  christos 
   5239  1.16.2.2  christos 	auring_push(&mixer->hwbuf, frame_count);
   5240  1.16.2.2  christos 
   5241  1.16.2.2  christos 	TRACE(4, "done mixseq=%d hwbuf=%d/%d/%d%s",
   5242  1.16.2.2  christos 	    (int)mixer->mixseq,
   5243  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
   5244  1.16.2.2  christos 	    (mixed == 0) ? " silent" : "");
   5245  1.16.2.2  christos }
   5246  1.16.2.2  christos 
   5247  1.16.2.2  christos /*
   5248  1.16.2.3    martin  * Do auto gain control.
   5249  1.16.2.3    martin  * Must be called sc_intr_lock held.
   5250  1.16.2.3    martin  */
   5251  1.16.2.3    martin static void
   5252  1.16.2.3    martin audio_pmixer_agc(audio_trackmixer_t *mixer, int sample_count)
   5253  1.16.2.3    martin {
   5254  1.16.2.3    martin 	struct audio_softc *sc __unused;
   5255  1.16.2.3    martin 	aint2_t val;
   5256  1.16.2.3    martin 	aint2_t maxval;
   5257  1.16.2.3    martin 	aint2_t minval;
   5258  1.16.2.3    martin 	aint2_t over_plus;
   5259  1.16.2.3    martin 	aint2_t over_minus;
   5260  1.16.2.3    martin 	aint2_t *m;
   5261  1.16.2.3    martin 	int newvol;
   5262  1.16.2.3    martin 	int i;
   5263  1.16.2.3    martin 
   5264  1.16.2.3    martin 	sc = mixer->sc;
   5265  1.16.2.3    martin 
   5266  1.16.2.3    martin 	/* Overflow detection */
   5267  1.16.2.3    martin 	maxval = AINT_T_MAX;
   5268  1.16.2.3    martin 	minval = AINT_T_MIN;
   5269  1.16.2.3    martin 	m = mixer->mixsample;
   5270  1.16.2.3    martin 	for (i = 0; i < sample_count; i++) {
   5271  1.16.2.3    martin 		val = *m++;
   5272  1.16.2.3    martin 		if (val > maxval)
   5273  1.16.2.3    martin 			maxval = val;
   5274  1.16.2.3    martin 		else if (val < minval)
   5275  1.16.2.3    martin 			minval = val;
   5276  1.16.2.3    martin 	}
   5277  1.16.2.3    martin 
   5278  1.16.2.3    martin 	/* Absolute value of overflowed amount */
   5279  1.16.2.3    martin 	over_plus = maxval - AINT_T_MAX;
   5280  1.16.2.3    martin 	over_minus = AINT_T_MIN - minval;
   5281  1.16.2.3    martin 
   5282  1.16.2.3    martin 	if (over_plus > 0 || over_minus > 0) {
   5283  1.16.2.3    martin 		if (over_plus > over_minus) {
   5284  1.16.2.3    martin 			newvol = (int)((aint2_t)AINT_T_MAX * 256 / maxval);
   5285  1.16.2.3    martin 		} else {
   5286  1.16.2.3    martin 			newvol = (int)((aint2_t)AINT_T_MIN * 256 / minval);
   5287  1.16.2.3    martin 		}
   5288  1.16.2.3    martin 
   5289  1.16.2.3    martin 		/*
   5290  1.16.2.3    martin 		 * Change the volume only if new one is smaller.
   5291  1.16.2.3    martin 		 * Reset the timer even if the volume isn't changed.
   5292  1.16.2.3    martin 		 */
   5293  1.16.2.3    martin 		if (newvol <= mixer->volume) {
   5294  1.16.2.3    martin 			mixer->volume = newvol;
   5295  1.16.2.3    martin 			mixer->voltimer = 0;
   5296  1.16.2.3    martin #if defined(AUDIO_DEBUG_AGC)
   5297  1.16.2.3    martin 			TRACE(1, "auto volume adjust: %d", mixer->volume);
   5298  1.16.2.3    martin #endif
   5299  1.16.2.3    martin 		}
   5300  1.16.2.3    martin 	}
   5301  1.16.2.3    martin }
   5302  1.16.2.3    martin 
   5303  1.16.2.3    martin /*
   5304  1.16.2.2  christos  * Mix one track.
   5305  1.16.2.2  christos  * 'mixed' specifies the number of tracks mixed so far.
   5306  1.16.2.2  christos  * It returns the number of tracks mixed.  In other words, it returns
   5307  1.16.2.2  christos  * mixed + 1 if this track is mixed.
   5308  1.16.2.2  christos  */
   5309  1.16.2.2  christos static int
   5310  1.16.2.2  christos audio_pmixer_mix_track(audio_trackmixer_t *mixer, audio_track_t *track,
   5311  1.16.2.2  christos 	int mixed)
   5312  1.16.2.2  christos {
   5313  1.16.2.2  christos 	int count;
   5314  1.16.2.2  christos 	int sample_count;
   5315  1.16.2.2  christos 	int remain;
   5316  1.16.2.2  christos 	int i;
   5317  1.16.2.2  christos 	const aint_t *s;
   5318  1.16.2.2  christos 	aint2_t *d;
   5319  1.16.2.2  christos 
   5320  1.16.2.2  christos 	/* XXX TODO: Is this necessary for now? */
   5321  1.16.2.2  christos 	if (mixer->mixseq < track->seq)
   5322  1.16.2.2  christos 		return mixed;
   5323  1.16.2.2  christos 
   5324  1.16.2.2  christos 	count = auring_get_contig_used(&track->outbuf);
   5325  1.16.2.2  christos 	count = uimin(count, mixer->frames_per_block);
   5326  1.16.2.2  christos 
   5327  1.16.2.2  christos 	s = auring_headptr_aint(&track->outbuf);
   5328  1.16.2.2  christos 	d = mixer->mixsample;
   5329  1.16.2.2  christos 
   5330  1.16.2.2  christos 	/*
   5331  1.16.2.2  christos 	 * Apply track volume with double-sized integer and perform
   5332  1.16.2.2  christos 	 * additive synthesis.
   5333  1.16.2.2  christos 	 *
   5334  1.16.2.2  christos 	 * XXX If you limit the track volume to 1.0 or less (<= 256),
   5335  1.16.2.2  christos 	 *     it would be better to do this in the track conversion stage
   5336  1.16.2.2  christos 	 *     rather than here.  However, if you accept the volume to
   5337  1.16.2.2  christos 	 *     be greater than 1.0 (> 256), it's better to do it here.
   5338  1.16.2.2  christos 	 *     Because the operation here is done by double-sized integer.
   5339  1.16.2.2  christos 	 */
   5340  1.16.2.2  christos 	sample_count = count * mixer->mixfmt.channels;
   5341  1.16.2.2  christos 	if (mixed == 0) {
   5342  1.16.2.2  christos 		/* If this is the first track, assignment can be used. */
   5343  1.16.2.2  christos #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   5344  1.16.2.2  christos 		if (track->volume != 256) {
   5345  1.16.2.2  christos 			for (i = 0; i < sample_count; i++) {
   5346  1.16.2.3    martin 				aint2_t v;
   5347  1.16.2.3    martin 				v = *s++;
   5348  1.16.2.3    martin 				*d++ = AUDIO_SCALEDOWN(v * track->volume, 8)
   5349  1.16.2.2  christos 			}
   5350  1.16.2.2  christos 		} else
   5351  1.16.2.2  christos #endif
   5352  1.16.2.2  christos 		{
   5353  1.16.2.2  christos 			for (i = 0; i < sample_count; i++) {
   5354  1.16.2.2  christos 				*d++ = ((aint2_t)*s++);
   5355  1.16.2.2  christos 			}
   5356  1.16.2.2  christos 		}
   5357  1.16.2.3    martin 		/* Fill silence if the first track is not filled. */
   5358  1.16.2.3    martin 		for (; i < mixer->frames_per_block * mixer->mixfmt.channels; i++)
   5359  1.16.2.3    martin 			*d++ = 0;
   5360  1.16.2.2  christos 	} else {
   5361  1.16.2.2  christos 		/* If this is the second or later, add it. */
   5362  1.16.2.2  christos #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
   5363  1.16.2.2  christos 		if (track->volume != 256) {
   5364  1.16.2.2  christos 			for (i = 0; i < sample_count; i++) {
   5365  1.16.2.3    martin 				aint2_t v;
   5366  1.16.2.3    martin 				v = *s++;
   5367  1.16.2.3    martin 				*d++ += AUDIO_SCALEDOWN(v * track->volume, 8);
   5368  1.16.2.2  christos 			}
   5369  1.16.2.2  christos 		} else
   5370  1.16.2.2  christos #endif
   5371  1.16.2.2  christos 		{
   5372  1.16.2.2  christos 			for (i = 0; i < sample_count; i++) {
   5373  1.16.2.2  christos 				*d++ += ((aint2_t)*s++);
   5374  1.16.2.2  christos 			}
   5375  1.16.2.2  christos 		}
   5376  1.16.2.2  christos 	}
   5377  1.16.2.2  christos 
   5378  1.16.2.2  christos 	auring_take(&track->outbuf, count);
   5379  1.16.2.2  christos 	/*
   5380  1.16.2.2  christos 	 * The counters have to align block even if outbuf is less than
   5381  1.16.2.2  christos 	 * one block. XXX Is this still necessary?
   5382  1.16.2.2  christos 	 */
   5383  1.16.2.2  christos 	remain = mixer->frames_per_block - count;
   5384  1.16.2.2  christos 	if (__predict_false(remain != 0)) {
   5385  1.16.2.2  christos 		auring_push(&track->outbuf, remain);
   5386  1.16.2.2  christos 		auring_take(&track->outbuf, remain);
   5387  1.16.2.2  christos 	}
   5388  1.16.2.2  christos 
   5389  1.16.2.2  christos 	/*
   5390  1.16.2.2  christos 	 * Update track sequence.
   5391  1.16.2.2  christos 	 * mixseq has previous value yet at this point.
   5392  1.16.2.2  christos 	 */
   5393  1.16.2.2  christos 	track->seq = mixer->mixseq + 1;
   5394  1.16.2.2  christos 
   5395  1.16.2.2  christos 	return mixed + 1;
   5396  1.16.2.2  christos }
   5397  1.16.2.2  christos 
   5398  1.16.2.2  christos /*
   5399  1.16.2.2  christos  * Output one block from hwbuf to HW.
   5400  1.16.2.2  christos  * Must be called with sc_intr_lock held.
   5401  1.16.2.2  christos  */
   5402  1.16.2.2  christos static void
   5403  1.16.2.2  christos audio_pmixer_output(struct audio_softc *sc)
   5404  1.16.2.2  christos {
   5405  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5406  1.16.2.2  christos 	audio_params_t params;
   5407  1.16.2.2  christos 	void *start;
   5408  1.16.2.2  christos 	void *end;
   5409  1.16.2.2  christos 	int blksize;
   5410  1.16.2.2  christos 	int error;
   5411  1.16.2.2  christos 
   5412  1.16.2.2  christos 	mixer = sc->sc_pmixer;
   5413  1.16.2.2  christos 	TRACE(4, "pbusy=%d hwbuf=%d/%d/%d",
   5414  1.16.2.2  christos 	    sc->sc_pbusy,
   5415  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5416  1.16.2.3    martin 	KASSERTMSG(mixer->hwbuf.used >= mixer->frames_per_block,
   5417  1.16.2.3    martin 	    "mixer->hwbuf.used=%d mixer->frames_per_block=%d",
   5418  1.16.2.3    martin 	    mixer->hwbuf.used, mixer->frames_per_block);
   5419  1.16.2.2  christos 
   5420  1.16.2.2  christos 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   5421  1.16.2.2  christos 
   5422  1.16.2.2  christos 	if (sc->hw_if->trigger_output) {
   5423  1.16.2.2  christos 		/* trigger (at once) */
   5424  1.16.2.2  christos 		if (!sc->sc_pbusy) {
   5425  1.16.2.2  christos 			start = mixer->hwbuf.mem;
   5426  1.16.2.2  christos 			end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
   5427  1.16.2.2  christos 			params = format2_to_params(&mixer->hwbuf.fmt);
   5428  1.16.2.2  christos 
   5429  1.16.2.2  christos 			error = sc->hw_if->trigger_output(sc->hw_hdl,
   5430  1.16.2.2  christos 			    start, end, blksize, audio_pintr, sc, &params);
   5431  1.16.2.2  christos 			if (error) {
   5432  1.16.2.2  christos 				device_printf(sc->sc_dev,
   5433  1.16.2.3    martin 				    "trigger_output failed with %d\n", error);
   5434  1.16.2.2  christos 				return;
   5435  1.16.2.2  christos 			}
   5436  1.16.2.2  christos 		}
   5437  1.16.2.2  christos 	} else {
   5438  1.16.2.2  christos 		/* start (everytime) */
   5439  1.16.2.2  christos 		start = auring_headptr(&mixer->hwbuf);
   5440  1.16.2.2  christos 
   5441  1.16.2.2  christos 		error = sc->hw_if->start_output(sc->hw_hdl,
   5442  1.16.2.2  christos 		    start, blksize, audio_pintr, sc);
   5443  1.16.2.2  christos 		if (error) {
   5444  1.16.2.2  christos 			device_printf(sc->sc_dev,
   5445  1.16.2.3    martin 			    "start_output failed with %d\n", error);
   5446  1.16.2.2  christos 			return;
   5447  1.16.2.2  christos 		}
   5448  1.16.2.2  christos 	}
   5449  1.16.2.2  christos }
   5450  1.16.2.2  christos 
   5451  1.16.2.2  christos /*
   5452  1.16.2.2  christos  * This is an interrupt handler for playback.
   5453  1.16.2.2  christos  * It is called with sc_intr_lock held.
   5454  1.16.2.2  christos  *
   5455  1.16.2.2  christos  * It is usually called from hardware interrupt.  However, note that
   5456  1.16.2.2  christos  * for some drivers (e.g. uaudio) it is called from software interrupt.
   5457  1.16.2.2  christos  */
   5458  1.16.2.2  christos static void
   5459  1.16.2.2  christos audio_pintr(void *arg)
   5460  1.16.2.2  christos {
   5461  1.16.2.2  christos 	struct audio_softc *sc;
   5462  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5463  1.16.2.2  christos 
   5464  1.16.2.2  christos 	sc = arg;
   5465  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_intr_lock));
   5466  1.16.2.2  christos 
   5467  1.16.2.2  christos 	if (sc->sc_dying)
   5468  1.16.2.2  christos 		return;
   5469  1.16.2.2  christos 	if (sc->sc_pbusy == false) {
   5470  1.16.2.3    martin #if defined(DIAGNOSTIC)
   5471  1.16.2.2  christos 		device_printf(sc->sc_dev, "stray interrupt\n");
   5472  1.16.2.3    martin #endif
   5473  1.16.2.2  christos 		return;
   5474  1.16.2.2  christos 	}
   5475  1.16.2.2  christos 
   5476  1.16.2.2  christos 	mixer = sc->sc_pmixer;
   5477  1.16.2.2  christos 	mixer->hw_complete_counter += mixer->frames_per_block;
   5478  1.16.2.2  christos 	mixer->hwseq++;
   5479  1.16.2.2  christos 
   5480  1.16.2.2  christos 	auring_take(&mixer->hwbuf, mixer->frames_per_block);
   5481  1.16.2.2  christos 
   5482  1.16.2.2  christos 	TRACE(4,
   5483  1.16.2.2  christos 	    "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
   5484  1.16.2.2  christos 	    mixer->hwseq, mixer->hw_complete_counter,
   5485  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5486  1.16.2.2  christos 
   5487  1.16.2.2  christos #if defined(AUDIO_HW_SINGLE_BUFFER)
   5488  1.16.2.2  christos 	/*
   5489  1.16.2.2  christos 	 * Create a new block here and output it immediately.
   5490  1.16.2.2  christos 	 * It makes a latency lower but needs machine power.
   5491  1.16.2.2  christos 	 */
   5492  1.16.2.2  christos 	audio_pmixer_process(sc);
   5493  1.16.2.2  christos 	audio_pmixer_output(sc);
   5494  1.16.2.2  christos #else
   5495  1.16.2.2  christos 	/*
   5496  1.16.2.2  christos 	 * It is called when block N output is done.
   5497  1.16.2.2  christos 	 * Output immediately block N+1 created by the last interrupt.
   5498  1.16.2.2  christos 	 * And then create block N+2 for the next interrupt.
   5499  1.16.2.2  christos 	 * This method makes playback robust even on slower machines.
   5500  1.16.2.2  christos 	 * Instead the latency is increased by one block.
   5501  1.16.2.2  christos 	 */
   5502  1.16.2.2  christos 
   5503  1.16.2.2  christos 	/* At first, output ready block. */
   5504  1.16.2.2  christos 	if (mixer->hwbuf.used >= mixer->frames_per_block) {
   5505  1.16.2.2  christos 		audio_pmixer_output(sc);
   5506  1.16.2.2  christos 	}
   5507  1.16.2.2  christos 
   5508  1.16.2.2  christos 	bool later = false;
   5509  1.16.2.2  christos 
   5510  1.16.2.2  christos 	if (mixer->hwbuf.used < mixer->frames_per_block) {
   5511  1.16.2.2  christos 		later = true;
   5512  1.16.2.2  christos 	}
   5513  1.16.2.2  christos 
   5514  1.16.2.2  christos 	/* Then, process next block. */
   5515  1.16.2.2  christos 	audio_pmixer_process(sc);
   5516  1.16.2.2  christos 
   5517  1.16.2.2  christos 	if (later) {
   5518  1.16.2.2  christos 		audio_pmixer_output(sc);
   5519  1.16.2.2  christos 	}
   5520  1.16.2.2  christos #endif
   5521  1.16.2.2  christos 
   5522  1.16.2.2  christos 	/*
   5523  1.16.2.2  christos 	 * When this interrupt is the real hardware interrupt, disabling
   5524  1.16.2.2  christos 	 * preemption here is not necessary.  But some drivers (e.g. uaudio)
   5525  1.16.2.2  christos 	 * emulate it by software interrupt, so kpreempt_disable is necessary.
   5526  1.16.2.2  christos 	 */
   5527  1.16.2.2  christos 	kpreempt_disable();
   5528  1.16.2.2  christos 	softint_schedule(mixer->sih);
   5529  1.16.2.2  christos 	kpreempt_enable();
   5530  1.16.2.2  christos }
   5531  1.16.2.2  christos 
   5532  1.16.2.2  christos /*
   5533  1.16.2.2  christos  * Starts record mixer.
   5534  1.16.2.2  christos  * Must be called only if sc_rbusy is false.
   5535  1.16.2.3    martin  * Must be called with sc_lock && sc_exlock held.
   5536  1.16.2.2  christos  * Must not be called from the interrupt context.
   5537  1.16.2.2  christos  */
   5538  1.16.2.2  christos static void
   5539  1.16.2.2  christos audio_rmixer_start(struct audio_softc *sc)
   5540  1.16.2.2  christos {
   5541  1.16.2.2  christos 
   5542  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   5543  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   5544  1.16.2.2  christos 	KASSERT(sc->sc_rbusy == false);
   5545  1.16.2.2  christos 
   5546  1.16.2.2  christos 	mutex_enter(sc->sc_intr_lock);
   5547  1.16.2.2  christos 
   5548  1.16.2.2  christos 	TRACE(2, "%s", (audiodebug >= 3) ? "begin" : "");
   5549  1.16.2.2  christos 	audio_rmixer_input(sc);
   5550  1.16.2.2  christos 	sc->sc_rbusy = true;
   5551  1.16.2.2  christos 	TRACE(3, "end");
   5552  1.16.2.2  christos 
   5553  1.16.2.2  christos 	mutex_exit(sc->sc_intr_lock);
   5554  1.16.2.2  christos }
   5555  1.16.2.2  christos 
   5556  1.16.2.2  christos /*
   5557  1.16.2.2  christos  * When recording with MD filter:
   5558  1.16.2.2  christos  *
   5559  1.16.2.2  christos  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5560  1.16.2.2  christos  *                |
   5561  1.16.2.2  christos  *                | convert from hw format
   5562  1.16.2.2  christos  *                v
   5563  1.16.2.2  christos  *    codecbuf  [....]                  1 block (ring) buffer
   5564  1.16.2.2  christos  *               |  |
   5565  1.16.2.2  christos  *               v  v
   5566  1.16.2.2  christos  *            track track ...
   5567  1.16.2.2  christos  *
   5568  1.16.2.2  christos  * When recording without MD filter:
   5569  1.16.2.2  christos  *
   5570  1.16.2.2  christos  *    hwbuf     [............]          NBLKHW blocks ring buffer
   5571  1.16.2.2  christos  *               |  |
   5572  1.16.2.2  christos  *               v  v
   5573  1.16.2.2  christos  *            track track ...
   5574  1.16.2.2  christos  *
   5575  1.16.2.2  christos  * hwbuf:     HW encoding, HW precision, HW ch, HW freq.
   5576  1.16.2.2  christos  * codecbuf:  slinear_NE, internal precision, HW ch, HW freq.
   5577  1.16.2.2  christos  */
   5578  1.16.2.2  christos 
   5579  1.16.2.2  christos /*
   5580  1.16.2.2  christos  * Distribute a recorded block to all recording tracks.
   5581  1.16.2.2  christos  */
   5582  1.16.2.2  christos static void
   5583  1.16.2.2  christos audio_rmixer_process(struct audio_softc *sc)
   5584  1.16.2.2  christos {
   5585  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5586  1.16.2.2  christos 	audio_ring_t *mixersrc;
   5587  1.16.2.2  christos 	audio_file_t *f;
   5588  1.16.2.2  christos 	aint_t *p;
   5589  1.16.2.2  christos 	int count;
   5590  1.16.2.2  christos 	int bytes;
   5591  1.16.2.2  christos 	int i;
   5592  1.16.2.2  christos 
   5593  1.16.2.2  christos 	mixer = sc->sc_rmixer;
   5594  1.16.2.2  christos 
   5595  1.16.2.2  christos 	/*
   5596  1.16.2.2  christos 	 * count is the number of frames to be retrieved this time.
   5597  1.16.2.2  christos 	 * count should be one block.
   5598  1.16.2.2  christos 	 */
   5599  1.16.2.2  christos 	count = auring_get_contig_used(&mixer->hwbuf);
   5600  1.16.2.2  christos 	count = uimin(count, mixer->frames_per_block);
   5601  1.16.2.2  christos 	if (count <= 0) {
   5602  1.16.2.2  christos 		TRACE(4, "count %d: too short", count);
   5603  1.16.2.2  christos 		return;
   5604  1.16.2.2  christos 	}
   5605  1.16.2.2  christos 	bytes = frametobyte(&mixer->track_fmt, count);
   5606  1.16.2.2  christos 
   5607  1.16.2.2  christos 	/* Hardware driver's codec */
   5608  1.16.2.2  christos 	if (mixer->codec) {
   5609  1.16.2.2  christos 		mixer->codecarg.src = auring_headptr(&mixer->hwbuf);
   5610  1.16.2.2  christos 		mixer->codecarg.dst = auring_tailptr(&mixer->codecbuf);
   5611  1.16.2.2  christos 		mixer->codecarg.count = count;
   5612  1.16.2.2  christos 		mixer->codec(&mixer->codecarg);
   5613  1.16.2.2  christos 		auring_take(&mixer->hwbuf, mixer->codecarg.count);
   5614  1.16.2.2  christos 		auring_push(&mixer->codecbuf, mixer->codecarg.count);
   5615  1.16.2.2  christos 		mixersrc = &mixer->codecbuf;
   5616  1.16.2.2  christos 	} else {
   5617  1.16.2.2  christos 		mixersrc = &mixer->hwbuf;
   5618  1.16.2.2  christos 	}
   5619  1.16.2.2  christos 
   5620  1.16.2.2  christos 	if (mixer->swap_endian) {
   5621  1.16.2.2  christos 		/* inplace conversion */
   5622  1.16.2.2  christos 		p = auring_headptr_aint(mixersrc);
   5623  1.16.2.2  christos 		for (i = 0; i < count * mixer->track_fmt.channels; i++, p++) {
   5624  1.16.2.2  christos 			*p = bswap16(*p);
   5625  1.16.2.2  christos 		}
   5626  1.16.2.2  christos 	}
   5627  1.16.2.2  christos 
   5628  1.16.2.2  christos 	/* Distribute to all tracks. */
   5629  1.16.2.2  christos 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   5630  1.16.2.2  christos 		audio_track_t *track = f->rtrack;
   5631  1.16.2.2  christos 		audio_ring_t *input;
   5632  1.16.2.2  christos 
   5633  1.16.2.2  christos 		if (track == NULL)
   5634  1.16.2.2  christos 			continue;
   5635  1.16.2.2  christos 
   5636  1.16.2.2  christos 		if (track->is_pause) {
   5637  1.16.2.2  christos 			TRACET(4, track, "skip; paused");
   5638  1.16.2.2  christos 			continue;
   5639  1.16.2.2  christos 		}
   5640  1.16.2.2  christos 
   5641  1.16.2.2  christos 		if (audio_track_lock_tryenter(track) == false) {
   5642  1.16.2.2  christos 			TRACET(4, track, "skip; in use");
   5643  1.16.2.2  christos 			continue;
   5644  1.16.2.2  christos 		}
   5645  1.16.2.2  christos 
   5646  1.16.2.2  christos 		/* If the track buffer is full, discard the oldest one? */
   5647  1.16.2.2  christos 		input = track->input;
   5648  1.16.2.2  christos 		if (input->capacity - input->used < mixer->frames_per_block) {
   5649  1.16.2.2  christos 			int drops = mixer->frames_per_block -
   5650  1.16.2.2  christos 			    (input->capacity - input->used);
   5651  1.16.2.2  christos 			track->dropframes += drops;
   5652  1.16.2.2  christos 			TRACET(4, track, "drop %d frames: inp=%d/%d/%d",
   5653  1.16.2.2  christos 			    drops,
   5654  1.16.2.2  christos 			    input->head, input->used, input->capacity);
   5655  1.16.2.2  christos 			auring_take(input, drops);
   5656  1.16.2.2  christos 		}
   5657  1.16.2.3    martin 		KASSERTMSG(input->used % mixer->frames_per_block == 0,
   5658  1.16.2.3    martin 		    "input->used=%d mixer->frames_per_block=%d",
   5659  1.16.2.3    martin 		    input->used, mixer->frames_per_block);
   5660  1.16.2.2  christos 
   5661  1.16.2.2  christos 		memcpy(auring_tailptr_aint(input),
   5662  1.16.2.2  christos 		    auring_headptr_aint(mixersrc),
   5663  1.16.2.2  christos 		    bytes);
   5664  1.16.2.2  christos 		auring_push(input, count);
   5665  1.16.2.2  christos 
   5666  1.16.2.2  christos 		/* XXX sequence counter? */
   5667  1.16.2.2  christos 
   5668  1.16.2.2  christos 		audio_track_lock_exit(track);
   5669  1.16.2.2  christos 	}
   5670  1.16.2.2  christos 
   5671  1.16.2.2  christos 	auring_take(mixersrc, count);
   5672  1.16.2.2  christos }
   5673  1.16.2.2  christos 
   5674  1.16.2.2  christos /*
   5675  1.16.2.2  christos  * Input one block from HW to hwbuf.
   5676  1.16.2.2  christos  * Must be called with sc_intr_lock held.
   5677  1.16.2.2  christos  */
   5678  1.16.2.2  christos static void
   5679  1.16.2.2  christos audio_rmixer_input(struct audio_softc *sc)
   5680  1.16.2.2  christos {
   5681  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5682  1.16.2.2  christos 	audio_params_t params;
   5683  1.16.2.2  christos 	void *start;
   5684  1.16.2.2  christos 	void *end;
   5685  1.16.2.2  christos 	int blksize;
   5686  1.16.2.2  christos 	int error;
   5687  1.16.2.2  christos 
   5688  1.16.2.2  christos 	mixer = sc->sc_rmixer;
   5689  1.16.2.2  christos 	blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
   5690  1.16.2.2  christos 
   5691  1.16.2.2  christos 	if (sc->hw_if->trigger_input) {
   5692  1.16.2.2  christos 		/* trigger (at once) */
   5693  1.16.2.2  christos 		if (!sc->sc_rbusy) {
   5694  1.16.2.2  christos 			start = mixer->hwbuf.mem;
   5695  1.16.2.2  christos 			end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
   5696  1.16.2.2  christos 			params = format2_to_params(&mixer->hwbuf.fmt);
   5697  1.16.2.2  christos 
   5698  1.16.2.2  christos 			error = sc->hw_if->trigger_input(sc->hw_hdl,
   5699  1.16.2.2  christos 			    start, end, blksize, audio_rintr, sc, &params);
   5700  1.16.2.2  christos 			if (error) {
   5701  1.16.2.2  christos 				device_printf(sc->sc_dev,
   5702  1.16.2.3    martin 				    "trigger_input failed with %d\n", error);
   5703  1.16.2.2  christos 				return;
   5704  1.16.2.2  christos 			}
   5705  1.16.2.2  christos 		}
   5706  1.16.2.2  christos 	} else {
   5707  1.16.2.2  christos 		/* start (everytime) */
   5708  1.16.2.2  christos 		start = auring_tailptr(&mixer->hwbuf);
   5709  1.16.2.2  christos 
   5710  1.16.2.2  christos 		error = sc->hw_if->start_input(sc->hw_hdl,
   5711  1.16.2.2  christos 		    start, blksize, audio_rintr, sc);
   5712  1.16.2.2  christos 		if (error) {
   5713  1.16.2.2  christos 			device_printf(sc->sc_dev,
   5714  1.16.2.3    martin 			    "start_input failed with %d\n", error);
   5715  1.16.2.2  christos 			return;
   5716  1.16.2.2  christos 		}
   5717  1.16.2.2  christos 	}
   5718  1.16.2.2  christos }
   5719  1.16.2.2  christos 
   5720  1.16.2.2  christos /*
   5721  1.16.2.2  christos  * This is an interrupt handler for recording.
   5722  1.16.2.2  christos  * It is called with sc_intr_lock.
   5723  1.16.2.2  christos  *
   5724  1.16.2.2  christos  * It is usually called from hardware interrupt.  However, note that
   5725  1.16.2.2  christos  * for some drivers (e.g. uaudio) it is called from software interrupt.
   5726  1.16.2.2  christos  */
   5727  1.16.2.2  christos static void
   5728  1.16.2.2  christos audio_rintr(void *arg)
   5729  1.16.2.2  christos {
   5730  1.16.2.2  christos 	struct audio_softc *sc;
   5731  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5732  1.16.2.2  christos 
   5733  1.16.2.2  christos 	sc = arg;
   5734  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_intr_lock));
   5735  1.16.2.2  christos 
   5736  1.16.2.2  christos 	if (sc->sc_dying)
   5737  1.16.2.2  christos 		return;
   5738  1.16.2.2  christos 	if (sc->sc_rbusy == false) {
   5739  1.16.2.3    martin #if defined(DIAGNOSTIC)
   5740  1.16.2.2  christos 		device_printf(sc->sc_dev, "stray interrupt\n");
   5741  1.16.2.3    martin #endif
   5742  1.16.2.2  christos 		return;
   5743  1.16.2.2  christos 	}
   5744  1.16.2.2  christos 
   5745  1.16.2.2  christos 	mixer = sc->sc_rmixer;
   5746  1.16.2.2  christos 	mixer->hw_complete_counter += mixer->frames_per_block;
   5747  1.16.2.2  christos 	mixer->hwseq++;
   5748  1.16.2.2  christos 
   5749  1.16.2.2  christos 	auring_push(&mixer->hwbuf, mixer->frames_per_block);
   5750  1.16.2.2  christos 
   5751  1.16.2.2  christos 	TRACE(4,
   5752  1.16.2.2  christos 	    "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
   5753  1.16.2.2  christos 	    mixer->hwseq, mixer->hw_complete_counter,
   5754  1.16.2.2  christos 	    mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
   5755  1.16.2.2  christos 
   5756  1.16.2.2  christos 	/* Distrubute recorded block */
   5757  1.16.2.2  christos 	audio_rmixer_process(sc);
   5758  1.16.2.2  christos 
   5759  1.16.2.2  christos 	/* Request next block */
   5760  1.16.2.2  christos 	audio_rmixer_input(sc);
   5761  1.16.2.2  christos 
   5762  1.16.2.2  christos 	/*
   5763  1.16.2.2  christos 	 * When this interrupt is the real hardware interrupt, disabling
   5764  1.16.2.2  christos 	 * preemption here is not necessary.  But some drivers (e.g. uaudio)
   5765  1.16.2.2  christos 	 * emulate it by software interrupt, so kpreempt_disable is necessary.
   5766  1.16.2.2  christos 	 */
   5767  1.16.2.2  christos 	kpreempt_disable();
   5768  1.16.2.2  christos 	softint_schedule(mixer->sih);
   5769  1.16.2.2  christos 	kpreempt_enable();
   5770  1.16.2.2  christos }
   5771  1.16.2.2  christos 
   5772  1.16.2.2  christos /*
   5773  1.16.2.2  christos  * Halts playback mixer.
   5774  1.16.2.2  christos  * This function also clears related parameters, so call this function
   5775  1.16.2.2  christos  * instead of calling halt_output directly.
   5776  1.16.2.2  christos  * Must be called only if sc_pbusy is true.
   5777  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   5778  1.16.2.2  christos  */
   5779  1.16.2.2  christos static int
   5780  1.16.2.2  christos audio_pmixer_halt(struct audio_softc *sc)
   5781  1.16.2.2  christos {
   5782  1.16.2.2  christos 	int error;
   5783  1.16.2.2  christos 
   5784  1.16.2.2  christos 	TRACE(2, "");
   5785  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   5786  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   5787  1.16.2.2  christos 
   5788  1.16.2.2  christos 	mutex_enter(sc->sc_intr_lock);
   5789  1.16.2.2  christos 	error = sc->hw_if->halt_output(sc->hw_hdl);
   5790  1.16.2.2  christos 
   5791  1.16.2.2  christos 	/* Halts anyway even if some error has occurred. */
   5792  1.16.2.2  christos 	sc->sc_pbusy = false;
   5793  1.16.2.2  christos 	sc->sc_pmixer->hwbuf.head = 0;
   5794  1.16.2.2  christos 	sc->sc_pmixer->hwbuf.used = 0;
   5795  1.16.2.2  christos 	sc->sc_pmixer->mixseq = 0;
   5796  1.16.2.2  christos 	sc->sc_pmixer->hwseq = 0;
   5797  1.16.2.3    martin 	mutex_exit(sc->sc_intr_lock);
   5798  1.16.2.2  christos 
   5799  1.16.2.2  christos 	return error;
   5800  1.16.2.2  christos }
   5801  1.16.2.2  christos 
   5802  1.16.2.2  christos /*
   5803  1.16.2.2  christos  * Halts recording mixer.
   5804  1.16.2.2  christos  * This function also clears related parameters, so call this function
   5805  1.16.2.2  christos  * instead of calling halt_input directly.
   5806  1.16.2.2  christos  * Must be called only if sc_rbusy is true.
   5807  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   5808  1.16.2.2  christos  */
   5809  1.16.2.2  christos static int
   5810  1.16.2.2  christos audio_rmixer_halt(struct audio_softc *sc)
   5811  1.16.2.2  christos {
   5812  1.16.2.2  christos 	int error;
   5813  1.16.2.2  christos 
   5814  1.16.2.2  christos 	TRACE(2, "");
   5815  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   5816  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   5817  1.16.2.2  christos 
   5818  1.16.2.2  christos 	mutex_enter(sc->sc_intr_lock);
   5819  1.16.2.2  christos 	error = sc->hw_if->halt_input(sc->hw_hdl);
   5820  1.16.2.2  christos 
   5821  1.16.2.2  christos 	/* Halts anyway even if some error has occurred. */
   5822  1.16.2.2  christos 	sc->sc_rbusy = false;
   5823  1.16.2.2  christos 	sc->sc_rmixer->hwbuf.head = 0;
   5824  1.16.2.2  christos 	sc->sc_rmixer->hwbuf.used = 0;
   5825  1.16.2.2  christos 	sc->sc_rmixer->mixseq = 0;
   5826  1.16.2.2  christos 	sc->sc_rmixer->hwseq = 0;
   5827  1.16.2.3    martin 	mutex_exit(sc->sc_intr_lock);
   5828  1.16.2.2  christos 
   5829  1.16.2.2  christos 	return error;
   5830  1.16.2.2  christos }
   5831  1.16.2.2  christos 
   5832  1.16.2.2  christos /*
   5833  1.16.2.2  christos  * Flush this track.
   5834  1.16.2.2  christos  * Halts all operations, clears all buffers, reset error counters.
   5835  1.16.2.2  christos  * XXX I'm not sure...
   5836  1.16.2.2  christos  */
   5837  1.16.2.2  christos static void
   5838  1.16.2.2  christos audio_track_clear(struct audio_softc *sc, audio_track_t *track)
   5839  1.16.2.2  christos {
   5840  1.16.2.2  christos 
   5841  1.16.2.2  christos 	KASSERT(track);
   5842  1.16.2.2  christos 	TRACET(3, track, "clear");
   5843  1.16.2.2  christos 
   5844  1.16.2.2  christos 	audio_track_lock_enter(track);
   5845  1.16.2.2  christos 
   5846  1.16.2.2  christos 	track->usrbuf.used = 0;
   5847  1.16.2.2  christos 	/* Clear all internal parameters. */
   5848  1.16.2.2  christos 	if (track->codec.filter) {
   5849  1.16.2.2  christos 		track->codec.srcbuf.used = 0;
   5850  1.16.2.2  christos 		track->codec.srcbuf.head = 0;
   5851  1.16.2.2  christos 	}
   5852  1.16.2.2  christos 	if (track->chvol.filter) {
   5853  1.16.2.2  christos 		track->chvol.srcbuf.used = 0;
   5854  1.16.2.2  christos 		track->chvol.srcbuf.head = 0;
   5855  1.16.2.2  christos 	}
   5856  1.16.2.2  christos 	if (track->chmix.filter) {
   5857  1.16.2.2  christos 		track->chmix.srcbuf.used = 0;
   5858  1.16.2.2  christos 		track->chmix.srcbuf.head = 0;
   5859  1.16.2.2  christos 	}
   5860  1.16.2.2  christos 	if (track->freq.filter) {
   5861  1.16.2.2  christos 		track->freq.srcbuf.used = 0;
   5862  1.16.2.2  christos 		track->freq.srcbuf.head = 0;
   5863  1.16.2.2  christos 		if (track->freq_step < 65536)
   5864  1.16.2.2  christos 			track->freq_current = 65536;
   5865  1.16.2.2  christos 		else
   5866  1.16.2.2  christos 			track->freq_current = 0;
   5867  1.16.2.2  christos 		memset(track->freq_prev, 0, sizeof(track->freq_prev));
   5868  1.16.2.2  christos 		memset(track->freq_curr, 0, sizeof(track->freq_curr));
   5869  1.16.2.2  christos 	}
   5870  1.16.2.2  christos 	/* Clear buffer, then operation halts naturally. */
   5871  1.16.2.2  christos 	track->outbuf.used = 0;
   5872  1.16.2.2  christos 
   5873  1.16.2.2  christos 	/* Clear counters. */
   5874  1.16.2.2  christos 	track->dropframes = 0;
   5875  1.16.2.2  christos 
   5876  1.16.2.2  christos 	audio_track_lock_exit(track);
   5877  1.16.2.2  christos }
   5878  1.16.2.2  christos 
   5879  1.16.2.2  christos /*
   5880  1.16.2.2  christos  * Drain the track.
   5881  1.16.2.2  christos  * track must be present and for playback.
   5882  1.16.2.2  christos  * If successful, it returns 0.  Otherwise returns errno.
   5883  1.16.2.2  christos  * Must be called with sc_lock held.
   5884  1.16.2.2  christos  */
   5885  1.16.2.2  christos static int
   5886  1.16.2.2  christos audio_track_drain(struct audio_softc *sc, audio_track_t *track)
   5887  1.16.2.2  christos {
   5888  1.16.2.2  christos 	audio_trackmixer_t *mixer;
   5889  1.16.2.2  christos 	int done;
   5890  1.16.2.2  christos 	int error;
   5891  1.16.2.2  christos 
   5892  1.16.2.2  christos 	KASSERT(track);
   5893  1.16.2.2  christos 	TRACET(3, track, "start");
   5894  1.16.2.2  christos 	mixer = track->mixer;
   5895  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   5896  1.16.2.2  christos 
   5897  1.16.2.2  christos 	/* Ignore them if pause. */
   5898  1.16.2.2  christos 	if (track->is_pause) {
   5899  1.16.2.2  christos 		TRACET(3, track, "pause -> clear");
   5900  1.16.2.2  christos 		track->pstate = AUDIO_STATE_CLEAR;
   5901  1.16.2.2  christos 	}
   5902  1.16.2.2  christos 	/* Terminate early here if there is no data in the track. */
   5903  1.16.2.2  christos 	if (track->pstate == AUDIO_STATE_CLEAR) {
   5904  1.16.2.2  christos 		TRACET(3, track, "no need to drain");
   5905  1.16.2.2  christos 		return 0;
   5906  1.16.2.2  christos 	}
   5907  1.16.2.2  christos 	track->pstate = AUDIO_STATE_DRAINING;
   5908  1.16.2.2  christos 
   5909  1.16.2.2  christos 	for (;;) {
   5910  1.16.2.2  christos 		/* I want to display it before condition evaluation. */
   5911  1.16.2.2  christos 		TRACET(3, track, "pid=%d.%d trkseq=%d hwseq=%d out=%d/%d/%d",
   5912  1.16.2.2  christos 		    (int)curproc->p_pid, (int)curlwp->l_lid,
   5913  1.16.2.2  christos 		    (int)track->seq, (int)mixer->hwseq,
   5914  1.16.2.2  christos 		    track->outbuf.head, track->outbuf.used,
   5915  1.16.2.2  christos 		    track->outbuf.capacity);
   5916  1.16.2.2  christos 
   5917  1.16.2.2  christos 		/* Condition to terminate */
   5918  1.16.2.2  christos 		audio_track_lock_enter(track);
   5919  1.16.2.2  christos 		done = (track->usrbuf.used < frametobyte(&track->inputfmt, 1) &&
   5920  1.16.2.2  christos 		    track->outbuf.used == 0 &&
   5921  1.16.2.2  christos 		    track->seq <= mixer->hwseq);
   5922  1.16.2.2  christos 		audio_track_lock_exit(track);
   5923  1.16.2.2  christos 		if (done)
   5924  1.16.2.2  christos 			break;
   5925  1.16.2.2  christos 
   5926  1.16.2.2  christos 		TRACET(3, track, "sleep");
   5927  1.16.2.2  christos 		error = audio_track_waitio(sc, track);
   5928  1.16.2.2  christos 		if (error)
   5929  1.16.2.2  christos 			return error;
   5930  1.16.2.2  christos 
   5931  1.16.2.3    martin 		/* XXX call audio_track_play here ? */
   5932  1.16.2.3    martin 	}
   5933  1.16.2.3    martin 
   5934  1.16.2.3    martin 	track->pstate = AUDIO_STATE_CLEAR;
   5935  1.16.2.3    martin 	TRACET(3, track, "done trk_inp=%d trk_out=%d",
   5936  1.16.2.3    martin 		(int)track->inputcounter, (int)track->outputcounter);
   5937  1.16.2.3    martin 	return 0;
   5938  1.16.2.3    martin }
   5939  1.16.2.3    martin 
   5940  1.16.2.3    martin /*
   5941  1.16.2.3    martin  * Send signal to process.
   5942  1.16.2.3    martin  * This is intended to be called only from audio_softintr_{rd,wr}.
   5943  1.16.2.3    martin  * Must be called without sc_intr_lock held.
   5944  1.16.2.3    martin  */
   5945  1.16.2.3    martin static inline void
   5946  1.16.2.3    martin audio_psignal(struct audio_softc *sc, pid_t pid, int signum)
   5947  1.16.2.3    martin {
   5948  1.16.2.3    martin 	proc_t *p;
   5949  1.16.2.3    martin 
   5950  1.16.2.3    martin 	KASSERT(pid != 0);
   5951  1.16.2.2  christos 
   5952  1.16.2.3    martin 	/*
   5953  1.16.2.3    martin 	 * psignal() must be called without spin lock held.
   5954  1.16.2.3    martin 	 */
   5955  1.16.2.3    martin 
   5956  1.16.2.3    martin 	mutex_enter(proc_lock);
   5957  1.16.2.3    martin 	p = proc_find(pid);
   5958  1.16.2.3    martin 	if (p)
   5959  1.16.2.3    martin 		psignal(p, signum);
   5960  1.16.2.3    martin 	mutex_exit(proc_lock);
   5961  1.16.2.2  christos }
   5962  1.16.2.2  christos 
   5963  1.16.2.2  christos /*
   5964  1.16.2.2  christos  * This is software interrupt handler for record.
   5965  1.16.2.2  christos  * It is called from recording hardware interrupt everytime.
   5966  1.16.2.2  christos  * It does:
   5967  1.16.2.2  christos  * - Deliver SIGIO for all async processes.
   5968  1.16.2.2  christos  * - Notify to audio_read() that data has arrived.
   5969  1.16.2.2  christos  * - selnotify() for select/poll-ing processes.
   5970  1.16.2.2  christos  */
   5971  1.16.2.2  christos /*
   5972  1.16.2.2  christos  * XXX If a process issues FIOASYNC between hardware interrupt and
   5973  1.16.2.2  christos  *     software interrupt, (stray) SIGIO will be sent to the process
   5974  1.16.2.2  christos  *     despite the fact that it has not receive recorded data yet.
   5975  1.16.2.2  christos  */
   5976  1.16.2.2  christos static void
   5977  1.16.2.2  christos audio_softintr_rd(void *cookie)
   5978  1.16.2.2  christos {
   5979  1.16.2.2  christos 	struct audio_softc *sc = cookie;
   5980  1.16.2.2  christos 	audio_file_t *f;
   5981  1.16.2.2  christos 	pid_t pid;
   5982  1.16.2.2  christos 
   5983  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   5984  1.16.2.2  christos 
   5985  1.16.2.2  christos 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   5986  1.16.2.2  christos 		audio_track_t *track = f->rtrack;
   5987  1.16.2.2  christos 
   5988  1.16.2.2  christos 		if (track == NULL)
   5989  1.16.2.2  christos 			continue;
   5990  1.16.2.2  christos 
   5991  1.16.2.2  christos 		TRACET(4, track, "broadcast; inp=%d/%d/%d",
   5992  1.16.2.2  christos 		    track->input->head,
   5993  1.16.2.2  christos 		    track->input->used,
   5994  1.16.2.2  christos 		    track->input->capacity);
   5995  1.16.2.2  christos 
   5996  1.16.2.2  christos 		pid = f->async_audio;
   5997  1.16.2.2  christos 		if (pid != 0) {
   5998  1.16.2.2  christos 			TRACEF(4, f, "sending SIGIO %d", pid);
   5999  1.16.2.3    martin 			audio_psignal(sc, pid, SIGIO);
   6000  1.16.2.2  christos 		}
   6001  1.16.2.2  christos 	}
   6002  1.16.2.2  christos 
   6003  1.16.2.2  christos 	/* Notify that data has arrived. */
   6004  1.16.2.2  christos 	selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
   6005  1.16.2.2  christos 	KNOTE(&sc->sc_rsel.sel_klist, 0);
   6006  1.16.2.2  christos 	cv_broadcast(&sc->sc_rmixer->outcv);
   6007  1.16.2.2  christos 
   6008  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   6009  1.16.2.2  christos }
   6010  1.16.2.2  christos 
   6011  1.16.2.2  christos /*
   6012  1.16.2.2  christos  * This is software interrupt handler for playback.
   6013  1.16.2.2  christos  * It is called from playback hardware interrupt everytime.
   6014  1.16.2.2  christos  * It does:
   6015  1.16.2.2  christos  * - Deliver SIGIO for all async and writable (used < lowat) processes.
   6016  1.16.2.2  christos  * - Notify to audio_write() that outbuf block available.
   6017  1.16.2.2  christos  * - selnotify() for select/poll-ing processes if there are any writable
   6018  1.16.2.2  christos  *   (used < lowat) processes.  Checking each descriptor will be done by
   6019  1.16.2.2  christos  *   filt_audiowrite_event().
   6020  1.16.2.2  christos  */
   6021  1.16.2.2  christos static void
   6022  1.16.2.2  christos audio_softintr_wr(void *cookie)
   6023  1.16.2.2  christos {
   6024  1.16.2.2  christos 	struct audio_softc *sc = cookie;
   6025  1.16.2.2  christos 	audio_file_t *f;
   6026  1.16.2.2  christos 	bool found;
   6027  1.16.2.2  christos 	pid_t pid;
   6028  1.16.2.2  christos 
   6029  1.16.2.2  christos 	TRACE(4, "called");
   6030  1.16.2.2  christos 	found = false;
   6031  1.16.2.2  christos 
   6032  1.16.2.2  christos 	mutex_enter(sc->sc_lock);
   6033  1.16.2.2  christos 
   6034  1.16.2.2  christos 	SLIST_FOREACH(f, &sc->sc_files, entry) {
   6035  1.16.2.2  christos 		audio_track_t *track = f->ptrack;
   6036  1.16.2.2  christos 
   6037  1.16.2.2  christos 		if (track == NULL)
   6038  1.16.2.2  christos 			continue;
   6039  1.16.2.2  christos 
   6040  1.16.2.2  christos 		TRACET(4, track, "broadcast; trseq=%d out=%d/%d/%d",
   6041  1.16.2.2  christos 		    (int)track->seq,
   6042  1.16.2.2  christos 		    track->outbuf.head,
   6043  1.16.2.2  christos 		    track->outbuf.used,
   6044  1.16.2.2  christos 		    track->outbuf.capacity);
   6045  1.16.2.2  christos 
   6046  1.16.2.2  christos 		/*
   6047  1.16.2.2  christos 		 * Send a signal if the process is async mode and
   6048  1.16.2.2  christos 		 * used is lower than lowat.
   6049  1.16.2.2  christos 		 */
   6050  1.16.2.2  christos 		if (track->usrbuf.used <= track->usrbuf_usedlow &&
   6051  1.16.2.2  christos 		    !track->is_pause) {
   6052  1.16.2.3    martin 			/* For selnotify */
   6053  1.16.2.2  christos 			found = true;
   6054  1.16.2.3    martin 			/* For SIGIO */
   6055  1.16.2.2  christos 			pid = f->async_audio;
   6056  1.16.2.2  christos 			if (pid != 0) {
   6057  1.16.2.2  christos 				TRACEF(4, f, "sending SIGIO %d", pid);
   6058  1.16.2.3    martin 				audio_psignal(sc, pid, SIGIO);
   6059  1.16.2.2  christos 			}
   6060  1.16.2.2  christos 		}
   6061  1.16.2.2  christos 	}
   6062  1.16.2.2  christos 
   6063  1.16.2.2  christos 	/*
   6064  1.16.2.2  christos 	 * Notify for select/poll when someone become writable.
   6065  1.16.2.2  christos 	 * It needs sc_lock (and not sc_intr_lock).
   6066  1.16.2.2  christos 	 */
   6067  1.16.2.2  christos 	if (found) {
   6068  1.16.2.2  christos 		TRACE(4, "selnotify");
   6069  1.16.2.2  christos 		selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT);
   6070  1.16.2.2  christos 		KNOTE(&sc->sc_wsel.sel_klist, 0);
   6071  1.16.2.2  christos 	}
   6072  1.16.2.2  christos 
   6073  1.16.2.2  christos 	/* Notify to audio_write() that outbuf available. */
   6074  1.16.2.2  christos 	cv_broadcast(&sc->sc_pmixer->outcv);
   6075  1.16.2.2  christos 
   6076  1.16.2.2  christos 	mutex_exit(sc->sc_lock);
   6077  1.16.2.2  christos }
   6078  1.16.2.2  christos 
   6079  1.16.2.2  christos /*
   6080  1.16.2.2  christos  * Check (and convert) the format *p came from userland.
   6081  1.16.2.2  christos  * If successful, it writes back the converted format to *p if necessary
   6082  1.16.2.2  christos  * and returns 0.  Otherwise returns errno (*p may change even this case).
   6083  1.16.2.2  christos  */
   6084  1.16.2.2  christos static int
   6085  1.16.2.2  christos audio_check_params(audio_format2_t *p)
   6086  1.16.2.2  christos {
   6087  1.16.2.2  christos 
   6088  1.16.2.2  christos 	/* Convert obsoleted AUDIO_ENCODING_PCM* */
   6089  1.16.2.2  christos 	/* XXX Is this conversion right? */
   6090  1.16.2.2  christos 	if (p->encoding == AUDIO_ENCODING_PCM16) {
   6091  1.16.2.2  christos 		if (p->precision == 8)
   6092  1.16.2.2  christos 			p->encoding = AUDIO_ENCODING_ULINEAR;
   6093  1.16.2.2  christos 		else
   6094  1.16.2.2  christos 			p->encoding = AUDIO_ENCODING_SLINEAR;
   6095  1.16.2.2  christos 	} else if (p->encoding == AUDIO_ENCODING_PCM8) {
   6096  1.16.2.2  christos 		if (p->precision == 8)
   6097  1.16.2.2  christos 			p->encoding = AUDIO_ENCODING_ULINEAR;
   6098  1.16.2.2  christos 		else
   6099  1.16.2.2  christos 			return EINVAL;
   6100  1.16.2.2  christos 	}
   6101  1.16.2.2  christos 
   6102  1.16.2.2  christos 	/*
   6103  1.16.2.2  christos 	 * Convert obsoleted AUDIO_ENCODING_[SU]LINEAR without endianness
   6104  1.16.2.2  christos 	 * suffix.
   6105  1.16.2.2  christos 	 */
   6106  1.16.2.2  christos 	if (p->encoding == AUDIO_ENCODING_SLINEAR)
   6107  1.16.2.2  christos 		p->encoding = AUDIO_ENCODING_SLINEAR_NE;
   6108  1.16.2.2  christos 	if (p->encoding == AUDIO_ENCODING_ULINEAR)
   6109  1.16.2.2  christos 		p->encoding = AUDIO_ENCODING_ULINEAR_NE;
   6110  1.16.2.2  christos 
   6111  1.16.2.2  christos 	switch (p->encoding) {
   6112  1.16.2.2  christos 	case AUDIO_ENCODING_ULAW:
   6113  1.16.2.2  christos 	case AUDIO_ENCODING_ALAW:
   6114  1.16.2.2  christos 		if (p->precision != 8)
   6115  1.16.2.2  christos 			return EINVAL;
   6116  1.16.2.2  christos 		break;
   6117  1.16.2.2  christos 	case AUDIO_ENCODING_ADPCM:
   6118  1.16.2.2  christos 		if (p->precision != 4 && p->precision != 8)
   6119  1.16.2.2  christos 			return EINVAL;
   6120  1.16.2.2  christos 		break;
   6121  1.16.2.2  christos 	case AUDIO_ENCODING_SLINEAR_LE:
   6122  1.16.2.2  christos 	case AUDIO_ENCODING_SLINEAR_BE:
   6123  1.16.2.2  christos 	case AUDIO_ENCODING_ULINEAR_LE:
   6124  1.16.2.2  christos 	case AUDIO_ENCODING_ULINEAR_BE:
   6125  1.16.2.2  christos 		if (p->precision !=  8 && p->precision != 16 &&
   6126  1.16.2.2  christos 		    p->precision != 24 && p->precision != 32)
   6127  1.16.2.2  christos 			return EINVAL;
   6128  1.16.2.2  christos 
   6129  1.16.2.2  christos 		/* 8bit format does not have endianness. */
   6130  1.16.2.2  christos 		if (p->precision == 8) {
   6131  1.16.2.2  christos 			if (p->encoding == AUDIO_ENCODING_SLINEAR_OE)
   6132  1.16.2.2  christos 				p->encoding = AUDIO_ENCODING_SLINEAR_NE;
   6133  1.16.2.2  christos 			if (p->encoding == AUDIO_ENCODING_ULINEAR_OE)
   6134  1.16.2.2  christos 				p->encoding = AUDIO_ENCODING_ULINEAR_NE;
   6135  1.16.2.2  christos 		}
   6136  1.16.2.2  christos 
   6137  1.16.2.2  christos 		if (p->precision > p->stride)
   6138  1.16.2.2  christos 			return EINVAL;
   6139  1.16.2.2  christos 		break;
   6140  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L1_STREAM:
   6141  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L1_PACKETS:
   6142  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L1_SYSTEM:
   6143  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L2_STREAM:
   6144  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L2_PACKETS:
   6145  1.16.2.2  christos 	case AUDIO_ENCODING_MPEG_L2_SYSTEM:
   6146  1.16.2.2  christos 	case AUDIO_ENCODING_AC3:
   6147  1.16.2.2  christos 		break;
   6148  1.16.2.2  christos 	default:
   6149  1.16.2.2  christos 		return EINVAL;
   6150  1.16.2.2  christos 	}
   6151  1.16.2.2  christos 
   6152  1.16.2.2  christos 	/* sanity check # of channels*/
   6153  1.16.2.2  christos 	if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS)
   6154  1.16.2.2  christos 		return EINVAL;
   6155  1.16.2.2  christos 
   6156  1.16.2.2  christos 	return 0;
   6157  1.16.2.2  christos }
   6158  1.16.2.2  christos 
   6159  1.16.2.2  christos /*
   6160  1.16.2.2  christos  * Initialize playback and record mixers.
   6161  1.16.2.3    martin  * mode (AUMODE_{PLAY,RECORD}) indicates the mixer to be initialized.
   6162  1.16.2.2  christos  * phwfmt and rhwfmt indicate the hardware format.  pfil and rfil indicate
   6163  1.16.2.2  christos  * the filter registration information.  These four must not be NULL.
   6164  1.16.2.2  christos  * If successful returns 0.  Otherwise returns errno.
   6165  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   6166  1.16.2.2  christos  * Must not be called if there are any tracks.
   6167  1.16.2.2  christos  * Caller should check that the initialization succeed by whether
   6168  1.16.2.2  christos  * sc_[pr]mixer is not NULL.
   6169  1.16.2.2  christos  */
   6170  1.16.2.2  christos static int
   6171  1.16.2.2  christos audio_mixers_init(struct audio_softc *sc, int mode,
   6172  1.16.2.2  christos 	const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
   6173  1.16.2.2  christos 	const audio_filter_reg_t *pfil, const audio_filter_reg_t *rfil)
   6174  1.16.2.2  christos {
   6175  1.16.2.2  christos 	int error;
   6176  1.16.2.2  christos 
   6177  1.16.2.2  christos 	KASSERT(phwfmt != NULL);
   6178  1.16.2.2  christos 	KASSERT(rhwfmt != NULL);
   6179  1.16.2.2  christos 	KASSERT(pfil != NULL);
   6180  1.16.2.2  christos 	KASSERT(rfil != NULL);
   6181  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   6182  1.16.2.2  christos 
   6183  1.16.2.2  christos 	if ((mode & AUMODE_PLAY)) {
   6184  1.16.2.3    martin 		if (sc->sc_pmixer == NULL) {
   6185  1.16.2.3    martin 			sc->sc_pmixer = kmem_zalloc(sizeof(*sc->sc_pmixer),
   6186  1.16.2.3    martin 			    KM_SLEEP);
   6187  1.16.2.3    martin 		} else {
   6188  1.16.2.3    martin 			/* destroy() doesn't free memory. */
   6189  1.16.2.2  christos 			audio_mixer_destroy(sc, sc->sc_pmixer);
   6190  1.16.2.3    martin 			memset(sc->sc_pmixer, 0, sizeof(*sc->sc_pmixer));
   6191  1.16.2.2  christos 		}
   6192  1.16.2.2  christos 		error = audio_mixer_init(sc, AUMODE_PLAY, phwfmt, pfil);
   6193  1.16.2.2  christos 		if (error) {
   6194  1.16.2.3    martin 			device_printf(sc->sc_dev,
   6195  1.16.2.3    martin 			    "configuring playback mode failed with %d\n",
   6196  1.16.2.3    martin 			    error);
   6197  1.16.2.2  christos 			kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
   6198  1.16.2.2  christos 			sc->sc_pmixer = NULL;
   6199  1.16.2.2  christos 			return error;
   6200  1.16.2.2  christos 		}
   6201  1.16.2.2  christos 	}
   6202  1.16.2.2  christos 	if ((mode & AUMODE_RECORD)) {
   6203  1.16.2.3    martin 		if (sc->sc_rmixer == NULL) {
   6204  1.16.2.3    martin 			sc->sc_rmixer = kmem_zalloc(sizeof(*sc->sc_rmixer),
   6205  1.16.2.3    martin 			    KM_SLEEP);
   6206  1.16.2.3    martin 		} else {
   6207  1.16.2.3    martin 			/* destroy() doesn't free memory. */
   6208  1.16.2.2  christos 			audio_mixer_destroy(sc, sc->sc_rmixer);
   6209  1.16.2.3    martin 			memset(sc->sc_rmixer, 0, sizeof(*sc->sc_rmixer));
   6210  1.16.2.2  christos 		}
   6211  1.16.2.2  christos 		error = audio_mixer_init(sc, AUMODE_RECORD, rhwfmt, rfil);
   6212  1.16.2.2  christos 		if (error) {
   6213  1.16.2.3    martin 			device_printf(sc->sc_dev,
   6214  1.16.2.3    martin 			    "configuring record mode failed with %d\n",
   6215  1.16.2.3    martin 			    error);
   6216  1.16.2.2  christos 			kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
   6217  1.16.2.2  christos 			sc->sc_rmixer = NULL;
   6218  1.16.2.2  christos 			return error;
   6219  1.16.2.2  christos 		}
   6220  1.16.2.2  christos 	}
   6221  1.16.2.2  christos 
   6222  1.16.2.2  christos 	return 0;
   6223  1.16.2.2  christos }
   6224  1.16.2.2  christos 
   6225  1.16.2.2  christos /*
   6226  1.16.2.2  christos  * Select a frequency.
   6227  1.16.2.2  christos  * Prioritize 48kHz and 44.1kHz.  Otherwise choose the highest one.
   6228  1.16.2.2  christos  * XXX Better algorithm?
   6229  1.16.2.2  christos  */
   6230  1.16.2.2  christos static int
   6231  1.16.2.2  christos audio_select_freq(const struct audio_format *fmt)
   6232  1.16.2.2  christos {
   6233  1.16.2.2  christos 	int freq;
   6234  1.16.2.2  christos 	int high;
   6235  1.16.2.2  christos 	int low;
   6236  1.16.2.2  christos 	int j;
   6237  1.16.2.2  christos 
   6238  1.16.2.2  christos 	if (fmt->frequency_type == 0) {
   6239  1.16.2.2  christos 		low = fmt->frequency[0];
   6240  1.16.2.2  christos 		high = fmt->frequency[1];
   6241  1.16.2.2  christos 		freq = 48000;
   6242  1.16.2.2  christos 		if (low <= freq && freq <= high) {
   6243  1.16.2.2  christos 			return freq;
   6244  1.16.2.2  christos 		}
   6245  1.16.2.2  christos 		freq = 44100;
   6246  1.16.2.2  christos 		if (low <= freq && freq <= high) {
   6247  1.16.2.2  christos 			return freq;
   6248  1.16.2.2  christos 		}
   6249  1.16.2.2  christos 		return high;
   6250  1.16.2.2  christos 	} else {
   6251  1.16.2.2  christos 		for (j = 0; j < fmt->frequency_type; j++) {
   6252  1.16.2.2  christos 			if (fmt->frequency[j] == 48000) {
   6253  1.16.2.2  christos 				return fmt->frequency[j];
   6254  1.16.2.2  christos 			}
   6255  1.16.2.2  christos 		}
   6256  1.16.2.2  christos 		high = 0;
   6257  1.16.2.2  christos 		for (j = 0; j < fmt->frequency_type; j++) {
   6258  1.16.2.2  christos 			if (fmt->frequency[j] == 44100) {
   6259  1.16.2.2  christos 				return fmt->frequency[j];
   6260  1.16.2.2  christos 			}
   6261  1.16.2.2  christos 			if (fmt->frequency[j] > high) {
   6262  1.16.2.2  christos 				high = fmt->frequency[j];
   6263  1.16.2.2  christos 			}
   6264  1.16.2.2  christos 		}
   6265  1.16.2.2  christos 		return high;
   6266  1.16.2.2  christos 	}
   6267  1.16.2.2  christos }
   6268  1.16.2.2  christos 
   6269  1.16.2.2  christos /*
   6270  1.16.2.2  christos  * Choose the most preferred hardware format.
   6271  1.16.2.2  christos  * If successful, it will store the chosen format into *cand and return 0.
   6272  1.16.2.2  christos  * Otherwise, return errno.
   6273  1.16.2.3    martin  * Must be called without sc_lock held.
   6274  1.16.2.2  christos  */
   6275  1.16.2.2  christos static int
   6276  1.16.2.3    martin audio_hw_probe(struct audio_softc *sc, audio_format2_t *cand, int mode)
   6277  1.16.2.2  christos {
   6278  1.16.2.2  christos 	audio_format_query_t query;
   6279  1.16.2.2  christos 	int cand_score;
   6280  1.16.2.2  christos 	int score;
   6281  1.16.2.2  christos 	int i;
   6282  1.16.2.2  christos 	int error;
   6283  1.16.2.2  christos 
   6284  1.16.2.2  christos 	/*
   6285  1.16.2.2  christos 	 * Score each formats and choose the highest one.
   6286  1.16.2.2  christos 	 *
   6287  1.16.2.2  christos 	 *                 +---- priority(0-3)
   6288  1.16.2.2  christos 	 *                 |+--- encoding/precision
   6289  1.16.2.2  christos 	 *                 ||+-- channels
   6290  1.16.2.2  christos 	 * score = 0x000000PEC
   6291  1.16.2.2  christos 	 */
   6292  1.16.2.2  christos 
   6293  1.16.2.2  christos 	cand_score = 0;
   6294  1.16.2.2  christos 	for (i = 0; ; i++) {
   6295  1.16.2.2  christos 		memset(&query, 0, sizeof(query));
   6296  1.16.2.2  christos 		query.index = i;
   6297  1.16.2.2  christos 
   6298  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   6299  1.16.2.2  christos 		error = sc->hw_if->query_format(sc->hw_hdl, &query);
   6300  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   6301  1.16.2.2  christos 		if (error == EINVAL)
   6302  1.16.2.2  christos 			break;
   6303  1.16.2.2  christos 		if (error)
   6304  1.16.2.2  christos 			return error;
   6305  1.16.2.2  christos 
   6306  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   6307  1.16.2.2  christos 		DPRINTF(1, "fmt[%d] %c%c pri=%d %s,%d/%dbit,%dch,", i,
   6308  1.16.2.2  christos 		    (query.fmt.mode & AUMODE_PLAY)   ? 'P' : '-',
   6309  1.16.2.2  christos 		    (query.fmt.mode & AUMODE_RECORD) ? 'R' : '-',
   6310  1.16.2.2  christos 		    query.fmt.priority,
   6311  1.16.2.2  christos 		    audio_encoding_name(query.fmt.encoding),
   6312  1.16.2.2  christos 		    query.fmt.validbits,
   6313  1.16.2.2  christos 		    query.fmt.precision,
   6314  1.16.2.2  christos 		    query.fmt.channels);
   6315  1.16.2.2  christos 		if (query.fmt.frequency_type == 0) {
   6316  1.16.2.2  christos 			DPRINTF(1, "{%d-%d",
   6317  1.16.2.2  christos 			    query.fmt.frequency[0], query.fmt.frequency[1]);
   6318  1.16.2.2  christos 		} else {
   6319  1.16.2.2  christos 			int j;
   6320  1.16.2.2  christos 			for (j = 0; j < query.fmt.frequency_type; j++) {
   6321  1.16.2.2  christos 				DPRINTF(1, "%c%d",
   6322  1.16.2.2  christos 				    (j == 0) ? '{' : ',',
   6323  1.16.2.2  christos 				    query.fmt.frequency[j]);
   6324  1.16.2.2  christos 			}
   6325  1.16.2.2  christos 		}
   6326  1.16.2.2  christos 		DPRINTF(1, "}\n");
   6327  1.16.2.2  christos #endif
   6328  1.16.2.2  christos 
   6329  1.16.2.2  christos 		if ((query.fmt.mode & mode) == 0) {
   6330  1.16.2.2  christos 			DPRINTF(1, "fmt[%d] skip; mode not match %d\n", i,
   6331  1.16.2.2  christos 			    mode);
   6332  1.16.2.2  christos 			continue;
   6333  1.16.2.2  christos 		}
   6334  1.16.2.2  christos 
   6335  1.16.2.2  christos 		if (query.fmt.priority < 0) {
   6336  1.16.2.2  christos 			DPRINTF(1, "fmt[%d] skip; unsupported encoding\n", i);
   6337  1.16.2.2  christos 			continue;
   6338  1.16.2.2  christos 		}
   6339  1.16.2.2  christos 
   6340  1.16.2.2  christos 		/* Score */
   6341  1.16.2.2  christos 		score = (query.fmt.priority & 3) * 0x100;
   6342  1.16.2.2  christos 		if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_NE &&
   6343  1.16.2.2  christos 		    query.fmt.validbits == AUDIO_INTERNAL_BITS &&
   6344  1.16.2.2  christos 		    query.fmt.precision == AUDIO_INTERNAL_BITS) {
   6345  1.16.2.2  christos 			score += 0x20;
   6346  1.16.2.2  christos 		} else if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
   6347  1.16.2.2  christos 		    query.fmt.validbits == AUDIO_INTERNAL_BITS &&
   6348  1.16.2.2  christos 		    query.fmt.precision == AUDIO_INTERNAL_BITS) {
   6349  1.16.2.2  christos 			score += 0x10;
   6350  1.16.2.2  christos 		}
   6351  1.16.2.2  christos 		score += query.fmt.channels;
   6352  1.16.2.2  christos 
   6353  1.16.2.2  christos 		if (score < cand_score) {
   6354  1.16.2.2  christos 			DPRINTF(1, "fmt[%d] skip; score 0x%x < 0x%x\n", i,
   6355  1.16.2.2  christos 			    score, cand_score);
   6356  1.16.2.2  christos 			continue;
   6357  1.16.2.2  christos 		}
   6358  1.16.2.2  christos 
   6359  1.16.2.2  christos 		/* Update candidate */
   6360  1.16.2.2  christos 		cand_score = score;
   6361  1.16.2.2  christos 		cand->encoding    = query.fmt.encoding;
   6362  1.16.2.2  christos 		cand->precision   = query.fmt.validbits;
   6363  1.16.2.2  christos 		cand->stride      = query.fmt.precision;
   6364  1.16.2.2  christos 		cand->channels    = query.fmt.channels;
   6365  1.16.2.2  christos 		cand->sample_rate = audio_select_freq(&query.fmt);
   6366  1.16.2.2  christos 		DPRINTF(1, "fmt[%d] candidate (score=0x%x)"
   6367  1.16.2.2  christos 		    " pri=%d %s,%d/%d,%dch,%dHz\n", i,
   6368  1.16.2.2  christos 		    cand_score, query.fmt.priority,
   6369  1.16.2.2  christos 		    audio_encoding_name(query.fmt.encoding),
   6370  1.16.2.2  christos 		    cand->precision, cand->stride,
   6371  1.16.2.2  christos 		    cand->channels, cand->sample_rate);
   6372  1.16.2.2  christos 	}
   6373  1.16.2.2  christos 
   6374  1.16.2.2  christos 	if (cand_score == 0) {
   6375  1.16.2.2  christos 		DPRINTF(1, "%s no fmt\n", __func__);
   6376  1.16.2.2  christos 		return ENXIO;
   6377  1.16.2.2  christos 	}
   6378  1.16.2.2  christos 	DPRINTF(1, "%s selected: %s,%d/%d,%dch,%dHz\n", __func__,
   6379  1.16.2.2  christos 	    audio_encoding_name(cand->encoding),
   6380  1.16.2.2  christos 	    cand->precision, cand->stride, cand->channels, cand->sample_rate);
   6381  1.16.2.2  christos 	return 0;
   6382  1.16.2.2  christos }
   6383  1.16.2.2  christos 
   6384  1.16.2.2  christos /*
   6385  1.16.2.2  christos  * Validate fmt with query_format.
   6386  1.16.2.2  christos  * If fmt is included in the result of query_format, returns 0.
   6387  1.16.2.2  christos  * Otherwise returns EINVAL.
   6388  1.16.2.3    martin  * Must be called without sc_lock held.
   6389  1.16.2.2  christos  */
   6390  1.16.2.2  christos static int
   6391  1.16.2.2  christos audio_hw_validate_format(struct audio_softc *sc, int mode,
   6392  1.16.2.2  christos 	const audio_format2_t *fmt)
   6393  1.16.2.2  christos {
   6394  1.16.2.2  christos 	audio_format_query_t query;
   6395  1.16.2.2  christos 	struct audio_format *q;
   6396  1.16.2.2  christos 	int index;
   6397  1.16.2.2  christos 	int error;
   6398  1.16.2.2  christos 	int j;
   6399  1.16.2.2  christos 
   6400  1.16.2.2  christos 	for (index = 0; ; index++) {
   6401  1.16.2.2  christos 		query.index = index;
   6402  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   6403  1.16.2.2  christos 		error = sc->hw_if->query_format(sc->hw_hdl, &query);
   6404  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   6405  1.16.2.2  christos 		if (error == EINVAL)
   6406  1.16.2.2  christos 			break;
   6407  1.16.2.2  christos 		if (error)
   6408  1.16.2.2  christos 			return error;
   6409  1.16.2.2  christos 
   6410  1.16.2.2  christos 		q = &query.fmt;
   6411  1.16.2.2  christos 		/*
   6412  1.16.2.2  christos 		 * Note that fmt is audio_format2_t (precision/stride) but
   6413  1.16.2.2  christos 		 * q is audio_format_t (validbits/precision).
   6414  1.16.2.2  christos 		 */
   6415  1.16.2.2  christos 		if ((q->mode & mode) == 0) {
   6416  1.16.2.2  christos 			continue;
   6417  1.16.2.2  christos 		}
   6418  1.16.2.2  christos 		if (fmt->encoding != q->encoding) {
   6419  1.16.2.2  christos 			continue;
   6420  1.16.2.2  christos 		}
   6421  1.16.2.2  christos 		if (fmt->precision != q->validbits) {
   6422  1.16.2.2  christos 			continue;
   6423  1.16.2.2  christos 		}
   6424  1.16.2.2  christos 		if (fmt->stride != q->precision) {
   6425  1.16.2.2  christos 			continue;
   6426  1.16.2.2  christos 		}
   6427  1.16.2.2  christos 		if (fmt->channels != q->channels) {
   6428  1.16.2.2  christos 			continue;
   6429  1.16.2.2  christos 		}
   6430  1.16.2.2  christos 		if (q->frequency_type == 0) {
   6431  1.16.2.2  christos 			if (fmt->sample_rate < q->frequency[0] ||
   6432  1.16.2.2  christos 			    fmt->sample_rate > q->frequency[1]) {
   6433  1.16.2.2  christos 				continue;
   6434  1.16.2.2  christos 			}
   6435  1.16.2.2  christos 		} else {
   6436  1.16.2.2  christos 			for (j = 0; j < q->frequency_type; j++) {
   6437  1.16.2.2  christos 				if (fmt->sample_rate == q->frequency[j])
   6438  1.16.2.2  christos 					break;
   6439  1.16.2.2  christos 			}
   6440  1.16.2.2  christos 			if (j == query.fmt.frequency_type) {
   6441  1.16.2.2  christos 				continue;
   6442  1.16.2.2  christos 			}
   6443  1.16.2.2  christos 		}
   6444  1.16.2.2  christos 
   6445  1.16.2.2  christos 		/* Matched. */
   6446  1.16.2.2  christos 		return 0;
   6447  1.16.2.2  christos 	}
   6448  1.16.2.2  christos 
   6449  1.16.2.2  christos 	return EINVAL;
   6450  1.16.2.2  christos }
   6451  1.16.2.2  christos 
   6452  1.16.2.2  christos /*
   6453  1.16.2.2  christos  * Set track mixer's format depending on ai->mode.
   6454  1.16.2.2  christos  * If AUMODE_PLAY is set in ai->mode, it set up the playback mixer
   6455  1.16.2.3    martin  * with ai.play.*.
   6456  1.16.2.2  christos  * If AUMODE_RECORD is set in ai->mode, it set up the recording mixer
   6457  1.16.2.3    martin  * with ai.record.*.
   6458  1.16.2.2  christos  * All other fields in ai are ignored.
   6459  1.16.2.2  christos  * If successful returns 0.  Otherwise returns errno.
   6460  1.16.2.2  christos  * This function does not roll back even if it fails.
   6461  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   6462  1.16.2.2  christos  */
   6463  1.16.2.2  christos static int
   6464  1.16.2.2  christos audio_mixers_set_format(struct audio_softc *sc, const struct audio_info *ai)
   6465  1.16.2.2  christos {
   6466  1.16.2.2  christos 	audio_format2_t phwfmt;
   6467  1.16.2.2  christos 	audio_format2_t rhwfmt;
   6468  1.16.2.2  christos 	audio_filter_reg_t pfil;
   6469  1.16.2.2  christos 	audio_filter_reg_t rfil;
   6470  1.16.2.2  christos 	int mode;
   6471  1.16.2.2  christos 	int error;
   6472  1.16.2.2  christos 
   6473  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   6474  1.16.2.2  christos 
   6475  1.16.2.2  christos 	/*
   6476  1.16.2.2  christos 	 * Even when setting either one of playback and recording,
   6477  1.16.2.2  christos 	 * both must be halted.
   6478  1.16.2.2  christos 	 */
   6479  1.16.2.2  christos 	if (sc->sc_popens + sc->sc_ropens > 0)
   6480  1.16.2.2  christos 		return EBUSY;
   6481  1.16.2.2  christos 
   6482  1.16.2.2  christos 	if (!SPECIFIED(ai->mode) || ai->mode == 0)
   6483  1.16.2.2  christos 		return ENOTTY;
   6484  1.16.2.2  christos 
   6485  1.16.2.2  christos 	mode = ai->mode;
   6486  1.16.2.2  christos 	if ((mode & AUMODE_PLAY)) {
   6487  1.16.2.2  christos 		phwfmt.encoding    = ai->play.encoding;
   6488  1.16.2.2  christos 		phwfmt.precision   = ai->play.precision;
   6489  1.16.2.2  christos 		phwfmt.stride      = ai->play.precision;
   6490  1.16.2.2  christos 		phwfmt.channels    = ai->play.channels;
   6491  1.16.2.2  christos 		phwfmt.sample_rate = ai->play.sample_rate;
   6492  1.16.2.2  christos 	}
   6493  1.16.2.2  christos 	if ((mode & AUMODE_RECORD)) {
   6494  1.16.2.2  christos 		rhwfmt.encoding    = ai->record.encoding;
   6495  1.16.2.2  christos 		rhwfmt.precision   = ai->record.precision;
   6496  1.16.2.2  christos 		rhwfmt.stride      = ai->record.precision;
   6497  1.16.2.2  christos 		rhwfmt.channels    = ai->record.channels;
   6498  1.16.2.2  christos 		rhwfmt.sample_rate = ai->record.sample_rate;
   6499  1.16.2.2  christos 	}
   6500  1.16.2.2  christos 
   6501  1.16.2.2  christos 	/* On non-independent devices, use the same format for both. */
   6502  1.16.2.3    martin 	if ((sc->sc_props & AUDIO_PROP_INDEPENDENT) == 0) {
   6503  1.16.2.2  christos 		if (mode == AUMODE_RECORD) {
   6504  1.16.2.2  christos 			phwfmt = rhwfmt;
   6505  1.16.2.2  christos 		} else {
   6506  1.16.2.2  christos 			rhwfmt = phwfmt;
   6507  1.16.2.2  christos 		}
   6508  1.16.2.2  christos 		mode = AUMODE_PLAY | AUMODE_RECORD;
   6509  1.16.2.2  christos 	}
   6510  1.16.2.2  christos 
   6511  1.16.2.2  christos 	/* Then, unset the direction not exist on the hardware. */
   6512  1.16.2.3    martin 	if ((sc->sc_props & AUDIO_PROP_PLAYBACK) == 0)
   6513  1.16.2.2  christos 		mode &= ~AUMODE_PLAY;
   6514  1.16.2.3    martin 	if ((sc->sc_props & AUDIO_PROP_CAPTURE) == 0)
   6515  1.16.2.2  christos 		mode &= ~AUMODE_RECORD;
   6516  1.16.2.2  christos 
   6517  1.16.2.2  christos 	/* debug */
   6518  1.16.2.2  christos 	if ((mode & AUMODE_PLAY)) {
   6519  1.16.2.2  christos 		TRACE(1, "play=%s/%d/%d/%dch/%dHz",
   6520  1.16.2.2  christos 		    audio_encoding_name(phwfmt.encoding),
   6521  1.16.2.2  christos 		    phwfmt.precision,
   6522  1.16.2.2  christos 		    phwfmt.stride,
   6523  1.16.2.2  christos 		    phwfmt.channels,
   6524  1.16.2.2  christos 		    phwfmt.sample_rate);
   6525  1.16.2.2  christos 	}
   6526  1.16.2.2  christos 	if ((mode & AUMODE_RECORD)) {
   6527  1.16.2.2  christos 		TRACE(1, "rec =%s/%d/%d/%dch/%dHz",
   6528  1.16.2.2  christos 		    audio_encoding_name(rhwfmt.encoding),
   6529  1.16.2.2  christos 		    rhwfmt.precision,
   6530  1.16.2.2  christos 		    rhwfmt.stride,
   6531  1.16.2.2  christos 		    rhwfmt.channels,
   6532  1.16.2.2  christos 		    rhwfmt.sample_rate);
   6533  1.16.2.2  christos 	}
   6534  1.16.2.2  christos 
   6535  1.16.2.2  christos 	/* Check the format */
   6536  1.16.2.2  christos 	if ((mode & AUMODE_PLAY)) {
   6537  1.16.2.2  christos 		if (audio_hw_validate_format(sc, AUMODE_PLAY, &phwfmt)) {
   6538  1.16.2.2  christos 			TRACE(1, "invalid format");
   6539  1.16.2.2  christos 			return EINVAL;
   6540  1.16.2.2  christos 		}
   6541  1.16.2.2  christos 	}
   6542  1.16.2.2  christos 	if ((mode & AUMODE_RECORD)) {
   6543  1.16.2.2  christos 		if (audio_hw_validate_format(sc, AUMODE_RECORD, &rhwfmt)) {
   6544  1.16.2.2  christos 			TRACE(1, "invalid format");
   6545  1.16.2.2  christos 			return EINVAL;
   6546  1.16.2.2  christos 		}
   6547  1.16.2.2  christos 	}
   6548  1.16.2.2  christos 
   6549  1.16.2.2  christos 	/* Configure the mixers. */
   6550  1.16.2.2  christos 	memset(&pfil, 0, sizeof(pfil));
   6551  1.16.2.2  christos 	memset(&rfil, 0, sizeof(rfil));
   6552  1.16.2.2  christos 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   6553  1.16.2.2  christos 	if (error)
   6554  1.16.2.2  christos 		return error;
   6555  1.16.2.2  christos 
   6556  1.16.2.2  christos 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   6557  1.16.2.2  christos 	if (error)
   6558  1.16.2.2  christos 		return error;
   6559  1.16.2.2  christos 
   6560  1.16.2.3    martin 	/*
   6561  1.16.2.3    martin 	 * Reinitialize the sticky parameters for /dev/sound.
   6562  1.16.2.3    martin 	 * If the number of the hardware channels becomes less than the number
   6563  1.16.2.3    martin 	 * of channels that sticky parameters remember, subsequent /dev/sound
   6564  1.16.2.3    martin 	 * open will fail.  To prevent this, reinitialize the sticky
   6565  1.16.2.3    martin 	 * parameters whenever the hardware format is changed.
   6566  1.16.2.3    martin 	 */
   6567  1.16.2.3    martin 	sc->sc_sound_pparams = params_to_format2(&audio_default);
   6568  1.16.2.3    martin 	sc->sc_sound_rparams = params_to_format2(&audio_default);
   6569  1.16.2.3    martin 	sc->sc_sound_ppause = false;
   6570  1.16.2.3    martin 	sc->sc_sound_rpause = false;
   6571  1.16.2.3    martin 
   6572  1.16.2.2  christos 	return 0;
   6573  1.16.2.2  christos }
   6574  1.16.2.2  christos 
   6575  1.16.2.2  christos /*
   6576  1.16.2.2  christos  * Store current mixers format into *ai.
   6577  1.16.2.3    martin  * Must be called with sc_exlock held.
   6578  1.16.2.2  christos  */
   6579  1.16.2.2  christos static void
   6580  1.16.2.2  christos audio_mixers_get_format(struct audio_softc *sc, struct audio_info *ai)
   6581  1.16.2.2  christos {
   6582  1.16.2.3    martin 
   6583  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   6584  1.16.2.3    martin 
   6585  1.16.2.2  christos 	/*
   6586  1.16.2.2  christos 	 * There is no stride information in audio_info but it doesn't matter.
   6587  1.16.2.2  christos 	 * trackmixer always treats stride and precision as the same.
   6588  1.16.2.2  christos 	 */
   6589  1.16.2.2  christos 	AUDIO_INITINFO(ai);
   6590  1.16.2.2  christos 	ai->mode = 0;
   6591  1.16.2.2  christos 	if (sc->sc_pmixer) {
   6592  1.16.2.2  christos 		audio_format2_t *fmt = &sc->sc_pmixer->track_fmt;
   6593  1.16.2.2  christos 		ai->play.encoding    = fmt->encoding;
   6594  1.16.2.2  christos 		ai->play.precision   = fmt->precision;
   6595  1.16.2.2  christos 		ai->play.channels    = fmt->channels;
   6596  1.16.2.2  christos 		ai->play.sample_rate = fmt->sample_rate;
   6597  1.16.2.2  christos 		ai->mode |= AUMODE_PLAY;
   6598  1.16.2.2  christos 	}
   6599  1.16.2.2  christos 	if (sc->sc_rmixer) {
   6600  1.16.2.2  christos 		audio_format2_t *fmt = &sc->sc_rmixer->track_fmt;
   6601  1.16.2.2  christos 		ai->record.encoding    = fmt->encoding;
   6602  1.16.2.2  christos 		ai->record.precision   = fmt->precision;
   6603  1.16.2.2  christos 		ai->record.channels    = fmt->channels;
   6604  1.16.2.2  christos 		ai->record.sample_rate = fmt->sample_rate;
   6605  1.16.2.2  christos 		ai->mode |= AUMODE_RECORD;
   6606  1.16.2.2  christos 	}
   6607  1.16.2.2  christos }
   6608  1.16.2.2  christos 
   6609  1.16.2.2  christos /*
   6610  1.16.2.2  christos  * audio_info details:
   6611  1.16.2.2  christos  *
   6612  1.16.2.2  christos  * ai.{play,record}.sample_rate		(R/W)
   6613  1.16.2.2  christos  * ai.{play,record}.encoding		(R/W)
   6614  1.16.2.2  christos  * ai.{play,record}.precision		(R/W)
   6615  1.16.2.2  christos  * ai.{play,record}.channels		(R/W)
   6616  1.16.2.2  christos  *	These specify the playback or recording format.
   6617  1.16.2.2  christos  *	Ignore members within an inactive track.
   6618  1.16.2.2  christos  *
   6619  1.16.2.2  christos  * ai.mode				(R/W)
   6620  1.16.2.2  christos  *	It specifies the playback or recording mode, AUMODE_*.
   6621  1.16.2.2  christos  *	Currently, a mode change operation by ai.mode after opening is
   6622  1.16.2.2  christos  *	prohibited.  In addition, AUMODE_PLAY_ALL no longer makes sense.
   6623  1.16.2.2  christos  *	However, it's possible to get or to set for backward compatibility.
   6624  1.16.2.2  christos  *
   6625  1.16.2.2  christos  * ai.{hiwat,lowat}			(R/W)
   6626  1.16.2.2  christos  *	These specify the high water mark and low water mark for playback
   6627  1.16.2.2  christos  *	track.  The unit is block.
   6628  1.16.2.2  christos  *
   6629  1.16.2.2  christos  * ai.{play,record}.gain		(R/W)
   6630  1.16.2.2  christos  *	It specifies the HW mixer volume in 0-255.
   6631  1.16.2.2  christos  *	It is historical reason that the gain is connected to HW mixer.
   6632  1.16.2.2  christos  *
   6633  1.16.2.2  christos  * ai.{play,record}.balance		(R/W)
   6634  1.16.2.2  christos  *	It specifies the left-right balance of HW mixer in 0-64.
   6635  1.16.2.2  christos  *	32 means the center.
   6636  1.16.2.2  christos  *	It is historical reason that the balance is connected to HW mixer.
   6637  1.16.2.2  christos  *
   6638  1.16.2.2  christos  * ai.{play,record}.port		(R/W)
   6639  1.16.2.2  christos  *	It specifies the input/output port of HW mixer.
   6640  1.16.2.2  christos  *
   6641  1.16.2.2  christos  * ai.monitor_gain			(R/W)
   6642  1.16.2.2  christos  *	It specifies the recording monitor gain(?) of HW mixer.
   6643  1.16.2.2  christos  *
   6644  1.16.2.2  christos  * ai.{play,record}.pause		(R/W)
   6645  1.16.2.2  christos  *	Non-zero means the track is paused.
   6646  1.16.2.2  christos  *
   6647  1.16.2.2  christos  * ai.play.seek				(R/-)
   6648  1.16.2.2  christos  *	It indicates the number of bytes written but not processed.
   6649  1.16.2.2  christos  * ai.record.seek			(R/-)
   6650  1.16.2.2  christos  *	It indicates the number of bytes to be able to read.
   6651  1.16.2.2  christos  *
   6652  1.16.2.2  christos  * ai.{play,record}.avail_ports		(R/-)
   6653  1.16.2.2  christos  *	Mixer info.
   6654  1.16.2.2  christos  *
   6655  1.16.2.2  christos  * ai.{play,record}.buffer_size		(R/-)
   6656  1.16.2.2  christos  *	It indicates the buffer size in bytes.  Internally it means usrbuf.
   6657  1.16.2.2  christos  *
   6658  1.16.2.2  christos  * ai.{play,record}.samples		(R/-)
   6659  1.16.2.2  christos  *	It indicates the total number of bytes played or recorded.
   6660  1.16.2.2  christos  *
   6661  1.16.2.2  christos  * ai.{play,record}.eof			(R/-)
   6662  1.16.2.2  christos  *	It indicates the number of times reached EOF(?).
   6663  1.16.2.2  christos  *
   6664  1.16.2.2  christos  * ai.{play,record}.error		(R/-)
   6665  1.16.2.2  christos  *	Non-zero indicates overflow/underflow has occured.
   6666  1.16.2.2  christos  *
   6667  1.16.2.2  christos  * ai.{play,record}.waiting		(R/-)
   6668  1.16.2.2  christos  *	Non-zero indicates that other process waits to open.
   6669  1.16.2.2  christos  *	It will never happen anymore.
   6670  1.16.2.2  christos  *
   6671  1.16.2.2  christos  * ai.{play,record}.open		(R/-)
   6672  1.16.2.2  christos  *	Non-zero indicates the direction is opened by this process(?).
   6673  1.16.2.2  christos  *	XXX Is this better to indicate that "the device is opened by
   6674  1.16.2.2  christos  *	at least one process"?
   6675  1.16.2.2  christos  *
   6676  1.16.2.2  christos  * ai.{play,record}.active		(R/-)
   6677  1.16.2.2  christos  *	Non-zero indicates that I/O is currently active.
   6678  1.16.2.2  christos  *
   6679  1.16.2.2  christos  * ai.blocksize				(R/-)
   6680  1.16.2.2  christos  *	It indicates the block size in bytes.
   6681  1.16.2.2  christos  *	XXX The blocksize of playback and recording may be different.
   6682  1.16.2.2  christos  */
   6683  1.16.2.2  christos 
   6684  1.16.2.2  christos /*
   6685  1.16.2.2  christos  * Pause consideration:
   6686  1.16.2.2  christos  *
   6687  1.16.2.3    martin  * Pausing/unpausing never affect [pr]mixer.  This single rule makes
   6688  1.16.2.3    martin  * operation simple.  Note that playback and recording are asymmetric.
   6689  1.16.2.3    martin  *
   6690  1.16.2.3    martin  * For playback,
   6691  1.16.2.3    martin  *  1. Any playback open doesn't start pmixer regardless of initial pause
   6692  1.16.2.3    martin  *     state of this track.
   6693  1.16.2.3    martin  *  2. The first write access among playback tracks only starts pmixer
   6694  1.16.2.3    martin  *     regardless of this track's pause state.
   6695  1.16.2.3    martin  *  3. Even a pause of the last playback track doesn't stop pmixer.
   6696  1.16.2.3    martin  *  4. The last close of all playback tracks only stops pmixer.
   6697  1.16.2.3    martin  *
   6698  1.16.2.3    martin  * For recording,
   6699  1.16.2.3    martin  *  1. The first recording open only starts rmixer regardless of initial
   6700  1.16.2.3    martin  *     pause state of this track.
   6701  1.16.2.3    martin  *  2. Even a pause of the last track doesn't stop rmixer.
   6702  1.16.2.3    martin  *  3. The last close of all recording tracks only stops rmixer.
   6703  1.16.2.2  christos  */
   6704  1.16.2.2  christos 
   6705  1.16.2.2  christos /*
   6706  1.16.2.2  christos  * Set both track's parameters within a file depending on ai.
   6707  1.16.2.2  christos  * Update sc_sound_[pr]* if set.
   6708  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   6709  1.16.2.2  christos  */
   6710  1.16.2.2  christos static int
   6711  1.16.2.2  christos audio_file_setinfo(struct audio_softc *sc, audio_file_t *file,
   6712  1.16.2.2  christos 	const struct audio_info *ai)
   6713  1.16.2.2  christos {
   6714  1.16.2.2  christos 	const struct audio_prinfo *pi;
   6715  1.16.2.2  christos 	const struct audio_prinfo *ri;
   6716  1.16.2.2  christos 	audio_track_t *ptrack;
   6717  1.16.2.2  christos 	audio_track_t *rtrack;
   6718  1.16.2.2  christos 	audio_format2_t pfmt;
   6719  1.16.2.2  christos 	audio_format2_t rfmt;
   6720  1.16.2.2  christos 	int pchanges;
   6721  1.16.2.2  christos 	int rchanges;
   6722  1.16.2.2  christos 	int mode;
   6723  1.16.2.2  christos 	struct audio_info saved_ai;
   6724  1.16.2.2  christos 	audio_format2_t saved_pfmt;
   6725  1.16.2.2  christos 	audio_format2_t saved_rfmt;
   6726  1.16.2.2  christos 	int error;
   6727  1.16.2.2  christos 
   6728  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   6729  1.16.2.2  christos 
   6730  1.16.2.2  christos 	pi = &ai->play;
   6731  1.16.2.2  christos 	ri = &ai->record;
   6732  1.16.2.2  christos 	pchanges = 0;
   6733  1.16.2.2  christos 	rchanges = 0;
   6734  1.16.2.2  christos 
   6735  1.16.2.2  christos 	ptrack = file->ptrack;
   6736  1.16.2.2  christos 	rtrack = file->rtrack;
   6737  1.16.2.2  christos 
   6738  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   6739  1.16.2.2  christos 	if (audiodebug >= 2) {
   6740  1.16.2.2  christos 		char buf[256];
   6741  1.16.2.2  christos 		char p[64];
   6742  1.16.2.2  christos 		int buflen;
   6743  1.16.2.2  christos 		int plen;
   6744  1.16.2.2  christos #define SPRINTF(var, fmt...) do {	\
   6745  1.16.2.2  christos 	var##len += snprintf(var + var##len, sizeof(var) - var##len, fmt); \
   6746  1.16.2.2  christos } while (0)
   6747  1.16.2.2  christos 
   6748  1.16.2.2  christos 		buflen = 0;
   6749  1.16.2.2  christos 		plen = 0;
   6750  1.16.2.2  christos 		if (SPECIFIED(pi->encoding))
   6751  1.16.2.2  christos 			SPRINTF(p, "/%s", audio_encoding_name(pi->encoding));
   6752  1.16.2.2  christos 		if (SPECIFIED(pi->precision))
   6753  1.16.2.2  christos 			SPRINTF(p, "/%dbit", pi->precision);
   6754  1.16.2.2  christos 		if (SPECIFIED(pi->channels))
   6755  1.16.2.2  christos 			SPRINTF(p, "/%dch", pi->channels);
   6756  1.16.2.2  christos 		if (SPECIFIED(pi->sample_rate))
   6757  1.16.2.2  christos 			SPRINTF(p, "/%dHz", pi->sample_rate);
   6758  1.16.2.2  christos 		if (plen > 0)
   6759  1.16.2.2  christos 			SPRINTF(buf, ",play.param=%s", p + 1);
   6760  1.16.2.2  christos 
   6761  1.16.2.2  christos 		plen = 0;
   6762  1.16.2.2  christos 		if (SPECIFIED(ri->encoding))
   6763  1.16.2.2  christos 			SPRINTF(p, "/%s", audio_encoding_name(ri->encoding));
   6764  1.16.2.2  christos 		if (SPECIFIED(ri->precision))
   6765  1.16.2.2  christos 			SPRINTF(p, "/%dbit", ri->precision);
   6766  1.16.2.2  christos 		if (SPECIFIED(ri->channels))
   6767  1.16.2.2  christos 			SPRINTF(p, "/%dch", ri->channels);
   6768  1.16.2.2  christos 		if (SPECIFIED(ri->sample_rate))
   6769  1.16.2.2  christos 			SPRINTF(p, "/%dHz", ri->sample_rate);
   6770  1.16.2.2  christos 		if (plen > 0)
   6771  1.16.2.2  christos 			SPRINTF(buf, ",record.param=%s", p + 1);
   6772  1.16.2.2  christos 
   6773  1.16.2.2  christos 		if (SPECIFIED(ai->mode))
   6774  1.16.2.2  christos 			SPRINTF(buf, ",mode=%d", ai->mode);
   6775  1.16.2.2  christos 		if (SPECIFIED(ai->hiwat))
   6776  1.16.2.2  christos 			SPRINTF(buf, ",hiwat=%d", ai->hiwat);
   6777  1.16.2.2  christos 		if (SPECIFIED(ai->lowat))
   6778  1.16.2.2  christos 			SPRINTF(buf, ",lowat=%d", ai->lowat);
   6779  1.16.2.2  christos 		if (SPECIFIED(ai->play.gain))
   6780  1.16.2.2  christos 			SPRINTF(buf, ",play.gain=%d", ai->play.gain);
   6781  1.16.2.2  christos 		if (SPECIFIED(ai->record.gain))
   6782  1.16.2.2  christos 			SPRINTF(buf, ",record.gain=%d", ai->record.gain);
   6783  1.16.2.2  christos 		if (SPECIFIED_CH(ai->play.balance))
   6784  1.16.2.2  christos 			SPRINTF(buf, ",play.balance=%d", ai->play.balance);
   6785  1.16.2.2  christos 		if (SPECIFIED_CH(ai->record.balance))
   6786  1.16.2.2  christos 			SPRINTF(buf, ",record.balance=%d", ai->record.balance);
   6787  1.16.2.2  christos 		if (SPECIFIED(ai->play.port))
   6788  1.16.2.2  christos 			SPRINTF(buf, ",play.port=%d", ai->play.port);
   6789  1.16.2.2  christos 		if (SPECIFIED(ai->record.port))
   6790  1.16.2.2  christos 			SPRINTF(buf, ",record.port=%d", ai->record.port);
   6791  1.16.2.2  christos 		if (SPECIFIED(ai->monitor_gain))
   6792  1.16.2.2  christos 			SPRINTF(buf, ",monitor_gain=%d", ai->monitor_gain);
   6793  1.16.2.2  christos 		if (SPECIFIED_CH(ai->play.pause))
   6794  1.16.2.2  christos 			SPRINTF(buf, ",play.pause=%d", ai->play.pause);
   6795  1.16.2.2  christos 		if (SPECIFIED_CH(ai->record.pause))
   6796  1.16.2.2  christos 			SPRINTF(buf, ",record.pause=%d", ai->record.pause);
   6797  1.16.2.2  christos 
   6798  1.16.2.2  christos 		if (buflen > 0)
   6799  1.16.2.2  christos 			TRACE(2, "specified %s", buf + 1);
   6800  1.16.2.2  christos 	}
   6801  1.16.2.2  christos #endif
   6802  1.16.2.2  christos 
   6803  1.16.2.2  christos 	AUDIO_INITINFO(&saved_ai);
   6804  1.16.2.2  christos 	/* XXX shut up gcc */
   6805  1.16.2.2  christos 	memset(&saved_pfmt, 0, sizeof(saved_pfmt));
   6806  1.16.2.2  christos 	memset(&saved_rfmt, 0, sizeof(saved_rfmt));
   6807  1.16.2.2  christos 
   6808  1.16.2.3    martin 	/*
   6809  1.16.2.3    martin 	 * Set default value and save current parameters.
   6810  1.16.2.3    martin 	 * For backward compatibility, use sticky parameters for nonexistent
   6811  1.16.2.3    martin 	 * track.
   6812  1.16.2.3    martin 	 */
   6813  1.16.2.2  christos 	if (ptrack) {
   6814  1.16.2.2  christos 		pfmt = ptrack->usrbuf.fmt;
   6815  1.16.2.2  christos 		saved_pfmt = ptrack->usrbuf.fmt;
   6816  1.16.2.2  christos 		saved_ai.play.pause = ptrack->is_pause;
   6817  1.16.2.3    martin 	} else {
   6818  1.16.2.3    martin 		pfmt = sc->sc_sound_pparams;
   6819  1.16.2.2  christos 	}
   6820  1.16.2.2  christos 	if (rtrack) {
   6821  1.16.2.2  christos 		rfmt = rtrack->usrbuf.fmt;
   6822  1.16.2.2  christos 		saved_rfmt = rtrack->usrbuf.fmt;
   6823  1.16.2.2  christos 		saved_ai.record.pause = rtrack->is_pause;
   6824  1.16.2.3    martin 	} else {
   6825  1.16.2.3    martin 		rfmt = sc->sc_sound_rparams;
   6826  1.16.2.2  christos 	}
   6827  1.16.2.2  christos 	saved_ai.mode = file->mode;
   6828  1.16.2.2  christos 
   6829  1.16.2.3    martin 	/*
   6830  1.16.2.3    martin 	 * Overwrite if specified.
   6831  1.16.2.3    martin 	 */
   6832  1.16.2.2  christos 	mode = file->mode;
   6833  1.16.2.2  christos 	if (SPECIFIED(ai->mode)) {
   6834  1.16.2.2  christos 		/*
   6835  1.16.2.2  christos 		 * Setting ai->mode no longer does anything because it's
   6836  1.16.2.2  christos 		 * prohibited to change playback/recording mode after open
   6837  1.16.2.2  christos 		 * and AUMODE_PLAY_ALL is obsoleted.  However, it still
   6838  1.16.2.2  christos 		 * keeps the state of AUMODE_PLAY_ALL itself for backward
   6839  1.16.2.2  christos 		 * compatibility.
   6840  1.16.2.2  christos 		 * In the internal, only file->mode has the state of
   6841  1.16.2.2  christos 		 * AUMODE_PLAY_ALL flag and track->mode in both track does
   6842  1.16.2.2  christos 		 * not have.
   6843  1.16.2.2  christos 		 */
   6844  1.16.2.2  christos 		if ((file->mode & AUMODE_PLAY)) {
   6845  1.16.2.2  christos 			mode = (file->mode & (AUMODE_PLAY | AUMODE_RECORD))
   6846  1.16.2.2  christos 			    | (ai->mode & AUMODE_PLAY_ALL);
   6847  1.16.2.2  christos 		}
   6848  1.16.2.2  christos 	}
   6849  1.16.2.2  christos 
   6850  1.16.2.3    martin 	pchanges = audio_track_setinfo_check(ptrack, &pfmt, pi);
   6851  1.16.2.3    martin 	if (pchanges == -1) {
   6852  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   6853  1.16.2.3    martin 		TRACEF(1, file, "check play.params failed: "
   6854  1.16.2.3    martin 		    "%s %ubit %uch %uHz",
   6855  1.16.2.3    martin 		    audio_encoding_name(pi->encoding),
   6856  1.16.2.3    martin 		    pi->precision,
   6857  1.16.2.3    martin 		    pi->channels,
   6858  1.16.2.3    martin 		    pi->sample_rate);
   6859  1.16.2.2  christos #endif
   6860  1.16.2.3    martin 		return EINVAL;
   6861  1.16.2.2  christos 	}
   6862  1.16.2.3    martin 
   6863  1.16.2.3    martin 	rchanges = audio_track_setinfo_check(rtrack, &rfmt, ri);
   6864  1.16.2.3    martin 	if (rchanges == -1) {
   6865  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   6866  1.16.2.3    martin 		TRACEF(1, file, "check record.params failed: "
   6867  1.16.2.3    martin 		    "%s %ubit %uch %uHz",
   6868  1.16.2.3    martin 		    audio_encoding_name(ri->encoding),
   6869  1.16.2.3    martin 		    ri->precision,
   6870  1.16.2.3    martin 		    ri->channels,
   6871  1.16.2.3    martin 		    ri->sample_rate);
   6872  1.16.2.2  christos #endif
   6873  1.16.2.3    martin 		return EINVAL;
   6874  1.16.2.3    martin 	}
   6875  1.16.2.3    martin 
   6876  1.16.2.3    martin 	if (SPECIFIED(ai->mode)) {
   6877  1.16.2.3    martin 		pchanges = 1;
   6878  1.16.2.3    martin 		rchanges = 1;
   6879  1.16.2.2  christos 	}
   6880  1.16.2.2  christos 
   6881  1.16.2.2  christos 	/*
   6882  1.16.2.2  christos 	 * Even when setting either one of playback and recording,
   6883  1.16.2.2  christos 	 * both track must be halted.
   6884  1.16.2.2  christos 	 */
   6885  1.16.2.2  christos 	if (pchanges || rchanges) {
   6886  1.16.2.2  christos 		audio_file_clear(sc, file);
   6887  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   6888  1.16.2.3    martin 		char nbuf[16];
   6889  1.16.2.2  christos 		char fmtbuf[64];
   6890  1.16.2.2  christos 		if (pchanges) {
   6891  1.16.2.3    martin 			if (ptrack) {
   6892  1.16.2.3    martin 				snprintf(nbuf, sizeof(nbuf), "%d", ptrack->id);
   6893  1.16.2.3    martin 			} else {
   6894  1.16.2.3    martin 				snprintf(nbuf, sizeof(nbuf), "-");
   6895  1.16.2.3    martin 			}
   6896  1.16.2.2  christos 			audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &pfmt);
   6897  1.16.2.3    martin 			DPRINTF(1, "audio track#%s play mode: %s\n",
   6898  1.16.2.3    martin 			    nbuf, fmtbuf);
   6899  1.16.2.2  christos 		}
   6900  1.16.2.2  christos 		if (rchanges) {
   6901  1.16.2.3    martin 			if (rtrack) {
   6902  1.16.2.3    martin 				snprintf(nbuf, sizeof(nbuf), "%d", rtrack->id);
   6903  1.16.2.3    martin 			} else {
   6904  1.16.2.3    martin 				snprintf(nbuf, sizeof(nbuf), "-");
   6905  1.16.2.3    martin 			}
   6906  1.16.2.2  christos 			audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &rfmt);
   6907  1.16.2.3    martin 			DPRINTF(1, "audio track#%s rec  mode: %s\n",
   6908  1.16.2.3    martin 			    nbuf, fmtbuf);
   6909  1.16.2.2  christos 		}
   6910  1.16.2.2  christos #endif
   6911  1.16.2.2  christos 	}
   6912  1.16.2.2  christos 
   6913  1.16.2.2  christos 	/* Set mixer parameters */
   6914  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   6915  1.16.2.2  christos 	error = audio_hw_setinfo(sc, ai, &saved_ai);
   6916  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   6917  1.16.2.2  christos 	if (error)
   6918  1.16.2.2  christos 		goto abort1;
   6919  1.16.2.2  christos 
   6920  1.16.2.3    martin 	/*
   6921  1.16.2.3    martin 	 * Set to track and update sticky parameters.
   6922  1.16.2.3    martin 	 */
   6923  1.16.2.2  christos 	error = 0;
   6924  1.16.2.2  christos 	file->mode = mode;
   6925  1.16.2.3    martin 
   6926  1.16.2.3    martin 	if (SPECIFIED_CH(pi->pause)) {
   6927  1.16.2.3    martin 		if (ptrack)
   6928  1.16.2.2  christos 			ptrack->is_pause = pi->pause;
   6929  1.16.2.3    martin 		sc->sc_sound_ppause = pi->pause;
   6930  1.16.2.3    martin 	}
   6931  1.16.2.3    martin 	if (pchanges) {
   6932  1.16.2.3    martin 		if (ptrack) {
   6933  1.16.2.2  christos 			audio_track_lock_enter(ptrack);
   6934  1.16.2.2  christos 			error = audio_track_set_format(ptrack, &pfmt);
   6935  1.16.2.2  christos 			audio_track_lock_exit(ptrack);
   6936  1.16.2.2  christos 			if (error) {
   6937  1.16.2.2  christos 				TRACET(1, ptrack, "set play.params failed");
   6938  1.16.2.2  christos 				goto abort2;
   6939  1.16.2.2  christos 			}
   6940  1.16.2.2  christos 		}
   6941  1.16.2.3    martin 		sc->sc_sound_pparams = pfmt;
   6942  1.16.2.3    martin 	}
   6943  1.16.2.3    martin 	/* Change water marks after initializing the buffers. */
   6944  1.16.2.3    martin 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   6945  1.16.2.3    martin 		if (ptrack)
   6946  1.16.2.2  christos 			audio_track_setinfo_water(ptrack, ai);
   6947  1.16.2.2  christos 	}
   6948  1.16.2.3    martin 
   6949  1.16.2.3    martin 	if (SPECIFIED_CH(ri->pause)) {
   6950  1.16.2.3    martin 		if (rtrack)
   6951  1.16.2.2  christos 			rtrack->is_pause = ri->pause;
   6952  1.16.2.3    martin 		sc->sc_sound_rpause = ri->pause;
   6953  1.16.2.3    martin 	}
   6954  1.16.2.3    martin 	if (rchanges) {
   6955  1.16.2.3    martin 		if (rtrack) {
   6956  1.16.2.2  christos 			audio_track_lock_enter(rtrack);
   6957  1.16.2.2  christos 			error = audio_track_set_format(rtrack, &rfmt);
   6958  1.16.2.2  christos 			audio_track_lock_exit(rtrack);
   6959  1.16.2.2  christos 			if (error) {
   6960  1.16.2.2  christos 				TRACET(1, rtrack, "set record.params failed");
   6961  1.16.2.2  christos 				goto abort3;
   6962  1.16.2.2  christos 			}
   6963  1.16.2.2  christos 		}
   6964  1.16.2.3    martin 		sc->sc_sound_rparams = rfmt;
   6965  1.16.2.2  christos 	}
   6966  1.16.2.2  christos 
   6967  1.16.2.2  christos 	return 0;
   6968  1.16.2.2  christos 
   6969  1.16.2.2  christos 	/* Rollback */
   6970  1.16.2.2  christos abort3:
   6971  1.16.2.2  christos 	if (error != ENOMEM) {
   6972  1.16.2.2  christos 		rtrack->is_pause = saved_ai.record.pause;
   6973  1.16.2.2  christos 		audio_track_lock_enter(rtrack);
   6974  1.16.2.2  christos 		audio_track_set_format(rtrack, &saved_rfmt);
   6975  1.16.2.2  christos 		audio_track_lock_exit(rtrack);
   6976  1.16.2.2  christos 	}
   6977  1.16.2.3    martin 	sc->sc_sound_rpause = saved_ai.record.pause;
   6978  1.16.2.3    martin 	sc->sc_sound_rparams = saved_rfmt;
   6979  1.16.2.2  christos abort2:
   6980  1.16.2.2  christos 	if (ptrack && error != ENOMEM) {
   6981  1.16.2.2  christos 		ptrack->is_pause = saved_ai.play.pause;
   6982  1.16.2.2  christos 		audio_track_lock_enter(ptrack);
   6983  1.16.2.2  christos 		audio_track_set_format(ptrack, &saved_pfmt);
   6984  1.16.2.2  christos 		audio_track_lock_exit(ptrack);
   6985  1.16.2.2  christos 	}
   6986  1.16.2.3    martin 	sc->sc_sound_ppause = saved_ai.play.pause;
   6987  1.16.2.3    martin 	sc->sc_sound_pparams = saved_pfmt;
   6988  1.16.2.2  christos 	file->mode = saved_ai.mode;
   6989  1.16.2.2  christos abort1:
   6990  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   6991  1.16.2.2  christos 	audio_hw_setinfo(sc, &saved_ai, NULL);
   6992  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   6993  1.16.2.2  christos 
   6994  1.16.2.2  christos 	return error;
   6995  1.16.2.2  christos }
   6996  1.16.2.2  christos 
   6997  1.16.2.2  christos /*
   6998  1.16.2.2  christos  * Write SPECIFIED() parameters within info back to fmt.
   6999  1.16.2.3    martin  * Note that track can be NULL here.
   7000  1.16.2.2  christos  * Return value of 1 indicates that fmt is modified.
   7001  1.16.2.2  christos  * Return value of 0 indicates that fmt is not modified.
   7002  1.16.2.2  christos  * Return value of -1 indicates that error EINVAL has occurred.
   7003  1.16.2.2  christos  */
   7004  1.16.2.2  christos static int
   7005  1.16.2.3    martin audio_track_setinfo_check(audio_track_t *track,
   7006  1.16.2.3    martin 	audio_format2_t *fmt, const struct audio_prinfo *info)
   7007  1.16.2.2  christos {
   7008  1.16.2.3    martin 	const audio_format2_t *hwfmt;
   7009  1.16.2.2  christos 	int changes;
   7010  1.16.2.2  christos 
   7011  1.16.2.2  christos 	changes = 0;
   7012  1.16.2.2  christos 	if (SPECIFIED(info->sample_rate)) {
   7013  1.16.2.2  christos 		if (info->sample_rate < AUDIO_MIN_FREQUENCY)
   7014  1.16.2.2  christos 			return -1;
   7015  1.16.2.2  christos 		if (info->sample_rate > AUDIO_MAX_FREQUENCY)
   7016  1.16.2.2  christos 			return -1;
   7017  1.16.2.2  christos 		fmt->sample_rate = info->sample_rate;
   7018  1.16.2.2  christos 		changes = 1;
   7019  1.16.2.2  christos 	}
   7020  1.16.2.2  christos 	if (SPECIFIED(info->encoding)) {
   7021  1.16.2.2  christos 		fmt->encoding = info->encoding;
   7022  1.16.2.2  christos 		changes = 1;
   7023  1.16.2.2  christos 	}
   7024  1.16.2.2  christos 	if (SPECIFIED(info->precision)) {
   7025  1.16.2.2  christos 		fmt->precision = info->precision;
   7026  1.16.2.2  christos 		/* we don't have API to specify stride */
   7027  1.16.2.2  christos 		fmt->stride = info->precision;
   7028  1.16.2.2  christos 		changes = 1;
   7029  1.16.2.2  christos 	}
   7030  1.16.2.2  christos 	if (SPECIFIED(info->channels)) {
   7031  1.16.2.3    martin 		/*
   7032  1.16.2.3    martin 		 * We can convert between monaural and stereo each other.
   7033  1.16.2.3    martin 		 * We can reduce than the number of channels that the hardware
   7034  1.16.2.3    martin 		 * supports.
   7035  1.16.2.3    martin 		 */
   7036  1.16.2.3    martin 		if (info->channels > 2) {
   7037  1.16.2.3    martin 			if (track) {
   7038  1.16.2.3    martin 				hwfmt = &track->mixer->hwbuf.fmt;
   7039  1.16.2.3    martin 				if (info->channels > hwfmt->channels)
   7040  1.16.2.3    martin 					return -1;
   7041  1.16.2.3    martin 			} else {
   7042  1.16.2.3    martin 				/*
   7043  1.16.2.3    martin 				 * This should never happen.
   7044  1.16.2.3    martin 				 * If track == NULL, channels should be <= 2.
   7045  1.16.2.3    martin 				 */
   7046  1.16.2.3    martin 				return -1;
   7047  1.16.2.3    martin 			}
   7048  1.16.2.3    martin 		}
   7049  1.16.2.2  christos 		fmt->channels = info->channels;
   7050  1.16.2.2  christos 		changes = 1;
   7051  1.16.2.2  christos 	}
   7052  1.16.2.2  christos 
   7053  1.16.2.2  christos 	if (changes) {
   7054  1.16.2.2  christos 		if (audio_check_params(fmt) != 0)
   7055  1.16.2.2  christos 			return -1;
   7056  1.16.2.2  christos 	}
   7057  1.16.2.2  christos 
   7058  1.16.2.2  christos 	return changes;
   7059  1.16.2.2  christos }
   7060  1.16.2.2  christos 
   7061  1.16.2.2  christos /*
   7062  1.16.2.2  christos  * Change water marks for playback track if specfied.
   7063  1.16.2.2  christos  */
   7064  1.16.2.2  christos static void
   7065  1.16.2.2  christos audio_track_setinfo_water(audio_track_t *track, const struct audio_info *ai)
   7066  1.16.2.2  christos {
   7067  1.16.2.2  christos 	u_int blks;
   7068  1.16.2.2  christos 	u_int maxblks;
   7069  1.16.2.2  christos 	u_int blksize;
   7070  1.16.2.2  christos 
   7071  1.16.2.2  christos 	KASSERT(audio_track_is_playback(track));
   7072  1.16.2.2  christos 
   7073  1.16.2.2  christos 	blksize = track->usrbuf_blksize;
   7074  1.16.2.2  christos 	maxblks = track->usrbuf.capacity / blksize;
   7075  1.16.2.2  christos 
   7076  1.16.2.2  christos 	if (SPECIFIED(ai->hiwat)) {
   7077  1.16.2.2  christos 		blks = ai->hiwat;
   7078  1.16.2.2  christos 		if (blks > maxblks)
   7079  1.16.2.2  christos 			blks = maxblks;
   7080  1.16.2.2  christos 		if (blks < 2)
   7081  1.16.2.2  christos 			blks = 2;
   7082  1.16.2.2  christos 		track->usrbuf_usedhigh = blks * blksize;
   7083  1.16.2.2  christos 	}
   7084  1.16.2.2  christos 	if (SPECIFIED(ai->lowat)) {
   7085  1.16.2.2  christos 		blks = ai->lowat;
   7086  1.16.2.2  christos 		if (blks > maxblks - 1)
   7087  1.16.2.2  christos 			blks = maxblks - 1;
   7088  1.16.2.2  christos 		track->usrbuf_usedlow = blks * blksize;
   7089  1.16.2.2  christos 	}
   7090  1.16.2.2  christos 	if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
   7091  1.16.2.2  christos 		if (track->usrbuf_usedlow > track->usrbuf_usedhigh - blksize) {
   7092  1.16.2.2  christos 			track->usrbuf_usedlow = track->usrbuf_usedhigh -
   7093  1.16.2.2  christos 			    blksize;
   7094  1.16.2.2  christos 		}
   7095  1.16.2.2  christos 	}
   7096  1.16.2.2  christos }
   7097  1.16.2.2  christos 
   7098  1.16.2.2  christos /*
   7099  1.16.2.3    martin  * Set hardware part of *newai.
   7100  1.16.2.2  christos  * The parameters handled here are *.port, *.gain, *.balance and monitor_gain.
   7101  1.16.2.2  christos  * If oldai is specified, previous parameters are stored.
   7102  1.16.2.2  christos  * This function itself does not roll back if error occurred.
   7103  1.16.2.3    martin  * Must be called with sc_lock && sc_exlock held.
   7104  1.16.2.2  christos  */
   7105  1.16.2.2  christos static int
   7106  1.16.2.2  christos audio_hw_setinfo(struct audio_softc *sc, const struct audio_info *newai,
   7107  1.16.2.2  christos 	struct audio_info *oldai)
   7108  1.16.2.2  christos {
   7109  1.16.2.2  christos 	const struct audio_prinfo *newpi;
   7110  1.16.2.2  christos 	const struct audio_prinfo *newri;
   7111  1.16.2.2  christos 	struct audio_prinfo *oldpi;
   7112  1.16.2.2  christos 	struct audio_prinfo *oldri;
   7113  1.16.2.2  christos 	u_int pgain;
   7114  1.16.2.2  christos 	u_int rgain;
   7115  1.16.2.2  christos 	u_char pbalance;
   7116  1.16.2.2  christos 	u_char rbalance;
   7117  1.16.2.2  christos 	int error;
   7118  1.16.2.2  christos 
   7119  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   7120  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   7121  1.16.2.2  christos 
   7122  1.16.2.2  christos 	/* XXX shut up gcc */
   7123  1.16.2.2  christos 	oldpi = NULL;
   7124  1.16.2.2  christos 	oldri = NULL;
   7125  1.16.2.2  christos 
   7126  1.16.2.2  christos 	newpi = &newai->play;
   7127  1.16.2.2  christos 	newri = &newai->record;
   7128  1.16.2.2  christos 	if (oldai) {
   7129  1.16.2.2  christos 		oldpi = &oldai->play;
   7130  1.16.2.2  christos 		oldri = &oldai->record;
   7131  1.16.2.2  christos 	}
   7132  1.16.2.2  christos 	error = 0;
   7133  1.16.2.2  christos 
   7134  1.16.2.2  christos 	/*
   7135  1.16.2.2  christos 	 * It looks like unnecessary to halt HW mixers to set HW mixers.
   7136  1.16.2.2  christos 	 * mixer_ioctl(MIXER_WRITE) also doesn't halt.
   7137  1.16.2.2  christos 	 */
   7138  1.16.2.2  christos 
   7139  1.16.2.2  christos 	if (SPECIFIED(newpi->port)) {
   7140  1.16.2.2  christos 		if (oldai)
   7141  1.16.2.2  christos 			oldpi->port = au_get_port(sc, &sc->sc_outports);
   7142  1.16.2.2  christos 		error = au_set_port(sc, &sc->sc_outports, newpi->port);
   7143  1.16.2.2  christos 		if (error) {
   7144  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7145  1.16.2.2  christos 			    "setting play.port=%d failed with %d\n",
   7146  1.16.2.2  christos 			    newpi->port, error);
   7147  1.16.2.2  christos 			goto abort;
   7148  1.16.2.2  christos 		}
   7149  1.16.2.2  christos 	}
   7150  1.16.2.2  christos 	if (SPECIFIED(newri->port)) {
   7151  1.16.2.2  christos 		if (oldai)
   7152  1.16.2.2  christos 			oldri->port = au_get_port(sc, &sc->sc_inports);
   7153  1.16.2.2  christos 		error = au_set_port(sc, &sc->sc_inports, newri->port);
   7154  1.16.2.2  christos 		if (error) {
   7155  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7156  1.16.2.2  christos 			    "setting record.port=%d failed with %d\n",
   7157  1.16.2.2  christos 			    newri->port, error);
   7158  1.16.2.2  christos 			goto abort;
   7159  1.16.2.2  christos 		}
   7160  1.16.2.2  christos 	}
   7161  1.16.2.2  christos 
   7162  1.16.2.2  christos 	/* Backup play.{gain,balance} */
   7163  1.16.2.2  christos 	if (SPECIFIED(newpi->gain) || SPECIFIED_CH(newpi->balance)) {
   7164  1.16.2.2  christos 		au_get_gain(sc, &sc->sc_outports, &pgain, &pbalance);
   7165  1.16.2.2  christos 		if (oldai) {
   7166  1.16.2.2  christos 			oldpi->gain = pgain;
   7167  1.16.2.2  christos 			oldpi->balance = pbalance;
   7168  1.16.2.2  christos 		}
   7169  1.16.2.2  christos 	}
   7170  1.16.2.2  christos 	/* Backup record.{gain,balance} */
   7171  1.16.2.2  christos 	if (SPECIFIED(newri->gain) || SPECIFIED_CH(newri->balance)) {
   7172  1.16.2.2  christos 		au_get_gain(sc, &sc->sc_inports, &rgain, &rbalance);
   7173  1.16.2.2  christos 		if (oldai) {
   7174  1.16.2.2  christos 			oldri->gain = rgain;
   7175  1.16.2.2  christos 			oldri->balance = rbalance;
   7176  1.16.2.2  christos 		}
   7177  1.16.2.2  christos 	}
   7178  1.16.2.2  christos 	if (SPECIFIED(newpi->gain)) {
   7179  1.16.2.2  christos 		error = au_set_gain(sc, &sc->sc_outports,
   7180  1.16.2.2  christos 		    newpi->gain, pbalance);
   7181  1.16.2.2  christos 		if (error) {
   7182  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7183  1.16.2.2  christos 			    "setting play.gain=%d failed with %d\n",
   7184  1.16.2.2  christos 			    newpi->gain, error);
   7185  1.16.2.2  christos 			goto abort;
   7186  1.16.2.2  christos 		}
   7187  1.16.2.2  christos 	}
   7188  1.16.2.2  christos 	if (SPECIFIED(newri->gain)) {
   7189  1.16.2.2  christos 		error = au_set_gain(sc, &sc->sc_inports,
   7190  1.16.2.2  christos 		    newri->gain, rbalance);
   7191  1.16.2.2  christos 		if (error) {
   7192  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7193  1.16.2.2  christos 			    "setting record.gain=%d failed with %d\n",
   7194  1.16.2.2  christos 			    newri->gain, error);
   7195  1.16.2.2  christos 			goto abort;
   7196  1.16.2.2  christos 		}
   7197  1.16.2.2  christos 	}
   7198  1.16.2.2  christos 	if (SPECIFIED_CH(newpi->balance)) {
   7199  1.16.2.2  christos 		error = au_set_gain(sc, &sc->sc_outports,
   7200  1.16.2.2  christos 		    pgain, newpi->balance);
   7201  1.16.2.2  christos 		if (error) {
   7202  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7203  1.16.2.2  christos 			    "setting play.balance=%d failed with %d\n",
   7204  1.16.2.2  christos 			    newpi->balance, error);
   7205  1.16.2.2  christos 			goto abort;
   7206  1.16.2.2  christos 		}
   7207  1.16.2.2  christos 	}
   7208  1.16.2.2  christos 	if (SPECIFIED_CH(newri->balance)) {
   7209  1.16.2.2  christos 		error = au_set_gain(sc, &sc->sc_inports,
   7210  1.16.2.2  christos 		    rgain, newri->balance);
   7211  1.16.2.2  christos 		if (error) {
   7212  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7213  1.16.2.2  christos 			    "setting record.balance=%d failed with %d\n",
   7214  1.16.2.2  christos 			    newri->balance, error);
   7215  1.16.2.2  christos 			goto abort;
   7216  1.16.2.2  christos 		}
   7217  1.16.2.2  christos 	}
   7218  1.16.2.2  christos 
   7219  1.16.2.2  christos 	if (SPECIFIED(newai->monitor_gain) && sc->sc_monitor_port != -1) {
   7220  1.16.2.2  christos 		if (oldai)
   7221  1.16.2.2  christos 			oldai->monitor_gain = au_get_monitor_gain(sc);
   7222  1.16.2.2  christos 		error = au_set_monitor_gain(sc, newai->monitor_gain);
   7223  1.16.2.2  christos 		if (error) {
   7224  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7225  1.16.2.2  christos 			    "setting monitor_gain=%d failed with %d\n",
   7226  1.16.2.2  christos 			    newai->monitor_gain, error);
   7227  1.16.2.2  christos 			goto abort;
   7228  1.16.2.2  christos 		}
   7229  1.16.2.2  christos 	}
   7230  1.16.2.2  christos 
   7231  1.16.2.2  christos 	/* XXX TODO */
   7232  1.16.2.2  christos 	/* sc->sc_ai = *ai; */
   7233  1.16.2.2  christos 
   7234  1.16.2.2  christos 	error = 0;
   7235  1.16.2.2  christos abort:
   7236  1.16.2.2  christos 	return error;
   7237  1.16.2.2  christos }
   7238  1.16.2.2  christos 
   7239  1.16.2.2  christos /*
   7240  1.16.2.2  christos  * Setup the hardware with mixer format phwfmt, rhwfmt.
   7241  1.16.2.2  christos  * The arguments have following restrictions:
   7242  1.16.2.2  christos  * - setmode is the direction you want to set, AUMODE_PLAY or AUMODE_RECORD,
   7243  1.16.2.2  christos  *   or both.
   7244  1.16.2.2  christos  * - phwfmt and rhwfmt must not be NULL regardless of setmode.
   7245  1.16.2.2  christos  * - On non-independent devices, phwfmt and rhwfmt must have the same
   7246  1.16.2.2  christos  *   parameters.
   7247  1.16.2.2  christos  * - pfil and rfil must be zero-filled.
   7248  1.16.2.2  christos  * If successful,
   7249  1.16.2.2  christos  * - pfil, rfil will be filled with filter information specified by the
   7250  1.16.2.2  christos  *   hardware driver.
   7251  1.16.2.2  christos  * and then returns 0.  Otherwise returns errno.
   7252  1.16.2.3    martin  * Must be called without sc_lock held.
   7253  1.16.2.2  christos  */
   7254  1.16.2.2  christos static int
   7255  1.16.2.2  christos audio_hw_set_format(struct audio_softc *sc, int setmode,
   7256  1.16.2.3    martin 	const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
   7257  1.16.2.2  christos 	audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
   7258  1.16.2.2  christos {
   7259  1.16.2.2  christos 	audio_params_t pp, rp;
   7260  1.16.2.2  christos 	int error;
   7261  1.16.2.2  christos 
   7262  1.16.2.2  christos 	KASSERT(phwfmt != NULL);
   7263  1.16.2.2  christos 	KASSERT(rhwfmt != NULL);
   7264  1.16.2.2  christos 
   7265  1.16.2.2  christos 	pp = format2_to_params(phwfmt);
   7266  1.16.2.2  christos 	rp = format2_to_params(rhwfmt);
   7267  1.16.2.2  christos 
   7268  1.16.2.3    martin 	mutex_enter(sc->sc_lock);
   7269  1.16.2.2  christos 	error = sc->hw_if->set_format(sc->hw_hdl, setmode,
   7270  1.16.2.2  christos 	    &pp, &rp, pfil, rfil);
   7271  1.16.2.2  christos 	if (error) {
   7272  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   7273  1.16.2.2  christos 		device_printf(sc->sc_dev,
   7274  1.16.2.2  christos 		    "set_format failed with %d\n", error);
   7275  1.16.2.2  christos 		return error;
   7276  1.16.2.2  christos 	}
   7277  1.16.2.2  christos 
   7278  1.16.2.2  christos 	if (sc->hw_if->commit_settings) {
   7279  1.16.2.2  christos 		error = sc->hw_if->commit_settings(sc->hw_hdl);
   7280  1.16.2.2  christos 		if (error) {
   7281  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   7282  1.16.2.2  christos 			device_printf(sc->sc_dev,
   7283  1.16.2.2  christos 			    "commit_settings failed with %d\n", error);
   7284  1.16.2.2  christos 			return error;
   7285  1.16.2.2  christos 		}
   7286  1.16.2.2  christos 	}
   7287  1.16.2.3    martin 	mutex_exit(sc->sc_lock);
   7288  1.16.2.2  christos 
   7289  1.16.2.2  christos 	return 0;
   7290  1.16.2.2  christos }
   7291  1.16.2.2  christos 
   7292  1.16.2.2  christos /*
   7293  1.16.2.2  christos  * Fill audio_info structure.  If need_mixerinfo is true, it will also
   7294  1.16.2.2  christos  * fill the hardware mixer information.
   7295  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   7296  1.16.2.2  christos  */
   7297  1.16.2.2  christos static int
   7298  1.16.2.2  christos audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int need_mixerinfo,
   7299  1.16.2.2  christos 	audio_file_t *file)
   7300  1.16.2.2  christos {
   7301  1.16.2.2  christos 	struct audio_prinfo *ri, *pi;
   7302  1.16.2.2  christos 	audio_track_t *track;
   7303  1.16.2.2  christos 	audio_track_t *ptrack;
   7304  1.16.2.2  christos 	audio_track_t *rtrack;
   7305  1.16.2.2  christos 	int gain;
   7306  1.16.2.2  christos 
   7307  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   7308  1.16.2.2  christos 
   7309  1.16.2.2  christos 	ri = &ai->record;
   7310  1.16.2.2  christos 	pi = &ai->play;
   7311  1.16.2.2  christos 	ptrack = file->ptrack;
   7312  1.16.2.2  christos 	rtrack = file->rtrack;
   7313  1.16.2.2  christos 
   7314  1.16.2.2  christos 	memset(ai, 0, sizeof(*ai));
   7315  1.16.2.2  christos 
   7316  1.16.2.2  christos 	if (ptrack) {
   7317  1.16.2.2  christos 		pi->sample_rate = ptrack->usrbuf.fmt.sample_rate;
   7318  1.16.2.2  christos 		pi->channels    = ptrack->usrbuf.fmt.channels;
   7319  1.16.2.2  christos 		pi->precision   = ptrack->usrbuf.fmt.precision;
   7320  1.16.2.2  christos 		pi->encoding    = ptrack->usrbuf.fmt.encoding;
   7321  1.16.2.3    martin 		pi->pause       = ptrack->is_pause;
   7322  1.16.2.2  christos 	} else {
   7323  1.16.2.3    martin 		/* Use sticky parameters if the track is not available. */
   7324  1.16.2.3    martin 		pi->sample_rate = sc->sc_sound_pparams.sample_rate;
   7325  1.16.2.3    martin 		pi->channels    = sc->sc_sound_pparams.channels;
   7326  1.16.2.3    martin 		pi->precision   = sc->sc_sound_pparams.precision;
   7327  1.16.2.3    martin 		pi->encoding    = sc->sc_sound_pparams.encoding;
   7328  1.16.2.3    martin 		pi->pause       = sc->sc_sound_ppause;
   7329  1.16.2.2  christos 	}
   7330  1.16.2.2  christos 	if (rtrack) {
   7331  1.16.2.2  christos 		ri->sample_rate = rtrack->usrbuf.fmt.sample_rate;
   7332  1.16.2.2  christos 		ri->channels    = rtrack->usrbuf.fmt.channels;
   7333  1.16.2.2  christos 		ri->precision   = rtrack->usrbuf.fmt.precision;
   7334  1.16.2.2  christos 		ri->encoding    = rtrack->usrbuf.fmt.encoding;
   7335  1.16.2.3    martin 		ri->pause       = rtrack->is_pause;
   7336  1.16.2.2  christos 	} else {
   7337  1.16.2.3    martin 		/* Use sticky parameters if the track is not available. */
   7338  1.16.2.3    martin 		ri->sample_rate = sc->sc_sound_rparams.sample_rate;
   7339  1.16.2.3    martin 		ri->channels    = sc->sc_sound_rparams.channels;
   7340  1.16.2.3    martin 		ri->precision   = sc->sc_sound_rparams.precision;
   7341  1.16.2.3    martin 		ri->encoding    = sc->sc_sound_rparams.encoding;
   7342  1.16.2.3    martin 		ri->pause       = sc->sc_sound_rpause;
   7343  1.16.2.2  christos 	}
   7344  1.16.2.2  christos 
   7345  1.16.2.2  christos 	if (ptrack) {
   7346  1.16.2.2  christos 		pi->seek = ptrack->usrbuf.used;
   7347  1.16.2.2  christos 		pi->samples = ptrack->usrbuf_stamp;
   7348  1.16.2.2  christos 		pi->eof = ptrack->eofcounter;
   7349  1.16.2.2  christos 		pi->error = (ptrack->dropframes != 0) ? 1 : 0;
   7350  1.16.2.2  christos 		pi->open = 1;
   7351  1.16.2.2  christos 		pi->buffer_size = ptrack->usrbuf.capacity;
   7352  1.16.2.2  christos 	}
   7353  1.16.2.3    martin 	pi->waiting = 0;		/* open never hangs */
   7354  1.16.2.3    martin 	pi->active = sc->sc_pbusy;
   7355  1.16.2.3    martin 
   7356  1.16.2.2  christos 	if (rtrack) {
   7357  1.16.2.2  christos 		ri->seek = rtrack->usrbuf.used;
   7358  1.16.2.2  christos 		ri->samples = rtrack->usrbuf_stamp;
   7359  1.16.2.2  christos 		ri->eof = 0;
   7360  1.16.2.2  christos 		ri->error = (rtrack->dropframes != 0) ? 1 : 0;
   7361  1.16.2.2  christos 		ri->open = 1;
   7362  1.16.2.2  christos 		ri->buffer_size = rtrack->usrbuf.capacity;
   7363  1.16.2.2  christos 	}
   7364  1.16.2.3    martin 	ri->waiting = 0;		/* open never hangs */
   7365  1.16.2.3    martin 	ri->active = sc->sc_rbusy;
   7366  1.16.2.2  christos 
   7367  1.16.2.2  christos 	/*
   7368  1.16.2.2  christos 	 * XXX There may be different number of channels between playback
   7369  1.16.2.2  christos 	 *     and recording, so that blocksize also may be different.
   7370  1.16.2.2  christos 	 *     But struct audio_info has an united blocksize...
   7371  1.16.2.2  christos 	 *     Here, I use play info precedencely if ptrack is available,
   7372  1.16.2.2  christos 	 *     otherwise record info.
   7373  1.16.2.2  christos 	 *
   7374  1.16.2.2  christos 	 * XXX hiwat/lowat is a playback-only parameter.  What should I
   7375  1.16.2.2  christos 	 *     return for a record-only descriptor?
   7376  1.16.2.2  christos 	 */
   7377  1.16.2.2  christos 	track = ptrack ? ptrack : rtrack;
   7378  1.16.2.2  christos 	if (track) {
   7379  1.16.2.2  christos 		ai->blocksize = track->usrbuf_blksize;
   7380  1.16.2.2  christos 		ai->hiwat = track->usrbuf_usedhigh / track->usrbuf_blksize;
   7381  1.16.2.2  christos 		ai->lowat = track->usrbuf_usedlow / track->usrbuf_blksize;
   7382  1.16.2.2  christos 	}
   7383  1.16.2.2  christos 	ai->mode = file->mode;
   7384  1.16.2.2  christos 
   7385  1.16.2.3    martin 	/*
   7386  1.16.2.3    martin 	 * For backward compatibility, we have to pad these five fields
   7387  1.16.2.3    martin 	 * a fake non-zero value even if there are no tracks.
   7388  1.16.2.3    martin 	 */
   7389  1.16.2.3    martin 	if (ptrack == NULL)
   7390  1.16.2.3    martin 		pi->buffer_size = 65536;
   7391  1.16.2.3    martin 	if (rtrack == NULL)
   7392  1.16.2.3    martin 		ri->buffer_size = 65536;
   7393  1.16.2.3    martin 	if (ptrack == NULL && rtrack == NULL) {
   7394  1.16.2.3    martin 		ai->blocksize = 2048;
   7395  1.16.2.3    martin 		ai->hiwat = ai->play.buffer_size / ai->blocksize;
   7396  1.16.2.3    martin 		ai->lowat = ai->hiwat * 3 / 4;
   7397  1.16.2.3    martin 	}
   7398  1.16.2.3    martin 
   7399  1.16.2.2  christos 	if (need_mixerinfo) {
   7400  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   7401  1.16.2.2  christos 
   7402  1.16.2.2  christos 		pi->port = au_get_port(sc, &sc->sc_outports);
   7403  1.16.2.2  christos 		ri->port = au_get_port(sc, &sc->sc_inports);
   7404  1.16.2.2  christos 
   7405  1.16.2.2  christos 		pi->avail_ports = sc->sc_outports.allports;
   7406  1.16.2.2  christos 		ri->avail_ports = sc->sc_inports.allports;
   7407  1.16.2.2  christos 
   7408  1.16.2.2  christos 		au_get_gain(sc, &sc->sc_outports, &pi->gain, &pi->balance);
   7409  1.16.2.2  christos 		au_get_gain(sc, &sc->sc_inports, &ri->gain, &ri->balance);
   7410  1.16.2.2  christos 
   7411  1.16.2.2  christos 		if (sc->sc_monitor_port != -1) {
   7412  1.16.2.2  christos 			gain = au_get_monitor_gain(sc);
   7413  1.16.2.2  christos 			if (gain != -1)
   7414  1.16.2.2  christos 				ai->monitor_gain = gain;
   7415  1.16.2.2  christos 		}
   7416  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   7417  1.16.2.2  christos 	}
   7418  1.16.2.2  christos 
   7419  1.16.2.2  christos 	return 0;
   7420  1.16.2.2  christos }
   7421  1.16.2.2  christos 
   7422  1.16.2.2  christos /*
   7423  1.16.2.2  christos  * Return true if playback is configured.
   7424  1.16.2.2  christos  * This function can be used after audioattach.
   7425  1.16.2.2  christos  */
   7426  1.16.2.2  christos static bool
   7427  1.16.2.2  christos audio_can_playback(struct audio_softc *sc)
   7428  1.16.2.2  christos {
   7429  1.16.2.2  christos 
   7430  1.16.2.2  christos 	return (sc->sc_pmixer != NULL);
   7431  1.16.2.2  christos }
   7432  1.16.2.2  christos 
   7433  1.16.2.2  christos /*
   7434  1.16.2.2  christos  * Return true if recording is configured.
   7435  1.16.2.2  christos  * This function can be used after audioattach.
   7436  1.16.2.2  christos  */
   7437  1.16.2.2  christos static bool
   7438  1.16.2.2  christos audio_can_capture(struct audio_softc *sc)
   7439  1.16.2.2  christos {
   7440  1.16.2.2  christos 
   7441  1.16.2.2  christos 	return (sc->sc_rmixer != NULL);
   7442  1.16.2.2  christos }
   7443  1.16.2.2  christos 
   7444  1.16.2.2  christos /*
   7445  1.16.2.2  christos  * Get the afp->index'th item from the valid one of format[].
   7446  1.16.2.2  christos  * If found, stores it to afp->fmt and returns 0.  Otherwise return EINVAL.
   7447  1.16.2.2  christos  *
   7448  1.16.2.2  christos  * This is common routines for query_format.
   7449  1.16.2.2  christos  * If your hardware driver has struct audio_format[], the simplest case
   7450  1.16.2.2  christos  * you can write your query_format interface as follows:
   7451  1.16.2.2  christos  *
   7452  1.16.2.2  christos  * struct audio_format foo_format[] = { ... };
   7453  1.16.2.2  christos  *
   7454  1.16.2.2  christos  * int
   7455  1.16.2.2  christos  * foo_query_format(void *hdl, audio_format_query_t *afp)
   7456  1.16.2.2  christos  * {
   7457  1.16.2.2  christos  *   return audio_query_format(foo_format, __arraycount(foo_format), afp);
   7458  1.16.2.2  christos  * }
   7459  1.16.2.2  christos  */
   7460  1.16.2.2  christos int
   7461  1.16.2.2  christos audio_query_format(const struct audio_format *format, int nformats,
   7462  1.16.2.2  christos 	audio_format_query_t *afp)
   7463  1.16.2.2  christos {
   7464  1.16.2.2  christos 	const struct audio_format *f;
   7465  1.16.2.2  christos 	int idx;
   7466  1.16.2.2  christos 	int i;
   7467  1.16.2.2  christos 
   7468  1.16.2.2  christos 	idx = 0;
   7469  1.16.2.2  christos 	for (i = 0; i < nformats; i++) {
   7470  1.16.2.2  christos 		f = &format[i];
   7471  1.16.2.2  christos 		if (!AUFMT_IS_VALID(f))
   7472  1.16.2.2  christos 			continue;
   7473  1.16.2.2  christos 		if (afp->index == idx) {
   7474  1.16.2.2  christos 			afp->fmt = *f;
   7475  1.16.2.2  christos 			return 0;
   7476  1.16.2.2  christos 		}
   7477  1.16.2.2  christos 		idx++;
   7478  1.16.2.2  christos 	}
   7479  1.16.2.2  christos 	return EINVAL;
   7480  1.16.2.2  christos }
   7481  1.16.2.2  christos 
   7482  1.16.2.2  christos /*
   7483  1.16.2.2  christos  * This function is provided for the hardware driver's set_format() to
   7484  1.16.2.2  christos  * find index matches with 'param' from array of audio_format_t 'formats'.
   7485  1.16.2.2  christos  * 'mode' is either of AUMODE_PLAY or AUMODE_RECORD.
   7486  1.16.2.2  christos  * It returns the matched index and never fails.  Because param passed to
   7487  1.16.2.2  christos  * set_format() is selected from query_format().
   7488  1.16.2.2  christos  * This function will be an alternative to auconv_set_converter() to
   7489  1.16.2.2  christos  * find index.
   7490  1.16.2.2  christos  */
   7491  1.16.2.2  christos int
   7492  1.16.2.2  christos audio_indexof_format(const struct audio_format *formats, int nformats,
   7493  1.16.2.2  christos 	int mode, const audio_params_t *param)
   7494  1.16.2.2  christos {
   7495  1.16.2.2  christos 	const struct audio_format *f;
   7496  1.16.2.2  christos 	int index;
   7497  1.16.2.2  christos 	int j;
   7498  1.16.2.2  christos 
   7499  1.16.2.2  christos 	for (index = 0; index < nformats; index++) {
   7500  1.16.2.2  christos 		f = &formats[index];
   7501  1.16.2.2  christos 
   7502  1.16.2.2  christos 		if (!AUFMT_IS_VALID(f))
   7503  1.16.2.2  christos 			continue;
   7504  1.16.2.2  christos 		if ((f->mode & mode) == 0)
   7505  1.16.2.2  christos 			continue;
   7506  1.16.2.2  christos 		if (f->encoding != param->encoding)
   7507  1.16.2.2  christos 			continue;
   7508  1.16.2.2  christos 		if (f->validbits != param->precision)
   7509  1.16.2.2  christos 			continue;
   7510  1.16.2.2  christos 		if (f->channels != param->channels)
   7511  1.16.2.2  christos 			continue;
   7512  1.16.2.2  christos 
   7513  1.16.2.2  christos 		if (f->frequency_type == 0) {
   7514  1.16.2.2  christos 			if (param->sample_rate < f->frequency[0] ||
   7515  1.16.2.2  christos 			    param->sample_rate > f->frequency[1])
   7516  1.16.2.2  christos 				continue;
   7517  1.16.2.2  christos 		} else {
   7518  1.16.2.2  christos 			for (j = 0; j < f->frequency_type; j++) {
   7519  1.16.2.2  christos 				if (param->sample_rate == f->frequency[j])
   7520  1.16.2.2  christos 					break;
   7521  1.16.2.2  christos 			}
   7522  1.16.2.2  christos 			if (j == f->frequency_type)
   7523  1.16.2.2  christos 				continue;
   7524  1.16.2.2  christos 		}
   7525  1.16.2.2  christos 
   7526  1.16.2.2  christos 		/* Then, matched */
   7527  1.16.2.2  christos 		return index;
   7528  1.16.2.2  christos 	}
   7529  1.16.2.2  christos 
   7530  1.16.2.2  christos 	/* Not matched.  This should not be happened. */
   7531  1.16.2.2  christos 	panic("%s: cannot find matched format\n", __func__);
   7532  1.16.2.2  christos }
   7533  1.16.2.2  christos 
   7534  1.16.2.2  christos /*
   7535  1.16.2.2  christos  * Get or set hardware blocksize in msec.
   7536  1.16.2.2  christos  * XXX It's for debug.
   7537  1.16.2.2  christos  */
   7538  1.16.2.2  christos static int
   7539  1.16.2.2  christos audio_sysctl_blk_ms(SYSCTLFN_ARGS)
   7540  1.16.2.2  christos {
   7541  1.16.2.2  christos 	struct sysctlnode node;
   7542  1.16.2.2  christos 	struct audio_softc *sc;
   7543  1.16.2.2  christos 	audio_format2_t phwfmt;
   7544  1.16.2.2  christos 	audio_format2_t rhwfmt;
   7545  1.16.2.2  christos 	audio_filter_reg_t pfil;
   7546  1.16.2.2  christos 	audio_filter_reg_t rfil;
   7547  1.16.2.2  christos 	int t;
   7548  1.16.2.2  christos 	int old_blk_ms;
   7549  1.16.2.2  christos 	int mode;
   7550  1.16.2.2  christos 	int error;
   7551  1.16.2.2  christos 
   7552  1.16.2.2  christos 	node = *rnode;
   7553  1.16.2.2  christos 	sc = node.sysctl_data;
   7554  1.16.2.2  christos 
   7555  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   7556  1.16.2.3    martin 	if (error)
   7557  1.16.2.3    martin 		return error;
   7558  1.16.2.2  christos 
   7559  1.16.2.2  christos 	old_blk_ms = sc->sc_blk_ms;
   7560  1.16.2.2  christos 	t = old_blk_ms;
   7561  1.16.2.2  christos 	node.sysctl_data = &t;
   7562  1.16.2.2  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   7563  1.16.2.2  christos 	if (error || newp == NULL)
   7564  1.16.2.2  christos 		goto abort;
   7565  1.16.2.2  christos 
   7566  1.16.2.2  christos 	if (t < 0) {
   7567  1.16.2.2  christos 		error = EINVAL;
   7568  1.16.2.2  christos 		goto abort;
   7569  1.16.2.2  christos 	}
   7570  1.16.2.2  christos 
   7571  1.16.2.2  christos 	if (sc->sc_popens + sc->sc_ropens > 0) {
   7572  1.16.2.2  christos 		error = EBUSY;
   7573  1.16.2.2  christos 		goto abort;
   7574  1.16.2.2  christos 	}
   7575  1.16.2.2  christos 	sc->sc_blk_ms = t;
   7576  1.16.2.2  christos 	mode = 0;
   7577  1.16.2.2  christos 	if (sc->sc_pmixer) {
   7578  1.16.2.2  christos 		mode |= AUMODE_PLAY;
   7579  1.16.2.2  christos 		phwfmt = sc->sc_pmixer->hwbuf.fmt;
   7580  1.16.2.2  christos 	}
   7581  1.16.2.2  christos 	if (sc->sc_rmixer) {
   7582  1.16.2.2  christos 		mode |= AUMODE_RECORD;
   7583  1.16.2.2  christos 		rhwfmt = sc->sc_rmixer->hwbuf.fmt;
   7584  1.16.2.2  christos 	}
   7585  1.16.2.2  christos 
   7586  1.16.2.2  christos 	/* re-init hardware */
   7587  1.16.2.2  christos 	memset(&pfil, 0, sizeof(pfil));
   7588  1.16.2.2  christos 	memset(&rfil, 0, sizeof(rfil));
   7589  1.16.2.2  christos 	error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   7590  1.16.2.2  christos 	if (error) {
   7591  1.16.2.2  christos 		goto abort;
   7592  1.16.2.2  christos 	}
   7593  1.16.2.2  christos 
   7594  1.16.2.2  christos 	/* re-init track mixer */
   7595  1.16.2.2  christos 	error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   7596  1.16.2.2  christos 	if (error) {
   7597  1.16.2.2  christos 		/* Rollback */
   7598  1.16.2.2  christos 		sc->sc_blk_ms = old_blk_ms;
   7599  1.16.2.2  christos 		audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
   7600  1.16.2.2  christos 		goto abort;
   7601  1.16.2.2  christos 	}
   7602  1.16.2.2  christos 	error = 0;
   7603  1.16.2.2  christos abort:
   7604  1.16.2.3    martin 	audio_exlock_exit(sc);
   7605  1.16.2.2  christos 	return error;
   7606  1.16.2.2  christos }
   7607  1.16.2.2  christos 
   7608  1.16.2.2  christos /*
   7609  1.16.2.2  christos  * Get or set multiuser mode.
   7610  1.16.2.2  christos  */
   7611  1.16.2.2  christos static int
   7612  1.16.2.2  christos audio_sysctl_multiuser(SYSCTLFN_ARGS)
   7613  1.16.2.2  christos {
   7614  1.16.2.2  christos 	struct sysctlnode node;
   7615  1.16.2.2  christos 	struct audio_softc *sc;
   7616  1.16.2.2  christos 	bool t;
   7617  1.16.2.2  christos 	int error;
   7618  1.16.2.2  christos 
   7619  1.16.2.2  christos 	node = *rnode;
   7620  1.16.2.2  christos 	sc = node.sysctl_data;
   7621  1.16.2.2  christos 
   7622  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   7623  1.16.2.3    martin 	if (error)
   7624  1.16.2.3    martin 		return error;
   7625  1.16.2.2  christos 
   7626  1.16.2.2  christos 	t = sc->sc_multiuser;
   7627  1.16.2.2  christos 	node.sysctl_data = &t;
   7628  1.16.2.2  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   7629  1.16.2.2  christos 	if (error || newp == NULL)
   7630  1.16.2.2  christos 		goto abort;
   7631  1.16.2.2  christos 
   7632  1.16.2.2  christos 	sc->sc_multiuser = t;
   7633  1.16.2.2  christos 	error = 0;
   7634  1.16.2.2  christos abort:
   7635  1.16.2.3    martin 	audio_exlock_exit(sc);
   7636  1.16.2.2  christos 	return error;
   7637  1.16.2.2  christos }
   7638  1.16.2.2  christos 
   7639  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   7640  1.16.2.2  christos /*
   7641  1.16.2.2  christos  * Get or set debug verbose level. (0..4)
   7642  1.16.2.2  christos  * XXX It's for debug.
   7643  1.16.2.2  christos  * XXX It is not separated per device.
   7644  1.16.2.2  christos  */
   7645  1.16.2.2  christos static int
   7646  1.16.2.2  christos audio_sysctl_debug(SYSCTLFN_ARGS)
   7647  1.16.2.2  christos {
   7648  1.16.2.2  christos 	struct sysctlnode node;
   7649  1.16.2.2  christos 	int t;
   7650  1.16.2.2  christos 	int error;
   7651  1.16.2.2  christos 
   7652  1.16.2.2  christos 	node = *rnode;
   7653  1.16.2.2  christos 	t = audiodebug;
   7654  1.16.2.2  christos 	node.sysctl_data = &t;
   7655  1.16.2.2  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   7656  1.16.2.2  christos 	if (error || newp == NULL)
   7657  1.16.2.2  christos 		return error;
   7658  1.16.2.2  christos 
   7659  1.16.2.2  christos 	if (t < 0 || t > 4)
   7660  1.16.2.2  christos 		return EINVAL;
   7661  1.16.2.2  christos 	audiodebug = t;
   7662  1.16.2.2  christos 	printf("audio: audiodebug = %d\n", audiodebug);
   7663  1.16.2.2  christos 	return 0;
   7664  1.16.2.2  christos }
   7665  1.16.2.2  christos #endif /* AUDIO_DEBUG */
   7666  1.16.2.2  christos 
   7667  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   7668  1.16.2.2  christos static void
   7669  1.16.2.2  christos audio_idle(void *arg)
   7670  1.16.2.2  christos {
   7671  1.16.2.2  christos 	device_t dv = arg;
   7672  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   7673  1.16.2.2  christos 
   7674  1.16.2.2  christos #ifdef PNP_DEBUG
   7675  1.16.2.2  christos 	extern int pnp_debug_idle;
   7676  1.16.2.2  christos 	if (pnp_debug_idle)
   7677  1.16.2.2  christos 		printf("%s: idle handler called\n", device_xname(dv));
   7678  1.16.2.2  christos #endif
   7679  1.16.2.2  christos 
   7680  1.16.2.2  christos 	sc->sc_idle = true;
   7681  1.16.2.2  christos 
   7682  1.16.2.2  christos 	/* XXX joerg Make pmf_device_suspend handle children? */
   7683  1.16.2.2  christos 	if (!pmf_device_suspend(dv, PMF_Q_SELF))
   7684  1.16.2.2  christos 		return;
   7685  1.16.2.2  christos 
   7686  1.16.2.2  christos 	if (!pmf_device_suspend(sc->hw_dev, PMF_Q_SELF))
   7687  1.16.2.2  christos 		pmf_device_resume(dv, PMF_Q_SELF);
   7688  1.16.2.2  christos }
   7689  1.16.2.2  christos 
   7690  1.16.2.2  christos static void
   7691  1.16.2.2  christos audio_activity(device_t dv, devactive_t type)
   7692  1.16.2.2  christos {
   7693  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   7694  1.16.2.2  christos 
   7695  1.16.2.2  christos 	if (type != DVA_SYSTEM)
   7696  1.16.2.2  christos 		return;
   7697  1.16.2.2  christos 
   7698  1.16.2.2  christos 	callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
   7699  1.16.2.2  christos 
   7700  1.16.2.2  christos 	sc->sc_idle = false;
   7701  1.16.2.2  christos 	if (!device_is_active(dv)) {
   7702  1.16.2.2  christos 		/* XXX joerg How to deal with a failing resume... */
   7703  1.16.2.2  christos 		pmf_device_resume(sc->hw_dev, PMF_Q_SELF);
   7704  1.16.2.2  christos 		pmf_device_resume(dv, PMF_Q_SELF);
   7705  1.16.2.2  christos 	}
   7706  1.16.2.2  christos }
   7707  1.16.2.2  christos #endif
   7708  1.16.2.2  christos 
   7709  1.16.2.2  christos static bool
   7710  1.16.2.2  christos audio_suspend(device_t dv, const pmf_qual_t *qual)
   7711  1.16.2.2  christos {
   7712  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   7713  1.16.2.2  christos 	int error;
   7714  1.16.2.2  christos 
   7715  1.16.2.3    martin 	error = audio_exlock_mutex_enter(sc);
   7716  1.16.2.2  christos 	if (error)
   7717  1.16.2.2  christos 		return error;
   7718  1.16.2.2  christos 	audio_mixer_capture(sc);
   7719  1.16.2.2  christos 
   7720  1.16.2.2  christos 	/* Halts mixers but don't clear busy flag for resume */
   7721  1.16.2.2  christos 	if (sc->sc_pbusy) {
   7722  1.16.2.2  christos 		audio_pmixer_halt(sc);
   7723  1.16.2.2  christos 		sc->sc_pbusy = true;
   7724  1.16.2.2  christos 	}
   7725  1.16.2.2  christos 	if (sc->sc_rbusy) {
   7726  1.16.2.2  christos 		audio_rmixer_halt(sc);
   7727  1.16.2.2  christos 		sc->sc_rbusy = true;
   7728  1.16.2.2  christos 	}
   7729  1.16.2.2  christos 
   7730  1.16.2.2  christos #ifdef AUDIO_PM_IDLE
   7731  1.16.2.2  christos 	callout_halt(&sc->sc_idle_counter, sc->sc_lock);
   7732  1.16.2.2  christos #endif
   7733  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   7734  1.16.2.2  christos 
   7735  1.16.2.2  christos 	return true;
   7736  1.16.2.2  christos }
   7737  1.16.2.2  christos 
   7738  1.16.2.2  christos static bool
   7739  1.16.2.2  christos audio_resume(device_t dv, const pmf_qual_t *qual)
   7740  1.16.2.2  christos {
   7741  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   7742  1.16.2.2  christos 	struct audio_info ai;
   7743  1.16.2.2  christos 	int error;
   7744  1.16.2.2  christos 
   7745  1.16.2.3    martin 	error = audio_exlock_mutex_enter(sc);
   7746  1.16.2.2  christos 	if (error)
   7747  1.16.2.2  christos 		return error;
   7748  1.16.2.2  christos 
   7749  1.16.2.2  christos 	audio_mixer_restore(sc);
   7750  1.16.2.2  christos 	/* XXX ? */
   7751  1.16.2.2  christos 	AUDIO_INITINFO(&ai);
   7752  1.16.2.2  christos 	audio_hw_setinfo(sc, &ai, NULL);
   7753  1.16.2.2  christos 
   7754  1.16.2.2  christos 	if (sc->sc_pbusy)
   7755  1.16.2.2  christos 		audio_pmixer_start(sc, true);
   7756  1.16.2.2  christos 	if (sc->sc_rbusy)
   7757  1.16.2.2  christos 		audio_rmixer_start(sc);
   7758  1.16.2.2  christos 
   7759  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   7760  1.16.2.2  christos 
   7761  1.16.2.2  christos 	return true;
   7762  1.16.2.2  christos }
   7763  1.16.2.2  christos 
   7764  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   7765  1.16.2.2  christos static void
   7766  1.16.2.2  christos audio_format2_tostr(char *buf, size_t bufsize, const audio_format2_t *fmt)
   7767  1.16.2.2  christos {
   7768  1.16.2.2  christos 	int n;
   7769  1.16.2.2  christos 
   7770  1.16.2.2  christos 	n = 0;
   7771  1.16.2.2  christos 	n += snprintf(buf + n, bufsize - n, "%s",
   7772  1.16.2.2  christos 	    audio_encoding_name(fmt->encoding));
   7773  1.16.2.2  christos 	if (fmt->precision == fmt->stride) {
   7774  1.16.2.2  christos 		n += snprintf(buf + n, bufsize - n, " %dbit", fmt->precision);
   7775  1.16.2.2  christos 	} else {
   7776  1.16.2.2  christos 		n += snprintf(buf + n, bufsize - n, " %d/%dbit",
   7777  1.16.2.2  christos 			fmt->precision, fmt->stride);
   7778  1.16.2.2  christos 	}
   7779  1.16.2.2  christos 
   7780  1.16.2.2  christos 	snprintf(buf + n, bufsize - n, " %uch %uHz",
   7781  1.16.2.2  christos 	    fmt->channels, fmt->sample_rate);
   7782  1.16.2.2  christos }
   7783  1.16.2.2  christos #endif
   7784  1.16.2.2  christos 
   7785  1.16.2.2  christos #if defined(AUDIO_DEBUG)
   7786  1.16.2.2  christos static void
   7787  1.16.2.2  christos audio_print_format2(const char *s, const audio_format2_t *fmt)
   7788  1.16.2.2  christos {
   7789  1.16.2.2  christos 	char fmtstr[64];
   7790  1.16.2.2  christos 
   7791  1.16.2.2  christos 	audio_format2_tostr(fmtstr, sizeof(fmtstr), fmt);
   7792  1.16.2.2  christos 	printf("%s %s\n", s, fmtstr);
   7793  1.16.2.2  christos }
   7794  1.16.2.2  christos #endif
   7795  1.16.2.2  christos 
   7796  1.16.2.2  christos #ifdef DIAGNOSTIC
   7797  1.16.2.2  christos void
   7798  1.16.2.3    martin audio_diagnostic_format2(const char *where, const audio_format2_t *fmt)
   7799  1.16.2.2  christos {
   7800  1.16.2.2  christos 
   7801  1.16.2.3    martin 	KASSERTMSG(fmt, "called from %s", where);
   7802  1.16.2.2  christos 
   7803  1.16.2.2  christos 	/* XXX MSM6258 vs(4) only has 4bit stride format. */
   7804  1.16.2.2  christos 	if (fmt->encoding == AUDIO_ENCODING_ADPCM) {
   7805  1.16.2.2  christos 		KASSERTMSG(fmt->stride == 4 || fmt->stride == 8,
   7806  1.16.2.3    martin 		    "called from %s: fmt->stride=%d", where, fmt->stride);
   7807  1.16.2.2  christos 	} else {
   7808  1.16.2.2  christos 		KASSERTMSG(fmt->stride % NBBY == 0,
   7809  1.16.2.3    martin 		    "called from %s: fmt->stride=%d", where, fmt->stride);
   7810  1.16.2.2  christos 	}
   7811  1.16.2.2  christos 	KASSERTMSG(fmt->precision <= fmt->stride,
   7812  1.16.2.3    martin 	    "called from %s: fmt->precision=%d fmt->stride=%d",
   7813  1.16.2.3    martin 	    where, fmt->precision, fmt->stride);
   7814  1.16.2.2  christos 	KASSERTMSG(1 <= fmt->channels && fmt->channels <= AUDIO_MAX_CHANNELS,
   7815  1.16.2.3    martin 	    "called from %s: fmt->channels=%d", where, fmt->channels);
   7816  1.16.2.2  christos 
   7817  1.16.2.2  christos 	/* XXX No check for encodings? */
   7818  1.16.2.2  christos }
   7819  1.16.2.2  christos 
   7820  1.16.2.2  christos void
   7821  1.16.2.3    martin audio_diagnostic_filter_arg(const char *where, const audio_filter_arg_t *arg)
   7822  1.16.2.2  christos {
   7823  1.16.2.2  christos 
   7824  1.16.2.2  christos 	KASSERT(arg != NULL);
   7825  1.16.2.2  christos 	KASSERT(arg->src != NULL);
   7826  1.16.2.2  christos 	KASSERT(arg->dst != NULL);
   7827  1.16.2.3    martin 	audio_diagnostic_format2(where, arg->srcfmt);
   7828  1.16.2.3    martin 	audio_diagnostic_format2(where, arg->dstfmt);
   7829  1.16.2.3    martin 	KASSERT(arg->count > 0);
   7830  1.16.2.2  christos }
   7831  1.16.2.2  christos 
   7832  1.16.2.2  christos void
   7833  1.16.2.3    martin audio_diagnostic_ring(const char *where, const audio_ring_t *ring)
   7834  1.16.2.2  christos {
   7835  1.16.2.2  christos 
   7836  1.16.2.3    martin 	KASSERTMSG(ring, "called from %s", where);
   7837  1.16.2.3    martin 	audio_diagnostic_format2(where, &ring->fmt);
   7838  1.16.2.2  christos 	KASSERTMSG(0 <= ring->capacity && ring->capacity < INT_MAX / 2,
   7839  1.16.2.3    martin 	    "called from %s: ring->capacity=%d", where, ring->capacity);
   7840  1.16.2.2  christos 	KASSERTMSG(0 <= ring->used && ring->used <= ring->capacity,
   7841  1.16.2.3    martin 	    "called from %s: ring->used=%d ring->capacity=%d",
   7842  1.16.2.3    martin 	    where, ring->used, ring->capacity);
   7843  1.16.2.2  christos 	if (ring->capacity == 0) {
   7844  1.16.2.2  christos 		KASSERTMSG(ring->mem == NULL,
   7845  1.16.2.3    martin 		    "called from %s: capacity == 0 but mem != NULL", where);
   7846  1.16.2.2  christos 	} else {
   7847  1.16.2.2  christos 		KASSERTMSG(ring->mem != NULL,
   7848  1.16.2.3    martin 		    "called from %s: capacity != 0 but mem == NULL", where);
   7849  1.16.2.2  christos 		KASSERTMSG(0 <= ring->head && ring->head < ring->capacity,
   7850  1.16.2.3    martin 		    "called from %s: ring->head=%d ring->capacity=%d",
   7851  1.16.2.3    martin 		    where, ring->head, ring->capacity);
   7852  1.16.2.2  christos 	}
   7853  1.16.2.2  christos }
   7854  1.16.2.2  christos #endif /* DIAGNOSTIC */
   7855  1.16.2.2  christos 
   7856  1.16.2.2  christos 
   7857  1.16.2.2  christos /*
   7858  1.16.2.2  christos  * Mixer driver
   7859  1.16.2.2  christos  */
   7860  1.16.2.3    martin 
   7861  1.16.2.3    martin /*
   7862  1.16.2.3    martin  * Must be called without sc_lock held.
   7863  1.16.2.3    martin  */
   7864  1.16.2.2  christos int
   7865  1.16.2.2  christos mixer_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
   7866  1.16.2.2  christos 	struct lwp *l)
   7867  1.16.2.2  christos {
   7868  1.16.2.2  christos 	struct file *fp;
   7869  1.16.2.2  christos 	audio_file_t *af;
   7870  1.16.2.2  christos 	int error, fd;
   7871  1.16.2.2  christos 
   7872  1.16.2.2  christos 	TRACE(1, "flags=0x%x", flags);
   7873  1.16.2.2  christos 
   7874  1.16.2.2  christos 	error = fd_allocfile(&fp, &fd);
   7875  1.16.2.2  christos 	if (error)
   7876  1.16.2.2  christos 		return error;
   7877  1.16.2.2  christos 
   7878  1.16.2.2  christos 	af = kmem_zalloc(sizeof(*af), KM_SLEEP);
   7879  1.16.2.2  christos 	af->sc = sc;
   7880  1.16.2.2  christos 	af->dev = dev;
   7881  1.16.2.2  christos 
   7882  1.16.2.2  christos 	error = fd_clone(fp, fd, flags, &audio_fileops, af);
   7883  1.16.2.2  christos 	KASSERT(error == EMOVEFD);
   7884  1.16.2.2  christos 
   7885  1.16.2.2  christos 	return error;
   7886  1.16.2.2  christos }
   7887  1.16.2.2  christos 
   7888  1.16.2.2  christos /*
   7889  1.16.2.3    martin  * Add a process to those to be signalled on mixer activity.
   7890  1.16.2.3    martin  * If the process has already been added, do nothing.
   7891  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   7892  1.16.2.3    martin  */
   7893  1.16.2.3    martin static void
   7894  1.16.2.3    martin mixer_async_add(struct audio_softc *sc, pid_t pid)
   7895  1.16.2.3    martin {
   7896  1.16.2.3    martin 	int i;
   7897  1.16.2.3    martin 
   7898  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   7899  1.16.2.3    martin 
   7900  1.16.2.3    martin 	/* If already exists, returns without doing anything. */
   7901  1.16.2.3    martin 	for (i = 0; i < sc->sc_am_used; i++) {
   7902  1.16.2.3    martin 		if (sc->sc_am[i] == pid)
   7903  1.16.2.3    martin 			return;
   7904  1.16.2.3    martin 	}
   7905  1.16.2.3    martin 
   7906  1.16.2.3    martin 	/* Extend array if necessary. */
   7907  1.16.2.3    martin 	if (sc->sc_am_used >= sc->sc_am_capacity) {
   7908  1.16.2.3    martin 		sc->sc_am_capacity += AM_CAPACITY;
   7909  1.16.2.3    martin 		sc->sc_am = kern_realloc(sc->sc_am,
   7910  1.16.2.3    martin 		    sc->sc_am_capacity * sizeof(pid_t), M_WAITOK);
   7911  1.16.2.3    martin 		TRACE(2, "realloc am_capacity=%d", sc->sc_am_capacity);
   7912  1.16.2.3    martin 	}
   7913  1.16.2.3    martin 
   7914  1.16.2.3    martin 	TRACE(2, "am[%d]=%d", sc->sc_am_used, (int)pid);
   7915  1.16.2.3    martin 	sc->sc_am[sc->sc_am_used++] = pid;
   7916  1.16.2.3    martin }
   7917  1.16.2.3    martin 
   7918  1.16.2.3    martin /*
   7919  1.16.2.2  christos  * Remove a process from those to be signalled on mixer activity.
   7920  1.16.2.3    martin  * If the process has not been added, do nothing.
   7921  1.16.2.3    martin  * Must be called with sc_exlock held and without sc_lock held.
   7922  1.16.2.2  christos  */
   7923  1.16.2.2  christos static void
   7924  1.16.2.3    martin mixer_async_remove(struct audio_softc *sc, pid_t pid)
   7925  1.16.2.2  christos {
   7926  1.16.2.3    martin 	int i;
   7927  1.16.2.2  christos 
   7928  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   7929  1.16.2.2  christos 
   7930  1.16.2.3    martin 	for (i = 0; i < sc->sc_am_used; i++) {
   7931  1.16.2.3    martin 		if (sc->sc_am[i] == pid) {
   7932  1.16.2.3    martin 			sc->sc_am[i] = sc->sc_am[--sc->sc_am_used];
   7933  1.16.2.3    martin 			TRACE(2, "am[%d](%d) removed, used=%d",
   7934  1.16.2.3    martin 			    i, (int)pid, sc->sc_am_used);
   7935  1.16.2.3    martin 
   7936  1.16.2.3    martin 			/* Empty array if no longer necessary. */
   7937  1.16.2.3    martin 			if (sc->sc_am_used == 0) {
   7938  1.16.2.3    martin 				kern_free(sc->sc_am);
   7939  1.16.2.3    martin 				sc->sc_am = NULL;
   7940  1.16.2.3    martin 				sc->sc_am_capacity = 0;
   7941  1.16.2.3    martin 				TRACE(2, "released");
   7942  1.16.2.3    martin 			}
   7943  1.16.2.2  christos 			return;
   7944  1.16.2.2  christos 		}
   7945  1.16.2.2  christos 	}
   7946  1.16.2.2  christos }
   7947  1.16.2.2  christos 
   7948  1.16.2.2  christos /*
   7949  1.16.2.2  christos  * Signal all processes waiting for the mixer.
   7950  1.16.2.3    martin  * Must be called with sc_exlock held.
   7951  1.16.2.2  christos  */
   7952  1.16.2.2  christos static void
   7953  1.16.2.2  christos mixer_signal(struct audio_softc *sc)
   7954  1.16.2.2  christos {
   7955  1.16.2.2  christos 	proc_t *p;
   7956  1.16.2.3    martin 	int i;
   7957  1.16.2.3    martin 
   7958  1.16.2.3    martin 	KASSERT(sc->sc_exlock);
   7959  1.16.2.2  christos 
   7960  1.16.2.3    martin 	for (i = 0; i < sc->sc_am_used; i++) {
   7961  1.16.2.2  christos 		mutex_enter(proc_lock);
   7962  1.16.2.3    martin 		p = proc_find(sc->sc_am[i]);
   7963  1.16.2.3    martin 		if (p)
   7964  1.16.2.2  christos 			psignal(p, SIGIO);
   7965  1.16.2.2  christos 		mutex_exit(proc_lock);
   7966  1.16.2.2  christos 	}
   7967  1.16.2.2  christos }
   7968  1.16.2.2  christos 
   7969  1.16.2.2  christos /*
   7970  1.16.2.2  christos  * Close a mixer device
   7971  1.16.2.2  christos  */
   7972  1.16.2.2  christos int
   7973  1.16.2.2  christos mixer_close(struct audio_softc *sc, audio_file_t *file)
   7974  1.16.2.2  christos {
   7975  1.16.2.3    martin 	int error;
   7976  1.16.2.2  christos 
   7977  1.16.2.3    martin 	error = audio_exlock_enter(sc);
   7978  1.16.2.3    martin 	if (error)
   7979  1.16.2.3    martin 		return error;
   7980  1.16.2.2  christos 	TRACE(1, "");
   7981  1.16.2.3    martin 	mixer_async_remove(sc, curproc->p_pid);
   7982  1.16.2.3    martin 	audio_exlock_exit(sc);
   7983  1.16.2.2  christos 
   7984  1.16.2.2  christos 	return 0;
   7985  1.16.2.2  christos }
   7986  1.16.2.2  christos 
   7987  1.16.2.3    martin /*
   7988  1.16.2.3    martin  * Must be called without sc_lock nor sc_exlock held.
   7989  1.16.2.3    martin  */
   7990  1.16.2.2  christos int
   7991  1.16.2.2  christos mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
   7992  1.16.2.2  christos 	struct lwp *l)
   7993  1.16.2.2  christos {
   7994  1.16.2.2  christos 	mixer_devinfo_t *mi;
   7995  1.16.2.2  christos 	mixer_ctrl_t *mc;
   7996  1.16.2.2  christos 	int error;
   7997  1.16.2.2  christos 
   7998  1.16.2.2  christos 	TRACE(2, "(%lu,'%c',%lu)",
   7999  1.16.2.2  christos 	    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff);
   8000  1.16.2.2  christos 	error = EINVAL;
   8001  1.16.2.2  christos 
   8002  1.16.2.2  christos 	/* we can return cached values if we are sleeping */
   8003  1.16.2.2  christos 	if (cmd != AUDIO_MIXER_READ) {
   8004  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   8005  1.16.2.2  christos 		device_active(sc->sc_dev, DVA_SYSTEM);
   8006  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   8007  1.16.2.2  christos 	}
   8008  1.16.2.2  christos 
   8009  1.16.2.2  christos 	switch (cmd) {
   8010  1.16.2.2  christos 	case FIOASYNC:
   8011  1.16.2.3    martin 		error = audio_exlock_enter(sc);
   8012  1.16.2.3    martin 		if (error)
   8013  1.16.2.3    martin 			break;
   8014  1.16.2.2  christos 		if (*(int *)addr) {
   8015  1.16.2.3    martin 			mixer_async_add(sc, curproc->p_pid);
   8016  1.16.2.2  christos 		} else {
   8017  1.16.2.3    martin 			mixer_async_remove(sc, curproc->p_pid);
   8018  1.16.2.2  christos 		}
   8019  1.16.2.3    martin 		audio_exlock_exit(sc);
   8020  1.16.2.2  christos 		break;
   8021  1.16.2.2  christos 
   8022  1.16.2.2  christos 	case AUDIO_GETDEV:
   8023  1.16.2.2  christos 		TRACE(2, "AUDIO_GETDEV");
   8024  1.16.2.3    martin 		mutex_enter(sc->sc_lock);
   8025  1.16.2.2  christos 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
   8026  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   8027  1.16.2.2  christos 		break;
   8028  1.16.2.2  christos 
   8029  1.16.2.2  christos 	case AUDIO_MIXER_DEVINFO:
   8030  1.16.2.2  christos 		TRACE(2, "AUDIO_MIXER_DEVINFO");
   8031  1.16.2.2  christos 		mi = (mixer_devinfo_t *)addr;
   8032  1.16.2.2  christos 
   8033  1.16.2.2  christos 		mi->un.v.delta = 0; /* default */
   8034  1.16.2.2  christos 		mutex_enter(sc->sc_lock);
   8035  1.16.2.2  christos 		error = audio_query_devinfo(sc, mi);
   8036  1.16.2.2  christos 		mutex_exit(sc->sc_lock);
   8037  1.16.2.2  christos 		break;
   8038  1.16.2.2  christos 
   8039  1.16.2.2  christos 	case AUDIO_MIXER_READ:
   8040  1.16.2.2  christos 		TRACE(2, "AUDIO_MIXER_READ");
   8041  1.16.2.2  christos 		mc = (mixer_ctrl_t *)addr;
   8042  1.16.2.2  christos 
   8043  1.16.2.3    martin 		error = audio_exlock_mutex_enter(sc);
   8044  1.16.2.2  christos 		if (error)
   8045  1.16.2.2  christos 			break;
   8046  1.16.2.2  christos 		if (device_is_active(sc->hw_dev))
   8047  1.16.2.2  christos 			error = audio_get_port(sc, mc);
   8048  1.16.2.2  christos 		else if (mc->dev < 0 || mc->dev >= sc->sc_nmixer_states)
   8049  1.16.2.2  christos 			error = ENXIO;
   8050  1.16.2.2  christos 		else {
   8051  1.16.2.2  christos 			int dev = mc->dev;
   8052  1.16.2.2  christos 			memcpy(mc, &sc->sc_mixer_state[dev],
   8053  1.16.2.2  christos 			    sizeof(mixer_ctrl_t));
   8054  1.16.2.2  christos 			error = 0;
   8055  1.16.2.2  christos 		}
   8056  1.16.2.3    martin 		audio_exlock_mutex_exit(sc);
   8057  1.16.2.2  christos 		break;
   8058  1.16.2.2  christos 
   8059  1.16.2.2  christos 	case AUDIO_MIXER_WRITE:
   8060  1.16.2.2  christos 		TRACE(2, "AUDIO_MIXER_WRITE");
   8061  1.16.2.3    martin 		error = audio_exlock_mutex_enter(sc);
   8062  1.16.2.2  christos 		if (error)
   8063  1.16.2.2  christos 			break;
   8064  1.16.2.2  christos 		error = audio_set_port(sc, (mixer_ctrl_t *)addr);
   8065  1.16.2.2  christos 		if (error) {
   8066  1.16.2.3    martin 			audio_exlock_mutex_exit(sc);
   8067  1.16.2.2  christos 			break;
   8068  1.16.2.2  christos 		}
   8069  1.16.2.2  christos 
   8070  1.16.2.2  christos 		if (sc->hw_if->commit_settings) {
   8071  1.16.2.2  christos 			error = sc->hw_if->commit_settings(sc->hw_hdl);
   8072  1.16.2.2  christos 			if (error) {
   8073  1.16.2.3    martin 				audio_exlock_mutex_exit(sc);
   8074  1.16.2.2  christos 				break;
   8075  1.16.2.2  christos 			}
   8076  1.16.2.2  christos 		}
   8077  1.16.2.3    martin 		mutex_exit(sc->sc_lock);
   8078  1.16.2.2  christos 		mixer_signal(sc);
   8079  1.16.2.3    martin 		audio_exlock_exit(sc);
   8080  1.16.2.2  christos 		break;
   8081  1.16.2.2  christos 
   8082  1.16.2.2  christos 	default:
   8083  1.16.2.2  christos 		if (sc->hw_if->dev_ioctl) {
   8084  1.16.2.3    martin 			mutex_enter(sc->sc_lock);
   8085  1.16.2.2  christos 			error = sc->hw_if->dev_ioctl(sc->hw_hdl,
   8086  1.16.2.2  christos 			    cmd, addr, flag, l);
   8087  1.16.2.3    martin 			mutex_exit(sc->sc_lock);
   8088  1.16.2.2  christos 		} else
   8089  1.16.2.2  christos 			error = EINVAL;
   8090  1.16.2.2  christos 		break;
   8091  1.16.2.2  christos 	}
   8092  1.16.2.2  christos 	TRACE(2, "(%lu,'%c',%lu) result %d",
   8093  1.16.2.2  christos 	    IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, error);
   8094  1.16.2.2  christos 	return error;
   8095  1.16.2.2  christos }
   8096  1.16.2.2  christos 
   8097  1.16.2.2  christos /*
   8098  1.16.2.2  christos  * Must be called with sc_lock held.
   8099  1.16.2.2  christos  */
   8100  1.16.2.2  christos int
   8101  1.16.2.2  christos au_portof(struct audio_softc *sc, char *name, int class)
   8102  1.16.2.2  christos {
   8103  1.16.2.2  christos 	mixer_devinfo_t mi;
   8104  1.16.2.2  christos 
   8105  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8106  1.16.2.2  christos 
   8107  1.16.2.2  christos 	for (mi.index = 0; audio_query_devinfo(sc, &mi) == 0; mi.index++) {
   8108  1.16.2.2  christos 		if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
   8109  1.16.2.2  christos 			return mi.index;
   8110  1.16.2.2  christos 	}
   8111  1.16.2.2  christos 	return -1;
   8112  1.16.2.2  christos }
   8113  1.16.2.2  christos 
   8114  1.16.2.2  christos /*
   8115  1.16.2.2  christos  * Must be called with sc_lock held.
   8116  1.16.2.2  christos  */
   8117  1.16.2.2  christos void
   8118  1.16.2.2  christos au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
   8119  1.16.2.2  christos 	mixer_devinfo_t *mi, const struct portname *tbl)
   8120  1.16.2.2  christos {
   8121  1.16.2.2  christos 	int i, j;
   8122  1.16.2.2  christos 
   8123  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8124  1.16.2.2  christos 
   8125  1.16.2.2  christos 	ports->index = mi->index;
   8126  1.16.2.2  christos 	if (mi->type == AUDIO_MIXER_ENUM) {
   8127  1.16.2.2  christos 		ports->isenum = true;
   8128  1.16.2.2  christos 		for(i = 0; tbl[i].name; i++)
   8129  1.16.2.2  christos 		    for(j = 0; j < mi->un.e.num_mem; j++)
   8130  1.16.2.2  christos 			if (strcmp(mi->un.e.member[j].label.name,
   8131  1.16.2.2  christos 						    tbl[i].name) == 0) {
   8132  1.16.2.2  christos 				ports->allports |= tbl[i].mask;
   8133  1.16.2.2  christos 				ports->aumask[ports->nports] = tbl[i].mask;
   8134  1.16.2.2  christos 				ports->misel[ports->nports] =
   8135  1.16.2.2  christos 				    mi->un.e.member[j].ord;
   8136  1.16.2.2  christos 				ports->miport[ports->nports] =
   8137  1.16.2.2  christos 				    au_portof(sc, mi->un.e.member[j].label.name,
   8138  1.16.2.2  christos 				    mi->mixer_class);
   8139  1.16.2.2  christos 				if (ports->mixerout != -1 &&
   8140  1.16.2.2  christos 				    ports->miport[ports->nports] != -1)
   8141  1.16.2.2  christos 					ports->isdual = true;
   8142  1.16.2.2  christos 				++ports->nports;
   8143  1.16.2.2  christos 			}
   8144  1.16.2.2  christos 	} else if (mi->type == AUDIO_MIXER_SET) {
   8145  1.16.2.2  christos 		for(i = 0; tbl[i].name; i++)
   8146  1.16.2.2  christos 		    for(j = 0; j < mi->un.s.num_mem; j++)
   8147  1.16.2.2  christos 			if (strcmp(mi->un.s.member[j].label.name,
   8148  1.16.2.2  christos 						tbl[i].name) == 0) {
   8149  1.16.2.2  christos 				ports->allports |= tbl[i].mask;
   8150  1.16.2.2  christos 				ports->aumask[ports->nports] = tbl[i].mask;
   8151  1.16.2.2  christos 				ports->misel[ports->nports] =
   8152  1.16.2.2  christos 				    mi->un.s.member[j].mask;
   8153  1.16.2.2  christos 				ports->miport[ports->nports] =
   8154  1.16.2.2  christos 				    au_portof(sc, mi->un.s.member[j].label.name,
   8155  1.16.2.2  christos 				    mi->mixer_class);
   8156  1.16.2.2  christos 				++ports->nports;
   8157  1.16.2.2  christos 			}
   8158  1.16.2.2  christos 	}
   8159  1.16.2.2  christos }
   8160  1.16.2.2  christos 
   8161  1.16.2.2  christos /*
   8162  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8163  1.16.2.2  christos  */
   8164  1.16.2.2  christos int
   8165  1.16.2.2  christos au_set_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
   8166  1.16.2.2  christos {
   8167  1.16.2.2  christos 
   8168  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8169  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8170  1.16.2.2  christos 
   8171  1.16.2.2  christos 	ct->type = AUDIO_MIXER_VALUE;
   8172  1.16.2.2  christos 	ct->un.value.num_channels = 2;
   8173  1.16.2.2  christos 	ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   8174  1.16.2.2  christos 	ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   8175  1.16.2.2  christos 	if (audio_set_port(sc, ct) == 0)
   8176  1.16.2.2  christos 		return 0;
   8177  1.16.2.2  christos 	ct->un.value.num_channels = 1;
   8178  1.16.2.2  christos 	ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
   8179  1.16.2.2  christos 	return audio_set_port(sc, ct);
   8180  1.16.2.2  christos }
   8181  1.16.2.2  christos 
   8182  1.16.2.2  christos /*
   8183  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8184  1.16.2.2  christos  */
   8185  1.16.2.2  christos int
   8186  1.16.2.2  christos au_get_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
   8187  1.16.2.2  christos {
   8188  1.16.2.2  christos 	int error;
   8189  1.16.2.2  christos 
   8190  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8191  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8192  1.16.2.2  christos 
   8193  1.16.2.2  christos 	ct->un.value.num_channels = 2;
   8194  1.16.2.2  christos 	if (audio_get_port(sc, ct) == 0) {
   8195  1.16.2.2  christos 		*l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   8196  1.16.2.2  christos 		*r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   8197  1.16.2.2  christos 	} else {
   8198  1.16.2.2  christos 		ct->un.value.num_channels = 1;
   8199  1.16.2.2  christos 		error = audio_get_port(sc, ct);
   8200  1.16.2.2  christos 		if (error)
   8201  1.16.2.2  christos 			return error;
   8202  1.16.2.2  christos 		*r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
   8203  1.16.2.2  christos 	}
   8204  1.16.2.2  christos 	return 0;
   8205  1.16.2.2  christos }
   8206  1.16.2.2  christos 
   8207  1.16.2.2  christos /*
   8208  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8209  1.16.2.2  christos  */
   8210  1.16.2.2  christos int
   8211  1.16.2.2  christos au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   8212  1.16.2.2  christos 	int gain, int balance)
   8213  1.16.2.2  christos {
   8214  1.16.2.2  christos 	mixer_ctrl_t ct;
   8215  1.16.2.2  christos 	int i, error;
   8216  1.16.2.2  christos 	int l, r;
   8217  1.16.2.2  christos 	u_int mask;
   8218  1.16.2.2  christos 	int nset;
   8219  1.16.2.2  christos 
   8220  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8221  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8222  1.16.2.2  christos 
   8223  1.16.2.2  christos 	if (balance == AUDIO_MID_BALANCE) {
   8224  1.16.2.2  christos 		l = r = gain;
   8225  1.16.2.2  christos 	} else if (balance < AUDIO_MID_BALANCE) {
   8226  1.16.2.2  christos 		l = gain;
   8227  1.16.2.2  christos 		r = (balance * gain) / AUDIO_MID_BALANCE;
   8228  1.16.2.2  christos 	} else {
   8229  1.16.2.2  christos 		r = gain;
   8230  1.16.2.2  christos 		l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
   8231  1.16.2.2  christos 		    / AUDIO_MID_BALANCE;
   8232  1.16.2.2  christos 	}
   8233  1.16.2.2  christos 	TRACE(2, "gain=%d balance=%d, l=%d r=%d", gain, balance, l, r);
   8234  1.16.2.2  christos 
   8235  1.16.2.2  christos 	if (ports->index == -1) {
   8236  1.16.2.2  christos 	usemaster:
   8237  1.16.2.2  christos 		if (ports->master == -1)
   8238  1.16.2.2  christos 			return 0; /* just ignore it silently */
   8239  1.16.2.2  christos 		ct.dev = ports->master;
   8240  1.16.2.2  christos 		error = au_set_lr_value(sc, &ct, l, r);
   8241  1.16.2.2  christos 	} else {
   8242  1.16.2.2  christos 		ct.dev = ports->index;
   8243  1.16.2.2  christos 		if (ports->isenum) {
   8244  1.16.2.2  christos 			ct.type = AUDIO_MIXER_ENUM;
   8245  1.16.2.2  christos 			error = audio_get_port(sc, &ct);
   8246  1.16.2.2  christos 			if (error)
   8247  1.16.2.2  christos 				return error;
   8248  1.16.2.2  christos 			if (ports->isdual) {
   8249  1.16.2.2  christos 				if (ports->cur_port == -1)
   8250  1.16.2.2  christos 					ct.dev = ports->master;
   8251  1.16.2.2  christos 				else
   8252  1.16.2.2  christos 					ct.dev = ports->miport[ports->cur_port];
   8253  1.16.2.2  christos 				error = au_set_lr_value(sc, &ct, l, r);
   8254  1.16.2.2  christos 			} else {
   8255  1.16.2.2  christos 				for(i = 0; i < ports->nports; i++)
   8256  1.16.2.2  christos 				    if (ports->misel[i] == ct.un.ord) {
   8257  1.16.2.2  christos 					    ct.dev = ports->miport[i];
   8258  1.16.2.2  christos 					    if (ct.dev == -1 ||
   8259  1.16.2.2  christos 						au_set_lr_value(sc, &ct, l, r))
   8260  1.16.2.2  christos 						    goto usemaster;
   8261  1.16.2.2  christos 					    else
   8262  1.16.2.2  christos 						    break;
   8263  1.16.2.2  christos 				    }
   8264  1.16.2.2  christos 			}
   8265  1.16.2.2  christos 		} else {
   8266  1.16.2.2  christos 			ct.type = AUDIO_MIXER_SET;
   8267  1.16.2.2  christos 			error = audio_get_port(sc, &ct);
   8268  1.16.2.2  christos 			if (error)
   8269  1.16.2.2  christos 				return error;
   8270  1.16.2.2  christos 			mask = ct.un.mask;
   8271  1.16.2.2  christos 			nset = 0;
   8272  1.16.2.2  christos 			for(i = 0; i < ports->nports; i++) {
   8273  1.16.2.2  christos 				if (ports->misel[i] & mask) {
   8274  1.16.2.2  christos 				    ct.dev = ports->miport[i];
   8275  1.16.2.2  christos 				    if (ct.dev != -1 &&
   8276  1.16.2.2  christos 					au_set_lr_value(sc, &ct, l, r) == 0)
   8277  1.16.2.2  christos 					    nset++;
   8278  1.16.2.2  christos 				}
   8279  1.16.2.2  christos 			}
   8280  1.16.2.2  christos 			if (nset == 0)
   8281  1.16.2.2  christos 				goto usemaster;
   8282  1.16.2.2  christos 		}
   8283  1.16.2.2  christos 	}
   8284  1.16.2.2  christos 	if (!error)
   8285  1.16.2.2  christos 		mixer_signal(sc);
   8286  1.16.2.2  christos 	return error;
   8287  1.16.2.2  christos }
   8288  1.16.2.2  christos 
   8289  1.16.2.2  christos /*
   8290  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8291  1.16.2.2  christos  */
   8292  1.16.2.2  christos void
   8293  1.16.2.2  christos au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
   8294  1.16.2.2  christos 	u_int *pgain, u_char *pbalance)
   8295  1.16.2.2  christos {
   8296  1.16.2.2  christos 	mixer_ctrl_t ct;
   8297  1.16.2.2  christos 	int i, l, r, n;
   8298  1.16.2.2  christos 	int lgain, rgain;
   8299  1.16.2.2  christos 
   8300  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8301  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8302  1.16.2.2  christos 
   8303  1.16.2.2  christos 	lgain = AUDIO_MAX_GAIN / 2;
   8304  1.16.2.2  christos 	rgain = AUDIO_MAX_GAIN / 2;
   8305  1.16.2.2  christos 	if (ports->index == -1) {
   8306  1.16.2.2  christos 	usemaster:
   8307  1.16.2.2  christos 		if (ports->master == -1)
   8308  1.16.2.2  christos 			goto bad;
   8309  1.16.2.2  christos 		ct.dev = ports->master;
   8310  1.16.2.2  christos 		ct.type = AUDIO_MIXER_VALUE;
   8311  1.16.2.2  christos 		if (au_get_lr_value(sc, &ct, &lgain, &rgain))
   8312  1.16.2.2  christos 			goto bad;
   8313  1.16.2.2  christos 	} else {
   8314  1.16.2.2  christos 		ct.dev = ports->index;
   8315  1.16.2.2  christos 		if (ports->isenum) {
   8316  1.16.2.2  christos 			ct.type = AUDIO_MIXER_ENUM;
   8317  1.16.2.2  christos 			if (audio_get_port(sc, &ct))
   8318  1.16.2.2  christos 				goto bad;
   8319  1.16.2.2  christos 			ct.type = AUDIO_MIXER_VALUE;
   8320  1.16.2.2  christos 			if (ports->isdual) {
   8321  1.16.2.2  christos 				if (ports->cur_port == -1)
   8322  1.16.2.2  christos 					ct.dev = ports->master;
   8323  1.16.2.2  christos 				else
   8324  1.16.2.2  christos 					ct.dev = ports->miport[ports->cur_port];
   8325  1.16.2.2  christos 				au_get_lr_value(sc, &ct, &lgain, &rgain);
   8326  1.16.2.2  christos 			} else {
   8327  1.16.2.2  christos 				for(i = 0; i < ports->nports; i++)
   8328  1.16.2.2  christos 				    if (ports->misel[i] == ct.un.ord) {
   8329  1.16.2.2  christos 					    ct.dev = ports->miport[i];
   8330  1.16.2.2  christos 					    if (ct.dev == -1 ||
   8331  1.16.2.2  christos 						au_get_lr_value(sc, &ct,
   8332  1.16.2.2  christos 								&lgain, &rgain))
   8333  1.16.2.2  christos 						    goto usemaster;
   8334  1.16.2.2  christos 					    else
   8335  1.16.2.2  christos 						    break;
   8336  1.16.2.2  christos 				    }
   8337  1.16.2.2  christos 			}
   8338  1.16.2.2  christos 		} else {
   8339  1.16.2.2  christos 			ct.type = AUDIO_MIXER_SET;
   8340  1.16.2.2  christos 			if (audio_get_port(sc, &ct))
   8341  1.16.2.2  christos 				goto bad;
   8342  1.16.2.2  christos 			ct.type = AUDIO_MIXER_VALUE;
   8343  1.16.2.2  christos 			lgain = rgain = n = 0;
   8344  1.16.2.2  christos 			for(i = 0; i < ports->nports; i++) {
   8345  1.16.2.2  christos 				if (ports->misel[i] & ct.un.mask) {
   8346  1.16.2.2  christos 					ct.dev = ports->miport[i];
   8347  1.16.2.2  christos 					if (ct.dev == -1 ||
   8348  1.16.2.2  christos 					    au_get_lr_value(sc, &ct, &l, &r))
   8349  1.16.2.2  christos 						goto usemaster;
   8350  1.16.2.2  christos 					else {
   8351  1.16.2.2  christos 						lgain += l;
   8352  1.16.2.2  christos 						rgain += r;
   8353  1.16.2.2  christos 						n++;
   8354  1.16.2.2  christos 					}
   8355  1.16.2.2  christos 				}
   8356  1.16.2.2  christos 			}
   8357  1.16.2.2  christos 			if (n != 0) {
   8358  1.16.2.2  christos 				lgain /= n;
   8359  1.16.2.2  christos 				rgain /= n;
   8360  1.16.2.2  christos 			}
   8361  1.16.2.2  christos 		}
   8362  1.16.2.2  christos 	}
   8363  1.16.2.2  christos bad:
   8364  1.16.2.2  christos 	if (lgain == rgain) {	/* handles lgain==rgain==0 */
   8365  1.16.2.2  christos 		*pgain = lgain;
   8366  1.16.2.2  christos 		*pbalance = AUDIO_MID_BALANCE;
   8367  1.16.2.2  christos 	} else if (lgain < rgain) {
   8368  1.16.2.2  christos 		*pgain = rgain;
   8369  1.16.2.2  christos 		/* balance should be > AUDIO_MID_BALANCE */
   8370  1.16.2.2  christos 		*pbalance = AUDIO_RIGHT_BALANCE -
   8371  1.16.2.2  christos 			(AUDIO_MID_BALANCE * lgain) / rgain;
   8372  1.16.2.2  christos 	} else /* lgain > rgain */ {
   8373  1.16.2.2  christos 		*pgain = lgain;
   8374  1.16.2.2  christos 		/* balance should be < AUDIO_MID_BALANCE */
   8375  1.16.2.2  christos 		*pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
   8376  1.16.2.2  christos 	}
   8377  1.16.2.2  christos }
   8378  1.16.2.2  christos 
   8379  1.16.2.2  christos /*
   8380  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8381  1.16.2.2  christos  */
   8382  1.16.2.2  christos int
   8383  1.16.2.2  christos au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
   8384  1.16.2.2  christos {
   8385  1.16.2.2  christos 	mixer_ctrl_t ct;
   8386  1.16.2.2  christos 	int i, error, use_mixerout;
   8387  1.16.2.2  christos 
   8388  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8389  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8390  1.16.2.2  christos 
   8391  1.16.2.2  christos 	use_mixerout = 1;
   8392  1.16.2.2  christos 	if (port == 0) {
   8393  1.16.2.2  christos 		if (ports->allports == 0)
   8394  1.16.2.2  christos 			return 0;		/* Allow this special case. */
   8395  1.16.2.2  christos 		else if (ports->isdual) {
   8396  1.16.2.2  christos 			if (ports->cur_port == -1) {
   8397  1.16.2.2  christos 				return 0;
   8398  1.16.2.2  christos 			} else {
   8399  1.16.2.2  christos 				port = ports->aumask[ports->cur_port];
   8400  1.16.2.2  christos 				ports->cur_port = -1;
   8401  1.16.2.2  christos 				use_mixerout = 0;
   8402  1.16.2.2  christos 			}
   8403  1.16.2.2  christos 		}
   8404  1.16.2.2  christos 	}
   8405  1.16.2.2  christos 	if (ports->index == -1)
   8406  1.16.2.2  christos 		return EINVAL;
   8407  1.16.2.2  christos 	ct.dev = ports->index;
   8408  1.16.2.2  christos 	if (ports->isenum) {
   8409  1.16.2.2  christos 		if (port & (port-1))
   8410  1.16.2.2  christos 			return EINVAL; /* Only one port allowed */
   8411  1.16.2.2  christos 		ct.type = AUDIO_MIXER_ENUM;
   8412  1.16.2.2  christos 		error = EINVAL;
   8413  1.16.2.2  christos 		for(i = 0; i < ports->nports; i++)
   8414  1.16.2.2  christos 			if (ports->aumask[i] == port) {
   8415  1.16.2.2  christos 				if (ports->isdual && use_mixerout) {
   8416  1.16.2.2  christos 					ct.un.ord = ports->mixerout;
   8417  1.16.2.2  christos 					ports->cur_port = i;
   8418  1.16.2.2  christos 				} else {
   8419  1.16.2.2  christos 					ct.un.ord = ports->misel[i];
   8420  1.16.2.2  christos 				}
   8421  1.16.2.2  christos 				error = audio_set_port(sc, &ct);
   8422  1.16.2.2  christos 				break;
   8423  1.16.2.2  christos 			}
   8424  1.16.2.2  christos 	} else {
   8425  1.16.2.2  christos 		ct.type = AUDIO_MIXER_SET;
   8426  1.16.2.2  christos 		ct.un.mask = 0;
   8427  1.16.2.2  christos 		for(i = 0; i < ports->nports; i++)
   8428  1.16.2.2  christos 			if (ports->aumask[i] & port)
   8429  1.16.2.2  christos 				ct.un.mask |= ports->misel[i];
   8430  1.16.2.2  christos 		if (port != 0 && ct.un.mask == 0)
   8431  1.16.2.2  christos 			error = EINVAL;
   8432  1.16.2.2  christos 		else
   8433  1.16.2.2  christos 			error = audio_set_port(sc, &ct);
   8434  1.16.2.2  christos 	}
   8435  1.16.2.2  christos 	if (!error)
   8436  1.16.2.2  christos 		mixer_signal(sc);
   8437  1.16.2.2  christos 	return error;
   8438  1.16.2.2  christos }
   8439  1.16.2.2  christos 
   8440  1.16.2.2  christos /*
   8441  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8442  1.16.2.2  christos  */
   8443  1.16.2.2  christos int
   8444  1.16.2.2  christos au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
   8445  1.16.2.2  christos {
   8446  1.16.2.2  christos 	mixer_ctrl_t ct;
   8447  1.16.2.2  christos 	int i, aumask;
   8448  1.16.2.2  christos 
   8449  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8450  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8451  1.16.2.2  christos 
   8452  1.16.2.2  christos 	if (ports->index == -1)
   8453  1.16.2.2  christos 		return 0;
   8454  1.16.2.2  christos 	ct.dev = ports->index;
   8455  1.16.2.2  christos 	ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
   8456  1.16.2.2  christos 	if (audio_get_port(sc, &ct))
   8457  1.16.2.2  christos 		return 0;
   8458  1.16.2.2  christos 	aumask = 0;
   8459  1.16.2.2  christos 	if (ports->isenum) {
   8460  1.16.2.2  christos 		if (ports->isdual && ports->cur_port != -1) {
   8461  1.16.2.2  christos 			if (ports->mixerout == ct.un.ord)
   8462  1.16.2.2  christos 				aumask = ports->aumask[ports->cur_port];
   8463  1.16.2.2  christos 			else
   8464  1.16.2.2  christos 				ports->cur_port = -1;
   8465  1.16.2.2  christos 		}
   8466  1.16.2.2  christos 		if (aumask == 0)
   8467  1.16.2.2  christos 			for(i = 0; i < ports->nports; i++)
   8468  1.16.2.2  christos 				if (ports->misel[i] == ct.un.ord)
   8469  1.16.2.2  christos 					aumask = ports->aumask[i];
   8470  1.16.2.2  christos 	} else {
   8471  1.16.2.2  christos 		for(i = 0; i < ports->nports; i++)
   8472  1.16.2.2  christos 			if (ct.un.mask & ports->misel[i])
   8473  1.16.2.2  christos 				aumask |= ports->aumask[i];
   8474  1.16.2.2  christos 	}
   8475  1.16.2.2  christos 	return aumask;
   8476  1.16.2.2  christos }
   8477  1.16.2.2  christos 
   8478  1.16.2.2  christos /*
   8479  1.16.2.2  christos  * It returns 0 if success, otherwise errno.
   8480  1.16.2.2  christos  * Must be called only if sc->sc_monitor_port != -1.
   8481  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8482  1.16.2.2  christos  */
   8483  1.16.2.2  christos static int
   8484  1.16.2.2  christos au_set_monitor_gain(struct audio_softc *sc, int monitor_gain)
   8485  1.16.2.2  christos {
   8486  1.16.2.2  christos 	mixer_ctrl_t ct;
   8487  1.16.2.2  christos 
   8488  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8489  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8490  1.16.2.2  christos 
   8491  1.16.2.2  christos 	ct.dev = sc->sc_monitor_port;
   8492  1.16.2.2  christos 	ct.type = AUDIO_MIXER_VALUE;
   8493  1.16.2.2  christos 	ct.un.value.num_channels = 1;
   8494  1.16.2.2  christos 	ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = monitor_gain;
   8495  1.16.2.2  christos 	return audio_set_port(sc, &ct);
   8496  1.16.2.2  christos }
   8497  1.16.2.2  christos 
   8498  1.16.2.2  christos /*
   8499  1.16.2.2  christos  * It returns monitor gain if success, otherwise -1.
   8500  1.16.2.2  christos  * Must be called only if sc->sc_monitor_port != -1.
   8501  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8502  1.16.2.2  christos  */
   8503  1.16.2.2  christos static int
   8504  1.16.2.2  christos au_get_monitor_gain(struct audio_softc *sc)
   8505  1.16.2.2  christos {
   8506  1.16.2.2  christos 	mixer_ctrl_t ct;
   8507  1.16.2.2  christos 
   8508  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8509  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8510  1.16.2.2  christos 
   8511  1.16.2.2  christos 	ct.dev = sc->sc_monitor_port;
   8512  1.16.2.2  christos 	ct.type = AUDIO_MIXER_VALUE;
   8513  1.16.2.2  christos 	ct.un.value.num_channels = 1;
   8514  1.16.2.2  christos 	if (audio_get_port(sc, &ct))
   8515  1.16.2.2  christos 		return -1;
   8516  1.16.2.2  christos 	return ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
   8517  1.16.2.2  christos }
   8518  1.16.2.2  christos 
   8519  1.16.2.2  christos /*
   8520  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8521  1.16.2.2  christos  */
   8522  1.16.2.2  christos static int
   8523  1.16.2.2  christos audio_set_port(struct audio_softc *sc, mixer_ctrl_t *mc)
   8524  1.16.2.2  christos {
   8525  1.16.2.2  christos 
   8526  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8527  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8528  1.16.2.2  christos 
   8529  1.16.2.2  christos 	return sc->hw_if->set_port(sc->hw_hdl, mc);
   8530  1.16.2.2  christos }
   8531  1.16.2.2  christos 
   8532  1.16.2.2  christos /*
   8533  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8534  1.16.2.2  christos  */
   8535  1.16.2.2  christos static int
   8536  1.16.2.2  christos audio_get_port(struct audio_softc *sc, mixer_ctrl_t *mc)
   8537  1.16.2.2  christos {
   8538  1.16.2.2  christos 
   8539  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8540  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8541  1.16.2.2  christos 
   8542  1.16.2.2  christos 	return sc->hw_if->get_port(sc->hw_hdl, mc);
   8543  1.16.2.2  christos }
   8544  1.16.2.2  christos 
   8545  1.16.2.2  christos /*
   8546  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8547  1.16.2.2  christos  */
   8548  1.16.2.2  christos static void
   8549  1.16.2.2  christos audio_mixer_capture(struct audio_softc *sc)
   8550  1.16.2.2  christos {
   8551  1.16.2.2  christos 	mixer_devinfo_t mi;
   8552  1.16.2.2  christos 	mixer_ctrl_t *mc;
   8553  1.16.2.2  christos 
   8554  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8555  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8556  1.16.2.2  christos 
   8557  1.16.2.2  christos 	for (mi.index = 0;; mi.index++) {
   8558  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) != 0)
   8559  1.16.2.2  christos 			break;
   8560  1.16.2.2  christos 		KASSERT(mi.index < sc->sc_nmixer_states);
   8561  1.16.2.2  christos 		if (mi.type == AUDIO_MIXER_CLASS)
   8562  1.16.2.2  christos 			continue;
   8563  1.16.2.2  christos 		mc = &sc->sc_mixer_state[mi.index];
   8564  1.16.2.2  christos 		mc->dev = mi.index;
   8565  1.16.2.2  christos 		mc->type = mi.type;
   8566  1.16.2.2  christos 		mc->un.value.num_channels = mi.un.v.num_channels;
   8567  1.16.2.2  christos 		(void)audio_get_port(sc, mc);
   8568  1.16.2.2  christos 	}
   8569  1.16.2.2  christos 
   8570  1.16.2.2  christos 	return;
   8571  1.16.2.2  christos }
   8572  1.16.2.2  christos 
   8573  1.16.2.2  christos /*
   8574  1.16.2.2  christos  * Must be called with sc_lock && sc_exlock held.
   8575  1.16.2.2  christos  */
   8576  1.16.2.2  christos static void
   8577  1.16.2.2  christos audio_mixer_restore(struct audio_softc *sc)
   8578  1.16.2.2  christos {
   8579  1.16.2.2  christos 	mixer_devinfo_t mi;
   8580  1.16.2.2  christos 	mixer_ctrl_t *mc;
   8581  1.16.2.2  christos 
   8582  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8583  1.16.2.2  christos 	KASSERT(sc->sc_exlock);
   8584  1.16.2.2  christos 
   8585  1.16.2.2  christos 	for (mi.index = 0; ; mi.index++) {
   8586  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) != 0)
   8587  1.16.2.2  christos 			break;
   8588  1.16.2.2  christos 		if (mi.type == AUDIO_MIXER_CLASS)
   8589  1.16.2.2  christos 			continue;
   8590  1.16.2.2  christos 		mc = &sc->sc_mixer_state[mi.index];
   8591  1.16.2.2  christos 		(void)audio_set_port(sc, mc);
   8592  1.16.2.2  christos 	}
   8593  1.16.2.2  christos 	if (sc->hw_if->commit_settings)
   8594  1.16.2.2  christos 		sc->hw_if->commit_settings(sc->hw_hdl);
   8595  1.16.2.2  christos 
   8596  1.16.2.2  christos 	return;
   8597  1.16.2.2  christos }
   8598  1.16.2.2  christos 
   8599  1.16.2.2  christos static void
   8600  1.16.2.2  christos audio_volume_down(device_t dv)
   8601  1.16.2.2  christos {
   8602  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   8603  1.16.2.2  christos 	mixer_devinfo_t mi;
   8604  1.16.2.2  christos 	int newgain;
   8605  1.16.2.2  christos 	u_int gain;
   8606  1.16.2.2  christos 	u_char balance;
   8607  1.16.2.2  christos 
   8608  1.16.2.3    martin 	if (audio_exlock_mutex_enter(sc) != 0)
   8609  1.16.2.2  christos 		return;
   8610  1.16.2.2  christos 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   8611  1.16.2.2  christos 		mi.index = sc->sc_outports.master;
   8612  1.16.2.2  christos 		mi.un.v.delta = 0;
   8613  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) == 0) {
   8614  1.16.2.2  christos 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   8615  1.16.2.2  christos 			newgain = gain - mi.un.v.delta;
   8616  1.16.2.2  christos 			if (newgain < AUDIO_MIN_GAIN)
   8617  1.16.2.2  christos 				newgain = AUDIO_MIN_GAIN;
   8618  1.16.2.2  christos 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   8619  1.16.2.2  christos 		}
   8620  1.16.2.2  christos 	}
   8621  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   8622  1.16.2.2  christos }
   8623  1.16.2.2  christos 
   8624  1.16.2.2  christos static void
   8625  1.16.2.2  christos audio_volume_up(device_t dv)
   8626  1.16.2.2  christos {
   8627  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   8628  1.16.2.2  christos 	mixer_devinfo_t mi;
   8629  1.16.2.2  christos 	u_int gain, newgain;
   8630  1.16.2.2  christos 	u_char balance;
   8631  1.16.2.2  christos 
   8632  1.16.2.3    martin 	if (audio_exlock_mutex_enter(sc) != 0)
   8633  1.16.2.2  christos 		return;
   8634  1.16.2.2  christos 	if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
   8635  1.16.2.2  christos 		mi.index = sc->sc_outports.master;
   8636  1.16.2.2  christos 		mi.un.v.delta = 0;
   8637  1.16.2.2  christos 		if (audio_query_devinfo(sc, &mi) == 0) {
   8638  1.16.2.2  christos 			au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   8639  1.16.2.2  christos 			newgain = gain + mi.un.v.delta;
   8640  1.16.2.2  christos 			if (newgain > AUDIO_MAX_GAIN)
   8641  1.16.2.2  christos 				newgain = AUDIO_MAX_GAIN;
   8642  1.16.2.2  christos 			au_set_gain(sc, &sc->sc_outports, newgain, balance);
   8643  1.16.2.2  christos 		}
   8644  1.16.2.2  christos 	}
   8645  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   8646  1.16.2.2  christos }
   8647  1.16.2.2  christos 
   8648  1.16.2.2  christos static void
   8649  1.16.2.2  christos audio_volume_toggle(device_t dv)
   8650  1.16.2.2  christos {
   8651  1.16.2.2  christos 	struct audio_softc *sc = device_private(dv);
   8652  1.16.2.2  christos 	u_int gain, newgain;
   8653  1.16.2.2  christos 	u_char balance;
   8654  1.16.2.2  christos 
   8655  1.16.2.3    martin 	if (audio_exlock_mutex_enter(sc) != 0)
   8656  1.16.2.2  christos 		return;
   8657  1.16.2.2  christos 	au_get_gain(sc, &sc->sc_outports, &gain, &balance);
   8658  1.16.2.2  christos 	if (gain != 0) {
   8659  1.16.2.2  christos 		sc->sc_lastgain = gain;
   8660  1.16.2.2  christos 		newgain = 0;
   8661  1.16.2.2  christos 	} else
   8662  1.16.2.2  christos 		newgain = sc->sc_lastgain;
   8663  1.16.2.2  christos 	au_set_gain(sc, &sc->sc_outports, newgain, balance);
   8664  1.16.2.3    martin 	audio_exlock_mutex_exit(sc);
   8665  1.16.2.2  christos }
   8666  1.16.2.2  christos 
   8667  1.16.2.3    martin /*
   8668  1.16.2.3    martin  * Must be called with sc_lock held.
   8669  1.16.2.3    martin  */
   8670  1.16.2.2  christos static int
   8671  1.16.2.2  christos audio_query_devinfo(struct audio_softc *sc, mixer_devinfo_t *di)
   8672  1.16.2.2  christos {
   8673  1.16.2.2  christos 
   8674  1.16.2.2  christos 	KASSERT(mutex_owned(sc->sc_lock));
   8675  1.16.2.2  christos 
   8676  1.16.2.2  christos 	return sc->hw_if->query_devinfo(sc->hw_hdl, di);
   8677  1.16.2.2  christos }
   8678  1.16.2.2  christos 
   8679  1.16.2.2  christos #endif /* NAUDIO > 0 */
   8680  1.16.2.2  christos 
   8681  1.16.2.2  christos #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
   8682  1.16.2.2  christos #include <sys/param.h>
   8683  1.16.2.2  christos #include <sys/systm.h>
   8684  1.16.2.2  christos #include <sys/device.h>
   8685  1.16.2.2  christos #include <sys/audioio.h>
   8686  1.16.2.2  christos #include <dev/audio/audio_if.h>
   8687  1.16.2.2  christos #endif
   8688  1.16.2.2  christos 
   8689  1.16.2.2  christos #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
   8690  1.16.2.2  christos int
   8691  1.16.2.2  christos audioprint(void *aux, const char *pnp)
   8692  1.16.2.2  christos {
   8693  1.16.2.2  christos 	struct audio_attach_args *arg;
   8694  1.16.2.2  christos 	const char *type;
   8695  1.16.2.2  christos 
   8696  1.16.2.2  christos 	if (pnp != NULL) {
   8697  1.16.2.2  christos 		arg = aux;
   8698  1.16.2.2  christos 		switch (arg->type) {
   8699  1.16.2.2  christos 		case AUDIODEV_TYPE_AUDIO:
   8700  1.16.2.2  christos 			type = "audio";
   8701  1.16.2.2  christos 			break;
   8702  1.16.2.2  christos 		case AUDIODEV_TYPE_MIDI:
   8703  1.16.2.2  christos 			type = "midi";
   8704  1.16.2.2  christos 			break;
   8705  1.16.2.2  christos 		case AUDIODEV_TYPE_OPL:
   8706  1.16.2.2  christos 			type = "opl";
   8707  1.16.2.2  christos 			break;
   8708  1.16.2.2  christos 		case AUDIODEV_TYPE_MPU:
   8709  1.16.2.2  christos 			type = "mpu";
   8710  1.16.2.2  christos 			break;
   8711  1.16.2.2  christos 		default:
   8712  1.16.2.2  christos 			panic("audioprint: unknown type %d", arg->type);
   8713  1.16.2.2  christos 		}
   8714  1.16.2.2  christos 		aprint_normal("%s at %s", type, pnp);
   8715  1.16.2.2  christos 	}
   8716  1.16.2.2  christos 	return UNCONF;
   8717  1.16.2.2  christos }
   8718  1.16.2.2  christos 
   8719  1.16.2.2  christos #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */
   8720  1.16.2.2  christos 
   8721  1.16.2.2  christos #ifdef _MODULE
   8722  1.16.2.2  christos 
   8723  1.16.2.2  christos devmajor_t audio_bmajor = -1, audio_cmajor = -1;
   8724  1.16.2.2  christos 
   8725  1.16.2.2  christos #include "ioconf.c"
   8726  1.16.2.2  christos 
   8727  1.16.2.2  christos #endif
   8728  1.16.2.2  christos 
   8729  1.16.2.2  christos MODULE(MODULE_CLASS_DRIVER, audio, NULL);
   8730  1.16.2.2  christos 
   8731  1.16.2.2  christos static int
   8732  1.16.2.2  christos audio_modcmd(modcmd_t cmd, void *arg)
   8733  1.16.2.2  christos {
   8734  1.16.2.2  christos 	int error = 0;
   8735  1.16.2.2  christos 
   8736  1.16.2.2  christos 	switch (cmd) {
   8737  1.16.2.2  christos 	case MODULE_CMD_INIT:
   8738  1.16.2.3    martin 		/* XXX interrupt level? */
   8739  1.16.2.3    martin 		audio_psref_class = psref_class_create("audio", IPL_SOFTSERIAL);
   8740  1.16.2.3    martin #ifdef _MODULE
   8741  1.16.2.2  christos 		error = devsw_attach(audio_cd.cd_name, NULL, &audio_bmajor,
   8742  1.16.2.2  christos 		    &audio_cdevsw, &audio_cmajor);
   8743  1.16.2.2  christos 		if (error)
   8744  1.16.2.2  christos 			break;
   8745  1.16.2.2  christos 
   8746  1.16.2.2  christos 		error = config_init_component(cfdriver_ioconf_audio,
   8747  1.16.2.2  christos 		    cfattach_ioconf_audio, cfdata_ioconf_audio);
   8748  1.16.2.2  christos 		if (error) {
   8749  1.16.2.2  christos 			devsw_detach(NULL, &audio_cdevsw);
   8750  1.16.2.2  christos 		}
   8751  1.16.2.3    martin #endif
   8752  1.16.2.2  christos 		break;
   8753  1.16.2.2  christos 	case MODULE_CMD_FINI:
   8754  1.16.2.3    martin #ifdef _MODULE
   8755  1.16.2.2  christos 		devsw_detach(NULL, &audio_cdevsw);
   8756  1.16.2.2  christos 		error = config_fini_component(cfdriver_ioconf_audio,
   8757  1.16.2.2  christos 		   cfattach_ioconf_audio, cfdata_ioconf_audio);
   8758  1.16.2.2  christos 		if (error)
   8759  1.16.2.2  christos 			devsw_attach(audio_cd.cd_name, NULL, &audio_bmajor,
   8760  1.16.2.2  christos 			    &audio_cdevsw, &audio_cmajor);
   8761  1.16.2.3    martin #endif
   8762  1.16.2.3    martin 		psref_class_destroy(audio_psref_class);
   8763  1.16.2.2  christos 		break;
   8764  1.16.2.2  christos 	default:
   8765  1.16.2.2  christos 		error = ENOTTY;
   8766  1.16.2.2  christos 		break;
   8767  1.16.2.2  christos 	}
   8768  1.16.2.2  christos 
   8769  1.16.2.2  christos 	return error;
   8770  1.16.2.2  christos }
   8771