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