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