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