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