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