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