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