xref: /openbsd/sys/dev/audio.c (revision 4fb9ab68)
1 /*	$OpenBSD: audio.c,v 1.207 2024/06/07 08:48:10 jsg Exp $	*/
2 /*
3  * Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include <sys/param.h>
18 #include <sys/fcntl.h>
19 #include <sys/systm.h>
20 #include <sys/ioctl.h>
21 #include <sys/conf.h>
22 #include <sys/kernel.h>
23 #include <sys/event.h>
24 #include <sys/mutex.h>
25 #include <sys/task.h>
26 #include <sys/vnode.h>
27 #include <sys/malloc.h>
28 #include <sys/device.h>
29 #include <sys/audioio.h>
30 #include <dev/audio_if.h>
31 #include <dev/mulaw.h>
32 #include "audio.h"
33 #include "wskbd.h"
34 
35 #ifdef AUDIO_DEBUG
36 #define DPRINTF(...)				\
37 	do {					\
38 		if (audio_debug)		\
39 			printf(__VA_ARGS__);	\
40 	} while(0)
41 #define DPRINTFN(n, ...)			\
42 	do {					\
43 		if (audio_debug > (n))		\
44 			printf(__VA_ARGS__);	\
45 	} while(0)
46 #else
47 #define DPRINTF(...) do {} while(0)
48 #define DPRINTFN(n, ...) do {} while(0)
49 #endif
50 
51 #define DEVNAME(sc)		((sc)->dev.dv_xname)
52 #define AUDIO_UNIT(n)		(minor(n) & 0x0f)
53 #define AUDIO_DEV(n)		(minor(n) & 0xf0)
54 #define AUDIO_DEV_AUDIO		0	/* minor of /dev/audio0 */
55 #define AUDIO_DEV_AUDIOCTL	0xc0	/* minor of /dev/audioctl */
56 #define AUDIO_BUFSZ		65536	/* buffer size in bytes */
57 
58 /*
59  * mixer entries added by the audio(4) layer
60  */
61 #define MIXER_RECORD			0	/* record class */
62 #define MIXER_RECORD_ENABLE		1	/* record.enable control */
63 #define  MIXER_RECORD_ENABLE_OFF	0	/* record.enable=off value */
64 #define  MIXER_RECORD_ENABLE_ON		1	/* record.enable=on value */
65 #define  MIXER_RECORD_ENABLE_SYSCTL	2	/* record.enable=sysctl val */
66 
67 /*
68  * dma buffer
69  */
70 struct audio_buf {
71 	unsigned char *data;		/* DMA memory block */
72 	size_t datalen;			/* size of DMA memory block */
73 	size_t len;			/* size of DMA FIFO */
74 	size_t start;			/* first byte used in the FIFO */
75 	size_t used;			/* bytes used in the FIFO */
76 	size_t blksz;			/* DMA block size */
77 	unsigned int nblks;		/* number of blocks */
78 	struct klist klist;		/* list of knotes */
79 	unsigned int pos;		/* bytes transferred */
80 	unsigned int xrun;		/* bytes lost by xruns */
81 	int blocking;			/* read/write blocking */
82 };
83 
84 #if NWSKBD > 0
85 struct wskbd_vol
86 {
87 	int val;			/* index of the value control */
88 	int mute;			/* index of the mute control */
89 	int step;			/* increment/decrement step */
90 	int nch;			/* channels in the value control */
91 	int val_pending;		/* pending change of val */
92 	int mute_pending;		/* pending change of mute */
93 #define WSKBD_MUTE_TOGGLE	1
94 #define WSKBD_MUTE_DISABLE	2
95 #define WSKBD_MUTE_ENABLE	3
96 };
97 
98 int wskbd_set_mixervolume_unit(int, long, long);
99 #endif
100 
101 /*
102  * event indicating that a control was changed
103  */
104 struct mixer_ev {
105 	struct mixer_ev *next;
106 	int pending;
107 };
108 
109 /*
110  * device structure
111  */
112 struct audio_softc {
113 	struct device dev;
114 	const struct audio_hw_if *ops;	/* driver funcs */
115 	void *cookie;			/* wskbd cookie */
116 	void *arg;			/* first arg to driver funcs */
117 	int mode;			/* bitmask of AUMODE_* */
118 	int quiesce;			/* device suspended */
119 	struct audio_buf play, rec;
120 	unsigned int sw_enc;		/* user exposed AUDIO_ENCODING_* */
121 	unsigned int hw_enc;		/* hardware AUDIO_ENCODING_* */
122 	unsigned int bits;		/* bits per sample */
123 	unsigned int bps;		/* bytes-per-sample */
124 	unsigned int msb;		/* sample are MSB aligned */
125 	unsigned int rate;		/* rate in Hz */
126 	unsigned int round;		/* block size in frames */
127 	unsigned int pchan, rchan;	/* number of channels */
128 	unsigned char silence[4];	/* a sample of silence */
129 	int pause;			/* not trying to start DMA */
130 	int active;			/* DMA in process */
131 	int offs;			/* offset between play & rec dir */
132 	void (*conv_enc)(unsigned char *, int);	/* encode to native */
133 	void (*conv_dec)(unsigned char *, int);	/* decode to user */
134 	struct mixer_ctrl *mix_ents;	/* mixer state for suspend/resume */
135 	int mix_nent;			/* size of mixer state */
136 	int mix_isopen;			/* mixer open for reading */
137 	int mix_blocking;		/* read() blocking */
138 	struct klist mix_klist;		/* list of knotes */
139 	struct mixer_ev *mix_evbuf;	/* per mixer-control event */
140 	struct mixer_ev *mix_pending;	/* list of changed controls */
141 #if NWSKBD > 0
142 	struct wskbd_vol spkr, mic;
143 	struct task wskbd_task;
144 #endif
145 	int record_enable;		/* mixer record.enable value */
146 };
147 
148 int audio_match(struct device *, void *, void *);
149 void audio_attach(struct device *, struct device *, void *);
150 int audio_activate(struct device *, int);
151 int audio_detach(struct device *, int);
152 void audio_pintr(void *);
153 void audio_rintr(void *);
154 void audio_buf_wakeup(struct audio_buf *);
155 void audio_mixer_wakeup(struct audio_softc *);
156 #if NWSKBD > 0
157 void wskbd_mixer_init(struct audio_softc *);
158 void wskbd_mixer_cb(void *);
159 #endif
160 
161 const struct cfattach audio_ca = {
162 	sizeof(struct audio_softc), audio_match, audio_attach,
163 	audio_detach, audio_activate
164 };
165 
166 struct cfdriver audio_cd = {
167 	NULL, "audio", DV_DULL
168 };
169 
170 void filt_audioctlrdetach(struct knote *);
171 int filt_audioctlread(struct knote *, long);
172 int filt_audiomodify(struct kevent *, struct knote *);
173 int filt_audioprocess(struct knote *, struct kevent *);
174 
175 const struct filterops audioctlread_filtops = {
176 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
177 	.f_attach	= NULL,
178 	.f_detach	= filt_audioctlrdetach,
179 	.f_event	= filt_audioctlread,
180 	.f_modify	= filt_audiomodify,
181 	.f_process	= filt_audioprocess,
182 };
183 
184 void filt_audiowdetach(struct knote *);
185 int filt_audiowrite(struct knote *, long);
186 
187 const struct filterops audiowrite_filtops = {
188 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
189 	.f_attach	= NULL,
190 	.f_detach	= filt_audiowdetach,
191 	.f_event	= filt_audiowrite,
192 	.f_modify	= filt_audiomodify,
193 	.f_process	= filt_audioprocess,
194 };
195 
196 void filt_audiordetach(struct knote *);
197 int filt_audioread(struct knote *, long);
198 
199 const struct filterops audioread_filtops = {
200 	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
201 	.f_attach	= NULL,
202 	.f_detach	= filt_audiordetach,
203 	.f_event	= filt_audioread,
204 	.f_modify	= filt_audiomodify,
205 	.f_process	= filt_audioprocess,
206 };
207 
208 /*
209  * This mutex protects data structures (including registers on the
210  * sound-card) that are manipulated by both the interrupt handler and
211  * syscall code-paths.
212  *
213  * Note that driver methods may sleep (e.g. in malloc); consequently the
214  * audio layer calls them with the mutex unlocked. Driver methods are
215  * responsible for locking the mutex when they manipulate data used by
216  * the interrupt handler and interrupts may occur.
217  *
218  * Similarly, the driver is responsible for locking the mutex in its
219  * interrupt handler and to call the audio layer call-backs (i.e.
220  * audio_{p,r}int()) with the mutex locked.
221  */
222 struct mutex audio_lock = MUTEX_INITIALIZER(IPL_AUDIO);
223 
224 /*
225  * Global flag to control if audio recording is enabled when the
226  * mixerctl setting is record.enable=sysctl
227  */
228 int audio_record_enable = 0;
229 
230 #ifdef AUDIO_DEBUG
231 /*
232  * 0 - nothing, as if AUDIO_DEBUG isn't defined
233  * 1 - initialisations & setup
234  * 2 - blocks & interrupts
235  */
236 int audio_debug = 1;
237 #endif
238 
239 unsigned int
240 audio_gcd(unsigned int a, unsigned int b)
241 {
242 	unsigned int r;
243 
244 	while (b > 0) {
245 		r = a % b;
246 		a = b;
247 		b = r;
248 	}
249 	return a;
250 }
251 
252 /*
253  * Calculate the least block size (in frames) such that both the
254  * corresponding play and/or record block sizes (in bytes) are multiple
255  * of the given number of bytes.
256  */
257 int
258 audio_blksz_bytes(int mode,
259 	struct audio_params *p, struct audio_params *r, int bytes)
260 {
261 	unsigned int np, nr;
262 
263 	if (mode & AUMODE_PLAY) {
264 		np = bytes / audio_gcd(p->bps * p->channels, bytes);
265 		if (!(mode & AUMODE_RECORD))
266 			nr = np;
267 	}
268 	if (mode & AUMODE_RECORD) {
269 		nr = bytes / audio_gcd(r->bps * r->channels, bytes);
270 		if (!(mode & AUMODE_PLAY))
271 			np = nr;
272 	}
273 
274 	return nr * np / audio_gcd(nr, np);
275 }
276 
277 void
278 audio_mixer_wakeup(struct audio_softc *sc)
279 {
280 	MUTEX_ASSERT_LOCKED(&audio_lock);
281 
282 	if (sc->mix_blocking) {
283 		wakeup(&sc->mix_blocking);
284 		sc->mix_blocking = 0;
285 	}
286 	knote_locked(&sc->mix_klist, 0);
287 }
288 
289 void
290 audio_buf_wakeup(struct audio_buf *buf)
291 {
292 	MUTEX_ASSERT_LOCKED(&audio_lock);
293 
294 	if (buf->blocking) {
295 		wakeup(&buf->blocking);
296 		buf->blocking = 0;
297 	}
298 	knote_locked(&buf->klist, 0);
299 }
300 
301 int
302 audio_buf_init(struct audio_softc *sc, struct audio_buf *buf, int dir)
303 {
304 	klist_init_mutex(&buf->klist, &audio_lock);
305 	if (sc->ops->round_buffersize) {
306 		buf->datalen = sc->ops->round_buffersize(sc->arg,
307 		    dir, AUDIO_BUFSZ);
308 	} else
309 		buf->datalen = AUDIO_BUFSZ;
310 	if (sc->ops->allocm) {
311 		buf->data = sc->ops->allocm(sc->arg, dir, buf->datalen,
312 		    M_DEVBUF, M_WAITOK);
313 	} else
314 		buf->data = malloc(buf->datalen, M_DEVBUF, M_WAITOK);
315 	if (buf->data == NULL) {
316 		klist_free(&buf->klist);
317 		return ENOMEM;
318 	}
319 	return 0;
320 }
321 
322 void
323 audio_buf_done(struct audio_softc *sc, struct audio_buf *buf)
324 {
325 	if (sc->ops->freem)
326 		sc->ops->freem(sc->arg, buf->data, M_DEVBUF);
327 	else
328 		free(buf->data, M_DEVBUF, buf->datalen);
329 	klist_free(&buf->klist);
330 }
331 
332 /*
333  * return the reader pointer and the number of bytes available
334  */
335 unsigned char *
336 audio_buf_rgetblk(struct audio_buf *buf, size_t *rsize)
337 {
338 	size_t count;
339 
340 	count = buf->len - buf->start;
341 	if (count > buf->used)
342 		count = buf->used;
343 	*rsize = count;
344 	return buf->data + buf->start;
345 }
346 
347 /*
348  * discard "count" bytes at the start position.
349  */
350 void
351 audio_buf_rdiscard(struct audio_buf *buf, size_t count)
352 {
353 #ifdef AUDIO_DEBUG
354 	if (count > buf->used) {
355 		panic("audio_buf_rdiscard: bad count = %zu, "
356 		    "start = %zu, used = %zu", count, buf->start, buf->used);
357 	}
358 #endif
359 	buf->used -= count;
360 	buf->start += count;
361 	if (buf->start >= buf->len)
362 		buf->start -= buf->len;
363 }
364 
365 /*
366  * advance the writer pointer by "count" bytes
367  */
368 void
369 audio_buf_wcommit(struct audio_buf *buf, size_t count)
370 {
371 #ifdef AUDIO_DEBUG
372 	if (count > (buf->len - buf->used)) {
373 		panic("audio_buf_wcommit: bad count = %zu, "
374 		    "start = %zu, used = %zu", count, buf->start, buf->used);
375 	}
376 #endif
377 	buf->used += count;
378 }
379 
380 /*
381  * get writer pointer and the number of bytes writable
382  */
383 unsigned char *
384 audio_buf_wgetblk(struct audio_buf *buf, size_t *rsize)
385 {
386 	size_t end, avail, count;
387 
388 	end = buf->start + buf->used;
389 	if (end >= buf->len)
390 		end -= buf->len;
391 	avail = buf->len - buf->used;
392 	count = buf->len - end;
393 	if (count > avail)
394 		count = avail;
395 	*rsize = count;
396 	return buf->data + end;
397 }
398 
399 void
400 audio_calc_sil(struct audio_softc *sc)
401 {
402 	unsigned char *q;
403 	unsigned int s, i;
404 	int d, e;
405 
406 	e = sc->sw_enc;
407 #ifdef AUDIO_DEBUG
408 	switch (e) {
409 	case AUDIO_ENCODING_SLINEAR_LE:
410 	case AUDIO_ENCODING_ULINEAR_LE:
411 	case AUDIO_ENCODING_SLINEAR_BE:
412 	case AUDIO_ENCODING_ULINEAR_BE:
413 		break;
414 	default:
415 		printf("%s: unhandled play encoding %d\n", DEVNAME(sc), e);
416 		memset(sc->silence, 0, sc->bps);
417 		return;
418 	}
419 #endif
420 	if (e == AUDIO_ENCODING_SLINEAR_BE || e == AUDIO_ENCODING_ULINEAR_BE) {
421 		d = -1;
422 		q = sc->silence + sc->bps - 1;
423 	} else {
424 		d = 1;
425 		q = sc->silence;
426 	}
427 	if (e == AUDIO_ENCODING_SLINEAR_LE || e == AUDIO_ENCODING_SLINEAR_BE) {
428 		s = 0;
429 	} else {
430 		s = 0x80000000;
431 		if (sc->msb)
432 			s >>= 32 - 8 * sc->bps;
433 		else
434 			s >>= 32 - sc->bits;
435 	}
436 	for (i = 0; i < sc->bps; i++) {
437 		*q = s;
438 		q += d;
439 		s >>= 8;
440 	}
441 	if (sc->conv_enc)
442 		sc->conv_enc(sc->silence, sc->bps);
443 }
444 
445 void
446 audio_fill_sil(struct audio_softc *sc, unsigned char *ptr, size_t count)
447 {
448 	unsigned char *q, *p;
449 	size_t i, j;
450 
451 	q = ptr;
452 	for (j = count / sc->bps; j > 0; j--) {
453 		p = sc->silence;
454 		for (i = sc->bps; i > 0; i--)
455 			*q++ = *p++;
456 	}
457 }
458 
459 void
460 audio_clear(struct audio_softc *sc)
461 {
462 	if (sc->mode & AUMODE_PLAY) {
463 		sc->play.used = sc->play.start = 0;
464 		sc->play.pos = sc->play.xrun = 0;
465 		audio_fill_sil(sc, sc->play.data, sc->play.len);
466 	}
467 	if (sc->mode & AUMODE_RECORD) {
468 		sc->rec.used = sc->rec.start = 0;
469 		sc->rec.pos = sc->rec.xrun = 0;
470 		audio_fill_sil(sc, sc->rec.data, sc->rec.len);
471 	}
472 }
473 
474 /*
475  * called whenever a block is consumed by the driver
476  */
477 void
478 audio_pintr(void *addr)
479 {
480 	struct audio_softc *sc = addr;
481 	unsigned char *ptr;
482 	size_t count;
483 	int error, nblk, todo;
484 
485 	MUTEX_ASSERT_LOCKED(&audio_lock);
486 	if (!(sc->mode & AUMODE_PLAY) || !sc->active) {
487 		printf("%s: play interrupt but not playing\n", DEVNAME(sc));
488 		return;
489 	}
490 	if (sc->quiesce) {
491 		DPRINTF("%s: quiesced, skipping play intr\n", DEVNAME(sc));
492 		return;
493 	}
494 
495 	/*
496 	 * check if record pointer wrapped, see explanation
497 	 * in audio_rintr()
498 	 */
499 	if ((sc->mode & AUMODE_RECORD) && sc->ops->underrun == NULL) {
500 		sc->offs--;
501 		nblk = sc->rec.len / sc->rec.blksz;
502 		todo = -sc->offs;
503 		if (todo >= nblk) {
504 			todo -= todo % nblk;
505 			DPRINTFN(1, "%s: rec ptr wrapped, moving %d blocks\n",
506 			    DEVNAME(sc), todo);
507 			while (todo-- > 0)
508 				audio_rintr(sc);
509 		}
510 	}
511 
512 	sc->play.pos += sc->play.blksz;
513 	if (!sc->ops->underrun) {
514 		audio_fill_sil(sc, sc->play.data + sc->play.start,
515 		    sc->play.blksz);
516 	}
517 	audio_buf_rdiscard(&sc->play, sc->play.blksz);
518 	if (sc->play.used < sc->play.blksz) {
519 		DPRINTFN(1, "%s: play underrun\n", DEVNAME(sc));
520 		sc->play.xrun += sc->play.blksz;
521 		audio_buf_wcommit(&sc->play, sc->play.blksz);
522 		if (sc->ops->underrun)
523 			sc->ops->underrun(sc->arg);
524 	}
525 
526 	DPRINTFN(1, "%s: play intr, used -> %zu, start -> %zu\n",
527 	    DEVNAME(sc), sc->play.used, sc->play.start);
528 
529 	if (!sc->ops->trigger_output) {
530 		ptr = audio_buf_rgetblk(&sc->play, &count);
531 		error = sc->ops->start_output(sc->arg,
532 		    ptr, sc->play.blksz, audio_pintr, sc);
533 		if (error) {
534 			printf("%s: play restart failed: %d\n",
535 			    DEVNAME(sc), error);
536 		}
537 	}
538 
539 	if (sc->play.used < sc->play.len) {
540 		DPRINTFN(1, "%s: play wakeup, chan = %d\n",
541 		    DEVNAME(sc), sc->play.blocking);
542 		audio_buf_wakeup(&sc->play);
543 	}
544 }
545 
546 /*
547  * called whenever a block is produced by the driver
548  */
549 void
550 audio_rintr(void *addr)
551 {
552 	struct audio_softc *sc = addr;
553 	unsigned char *ptr;
554 	size_t count;
555 	int error, nblk, todo;
556 
557 	MUTEX_ASSERT_LOCKED(&audio_lock);
558 	if (!(sc->mode & AUMODE_RECORD) || !sc->active) {
559 		printf("%s: rec interrupt but not recording\n", DEVNAME(sc));
560 		return;
561 	}
562 	if (sc->quiesce) {
563 		DPRINTF("%s: quiesced, skipping rec intr\n", DEVNAME(sc));
564 		return;
565 	}
566 
567 	/*
568 	 * Interrupts may be masked by other sub-systems during 320ms
569 	 * and more. During such a delay the hardware doesn't stop
570 	 * playing and the play buffer pointers may wrap, this can't be
571 	 * detected and corrected by low level drivers. This makes the
572 	 * record stream ahead of the play stream; this is detected as a
573 	 * hardware anomaly by userland and cause programs to misbehave.
574 	 *
575 	 * We fix this by advancing play position by an integer count of
576 	 * full buffers, so it reaches the record position.
577 	 */
578 	if ((sc->mode & AUMODE_PLAY) && sc->ops->underrun == NULL) {
579 		sc->offs++;
580 		nblk = sc->play.len / sc->play.blksz;
581 		todo = sc->offs;
582 		if (todo >= nblk) {
583 			todo -= todo % nblk;
584 			DPRINTFN(1, "%s: play ptr wrapped, moving %d blocks\n",
585 			    DEVNAME(sc), todo);
586 			while (todo-- > 0)
587 				audio_pintr(sc);
588 		}
589 	}
590 
591 	sc->rec.pos += sc->rec.blksz;
592 	if ((sc->record_enable == MIXER_RECORD_ENABLE_SYSCTL &&
593 	    !audio_record_enable) ||
594 	    sc->record_enable == MIXER_RECORD_ENABLE_OFF) {
595 		ptr = audio_buf_wgetblk(&sc->rec, &count);
596 		audio_fill_sil(sc, ptr, sc->rec.blksz);
597 	}
598 	audio_buf_wcommit(&sc->rec, sc->rec.blksz);
599 	if (sc->rec.used > sc->rec.len - sc->rec.blksz) {
600 		DPRINTFN(1, "%s: rec overrun\n", DEVNAME(sc));
601 		sc->rec.xrun += sc->rec.blksz;
602 		audio_buf_rdiscard(&sc->rec, sc->rec.blksz);
603 	}
604 	DPRINTFN(1, "%s: rec intr, used -> %zu\n", DEVNAME(sc), sc->rec.used);
605 
606 	if (!sc->ops->trigger_input) {
607 		ptr = audio_buf_wgetblk(&sc->rec, &count);
608 		error = sc->ops->start_input(sc->arg,
609 		    ptr, sc->rec.blksz, audio_rintr, sc);
610 		if (error) {
611 			printf("%s: rec restart failed: %d\n",
612 			    DEVNAME(sc), error);
613 		}
614 	}
615 
616 	if (sc->rec.used > 0) {
617 		DPRINTFN(1, "%s: rec wakeup, chan = %d\n",
618 		    DEVNAME(sc), sc->rec.blocking);
619 		audio_buf_wakeup(&sc->rec);
620 	}
621 }
622 
623 int
624 audio_start_do(struct audio_softc *sc)
625 {
626 	int error;
627 	struct audio_params p;
628 	unsigned char *ptr;
629 	size_t count;
630 
631 	DPRINTF("%s: starting\n", DEVNAME(sc));
632 
633 	error = 0;
634 	sc->offs = 0;
635 	if (sc->mode & AUMODE_PLAY) {
636 		if (sc->ops->trigger_output) {
637 			p.encoding = sc->hw_enc;
638 			p.precision = sc->bits;
639 			p.bps = sc->bps;
640 			p.msb = sc->msb;
641 			p.sample_rate = sc->rate;
642 			p.channels = sc->pchan;
643 			error = sc->ops->trigger_output(sc->arg,
644 			    sc->play.data,
645 			    sc->play.data + sc->play.len,
646 			    sc->play.blksz,
647 			    audio_pintr, sc, &p);
648 		} else {
649 			mtx_enter(&audio_lock);
650 			ptr = audio_buf_rgetblk(&sc->play, &count);
651 			error = sc->ops->start_output(sc->arg,
652 			    ptr, sc->play.blksz, audio_pintr, sc);
653 			mtx_leave(&audio_lock);
654 		}
655 		if (error)
656 			printf("%s: failed to start playback\n", DEVNAME(sc));
657 	}
658 	if (sc->mode & AUMODE_RECORD) {
659 		if (sc->ops->trigger_input) {
660 			p.encoding = sc->hw_enc;
661 			p.precision = sc->bits;
662 			p.bps = sc->bps;
663 			p.msb = sc->msb;
664 			p.sample_rate = sc->rate;
665 			p.channels = sc->rchan;
666 			error = sc->ops->trigger_input(sc->arg,
667 			    sc->rec.data,
668 			    sc->rec.data + sc->rec.len,
669 			    sc->rec.blksz,
670 			    audio_rintr, sc, &p);
671 		} else {
672 			mtx_enter(&audio_lock);
673 			ptr = audio_buf_wgetblk(&sc->rec, &count);
674 			error = sc->ops->start_input(sc->arg,
675 			    ptr, sc->rec.blksz, audio_rintr, sc);
676 			mtx_leave(&audio_lock);
677 		}
678 		if (error)
679 			printf("%s: failed to start recording\n", DEVNAME(sc));
680 	}
681 	return error;
682 }
683 
684 int
685 audio_stop_do(struct audio_softc *sc)
686 {
687 	if (sc->mode & AUMODE_PLAY)
688 		sc->ops->halt_output(sc->arg);
689 	if (sc->mode & AUMODE_RECORD)
690 		sc->ops->halt_input(sc->arg);
691 	DPRINTF("%s: stopped\n", DEVNAME(sc));
692 	return 0;
693 }
694 
695 int
696 audio_start(struct audio_softc *sc)
697 {
698 	sc->active = 1;
699 	sc->play.xrun = sc->play.pos = sc->rec.xrun = sc->rec.pos = 0;
700 	return audio_start_do(sc);
701 }
702 
703 int
704 audio_stop(struct audio_softc *sc)
705 {
706 	int error;
707 
708 	error = audio_stop_do(sc);
709 	if (error)
710 		return error;
711 	audio_clear(sc);
712 	sc->active = 0;
713 	return 0;
714 }
715 
716 int
717 audio_canstart(struct audio_softc *sc)
718 {
719 	if (sc->active || sc->pause)
720 		return 0;
721 	if ((sc->mode & AUMODE_RECORD) && sc->rec.used != 0)
722 		return 0;
723 	if ((sc->mode & AUMODE_PLAY) && sc->play.used != sc->play.len)
724 		return 0;
725 	return 1;
726 }
727 
728 int
729 audio_setpar_blksz(struct audio_softc *sc,
730     struct audio_params *p, struct audio_params *r)
731 {
732 	unsigned int nr, np, max, min, mult;
733 	unsigned int blk_mult, blk_max;
734 
735 	if (sc->ops->set_blksz) {
736 		/*
737 		 * Don't allow block size of exceed half the buffer size
738 		 */
739 		if (sc->mode & AUMODE_PLAY) {
740 			max = sc->play.datalen / 2 / (sc->pchan * sc->bps);
741 			if (sc->round > max)
742 				sc->round = max;
743 		}
744 		if (sc->mode & AUMODE_RECORD) {
745 			max = sc->rec.datalen / 2 / (sc->rchan * sc->bps);
746 			if (sc->round > max)
747 				sc->round = max;
748 		}
749 
750 		sc->round = sc->ops->set_blksz(sc->arg, sc->mode,
751 		    p, r, sc->round);
752 
753 		DPRINTF("%s: block size set to: %u\n", DEVNAME(sc), sc->round);
754 		return 0;
755 	}
756 
757 	/*
758 	 * get least multiplier of the number of frames per block
759 	 */
760 	if (sc->ops->round_blocksize) {
761 		blk_mult = sc->ops->round_blocksize(sc->arg, 1);
762 		if (blk_mult == 0) {
763 			printf("%s: 0x%x: bad block size multiplier\n",
764 			    DEVNAME(sc), blk_mult);
765 			return ENODEV;
766 		}
767 	} else
768 		blk_mult = 1;
769 	DPRINTF("%s: hw block size multiplier: %u\n", DEVNAME(sc), blk_mult);
770 	if (sc->mode & AUMODE_PLAY) {
771 		np = blk_mult / audio_gcd(sc->pchan * sc->bps, blk_mult);
772 		if (!(sc->mode & AUMODE_RECORD))
773 			nr = np;
774 		DPRINTF("%s: play number of frames multiplier: %u\n",
775 		    DEVNAME(sc), np);
776 	}
777 	if (sc->mode & AUMODE_RECORD) {
778 		nr = blk_mult / audio_gcd(sc->rchan * sc->bps, blk_mult);
779 		if (!(sc->mode & AUMODE_PLAY))
780 			np = nr;
781 		DPRINTF("%s: record number of frames multiplier: %u\n",
782 		    DEVNAME(sc), nr);
783 	}
784 	mult = nr * np / audio_gcd(nr, np);
785 	DPRINTF("%s: least common number of frames multiplier: %u\n",
786 	    DEVNAME(sc), mult);
787 
788 	/*
789 	 * get minimum and maximum frames per block
790 	 */
791 	if (sc->ops->round_blocksize)
792 		blk_max = sc->ops->round_blocksize(sc->arg, AUDIO_BUFSZ);
793 	else
794 		blk_max = AUDIO_BUFSZ;
795 	if ((sc->mode & AUMODE_PLAY) && blk_max > sc->play.datalen / 2)
796 		blk_max = sc->play.datalen / 2;
797 	if ((sc->mode & AUMODE_RECORD) && blk_max > sc->rec.datalen / 2)
798 		blk_max = sc->rec.datalen / 2;
799 	if (sc->mode & AUMODE_PLAY) {
800 		np = blk_max / (sc->pchan * sc->bps);
801 		if (!(sc->mode & AUMODE_RECORD))
802 			nr = np;
803 	}
804 	if (sc->mode & AUMODE_RECORD) {
805 		nr = blk_max / (sc->rchan * sc->bps);
806 		if (!(sc->mode & AUMODE_PLAY))
807 			np = nr;
808 	}
809 	max = np < nr ? np : nr;
810 	max -= max % mult;
811 	min = sc->rate / 1000 + mult - 1;
812 	min -= min % mult;
813 	DPRINTF("%s: frame number range: %u..%u\n", DEVNAME(sc), min, max);
814 	if (max < min) {
815 		printf("%s: %u: bad max frame number\n", DEVNAME(sc), max);
816 		return EIO;
817 	}
818 
819 	/*
820 	 * adjust the frame per block to match our constraints
821 	 */
822 	sc->round += mult / 2;
823 	sc->round -= sc->round % mult;
824 	if (sc->round > max)
825 		sc->round = max;
826 	else if (sc->round < min)
827 		sc->round = min;
828 
829 	return 0;
830 }
831 
832 int
833 audio_setpar_nblks(struct audio_softc *sc,
834     struct audio_params *p, struct audio_params *r)
835 {
836 	unsigned int max;
837 
838 	/*
839 	 * set buffer size (number of blocks)
840 	 */
841 	if (sc->mode & AUMODE_PLAY) {
842 		max = sc->play.datalen / (sc->round * sc->pchan * sc->bps);
843 		if (sc->play.nblks > max)
844 			sc->play.nblks = max;
845 		else if (sc->play.nblks < 2)
846 			sc->play.nblks = 2;
847 		if (sc->ops->set_nblks) {
848 			sc->play.nblks = sc->ops->set_nblks(sc->arg, sc->mode,
849 			    p, sc->round, sc->play.nblks);
850 			DPRINTF("%s: play nblks -> %u\n", DEVNAME(sc),
851 			    sc->play.nblks);
852 		}
853 	}
854 	if (sc->mode & AUMODE_RECORD) {
855 		/*
856 		 * for recording, buffer size is not the latency (it's
857 		 * exactly one block), so let's get the maximum buffer
858 		 * size of maximum reliability during xruns
859 		 */
860 		max = sc->rec.datalen / (sc->round * sc->rchan * sc->bps);
861 		if (sc->ops->set_nblks) {
862 			max = sc->ops->set_nblks(sc->arg, sc->mode,
863 			    r, sc->round, max);
864 			DPRINTF("%s: rec nblks -> %u\n", DEVNAME(sc), max);
865 		}
866 		sc->rec.nblks = max;
867 	}
868 	return 0;
869 }
870 
871 int
872 audio_setpar(struct audio_softc *sc)
873 {
874 	struct audio_params p, r;
875 	int error;
876 
877 	DPRINTF("%s: setpar: req enc=%d bits=%d, bps=%d, msb=%d "
878 	    "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
879 	    DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
880 	    sc->rate, sc->pchan, sc->rchan, sc->round, sc->play.nblks);
881 
882 	/*
883 	 * check if requested parameters are in the allowed ranges
884 	 */
885 	if (sc->mode & AUMODE_PLAY) {
886 		if (sc->pchan < 1)
887 			sc->pchan = 1;
888 		else if (sc->pchan > 64)
889 			sc->pchan = 64;
890 	}
891 	if (sc->mode & AUMODE_RECORD) {
892 		if (sc->rchan < 1)
893 			sc->rchan = 1;
894 		else if (sc->rchan > 64)
895 			sc->rchan = 64;
896 	}
897 	switch (sc->sw_enc) {
898 	case AUDIO_ENCODING_ULAW:
899 	case AUDIO_ENCODING_ALAW:
900 	case AUDIO_ENCODING_SLINEAR_LE:
901 	case AUDIO_ENCODING_SLINEAR_BE:
902 	case AUDIO_ENCODING_ULINEAR_LE:
903 	case AUDIO_ENCODING_ULINEAR_BE:
904 		break;
905 	default:
906 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
907 	}
908 	if (sc->bits < 8)
909 		sc->bits = 8;
910 	else if (sc->bits > 32)
911 		sc->bits = 32;
912 	if (sc->bps < 1)
913 		sc->bps = 1;
914 	else if (sc->bps > 4)
915 		sc->bps = 4;
916 	if (sc->rate < 4000)
917 		sc->rate = 4000;
918 	else if (sc->rate > 192000)
919 		sc->rate = 192000;
920 
921 	/*
922 	 * copy into struct audio_params, required by drivers
923 	 */
924 	p.encoding = r.encoding = sc->sw_enc;
925 	p.precision = r.precision = sc->bits;
926 	p.bps = r.bps = sc->bps;
927 	p.msb = r.msb = sc->msb;
928 	p.sample_rate = r.sample_rate = sc->rate;
929 	p.channels = sc->pchan;
930 	r.channels = sc->rchan;
931 
932 	/*
933 	 * set parameters
934 	 */
935 	error = sc->ops->set_params(sc->arg, sc->mode, sc->mode, &p, &r);
936 	if (error)
937 		return error;
938 	if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) {
939 		if (p.encoding != r.encoding ||
940 		    p.precision != r.precision ||
941 		    p.bps != r.bps ||
942 		    p.msb != r.msb ||
943 		    p.sample_rate != r.sample_rate) {
944 			printf("%s: different play and record parameters "
945 			    "returned by hardware\n", DEVNAME(sc));
946 			return ENODEV;
947 		}
948 	}
949 	if (sc->mode & AUMODE_PLAY) {
950 		sc->hw_enc = p.encoding;
951 		sc->bits = p.precision;
952 		sc->bps = p.bps;
953 		sc->msb = p.msb;
954 		sc->rate = p.sample_rate;
955 		sc->pchan = p.channels;
956 	}
957 	if (sc->mode & AUMODE_RECORD) {
958 		sc->hw_enc = r.encoding;
959 		sc->bits = r.precision;
960 		sc->bps = r.bps;
961 		sc->msb = r.msb;
962 		sc->rate = r.sample_rate;
963 		sc->rchan = r.channels;
964 	}
965 	if (sc->rate == 0 || sc->bps == 0 || sc->bits == 0) {
966 		printf("%s: invalid parameters returned by hardware\n",
967 		    DEVNAME(sc));
968 		return ENODEV;
969 	}
970 	if (sc->ops->commit_settings) {
971 		error = sc->ops->commit_settings(sc->arg);
972 		if (error)
973 			return error;
974 	}
975 
976 	/*
977 	 * conversion from/to exotic/dead encoding, for drivers not supporting
978 	 * linear
979 	 */
980 	switch (sc->hw_enc) {
981 	case AUDIO_ENCODING_SLINEAR_LE:
982 	case AUDIO_ENCODING_SLINEAR_BE:
983 	case AUDIO_ENCODING_ULINEAR_LE:
984 	case AUDIO_ENCODING_ULINEAR_BE:
985 		sc->sw_enc = sc->hw_enc;
986 		sc->conv_dec = sc->conv_enc = NULL;
987 		break;
988 	case AUDIO_ENCODING_ULAW:
989 #if BYTE_ORDER == LITTLE_ENDIAN
990 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
991 #else
992 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
993 #endif
994 		if (sc->bits == 8) {
995 			sc->conv_enc = slinear8_to_mulaw;
996 			sc->conv_dec = mulaw_to_slinear8;
997 		} else if (sc->bits == 24) {
998 			sc->conv_enc = slinear24_to_mulaw24;
999 			sc->conv_dec = mulaw24_to_slinear24;
1000 		} else {
1001 			sc->sw_enc = sc->hw_enc;
1002 			sc->conv_dec = sc->conv_enc = NULL;
1003 		}
1004 		break;
1005 	default:
1006 		printf("%s: setpar: enc = %d, bits = %d: emulation skipped\n",
1007 		    DEVNAME(sc), sc->hw_enc, sc->bits);
1008 		sc->sw_enc = sc->hw_enc;
1009 		sc->conv_dec = sc->conv_enc = NULL;
1010 	}
1011 	audio_calc_sil(sc);
1012 
1013 	error = audio_setpar_blksz(sc, &p, &r);
1014 	if (error)
1015 		return error;
1016 
1017 	error = audio_setpar_nblks(sc, &p, &r);
1018 	if (error)
1019 		return error;
1020 
1021 	/*
1022 	 * set buffer
1023 	 */
1024 	if (sc->mode & AUMODE_PLAY) {
1025 		sc->play.blksz = sc->round * sc->pchan * sc->bps;
1026 		sc->play.len = sc->play.nblks * sc->play.blksz;
1027 	}
1028 	if (sc->mode & AUMODE_RECORD) {
1029 		sc->rec.blksz = sc->round * sc->rchan * sc->bps;
1030 		sc->rec.len = sc->rec.nblks * sc->rec.blksz;
1031 	}
1032 
1033 	DPRINTF("%s: setpar: new enc=%d bits=%d, bps=%d, msb=%d "
1034 	    "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
1035 	    DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
1036 	    sc->rate, sc->pchan, sc->rchan, sc->round, sc->play.nblks);
1037 	return 0;
1038 }
1039 
1040 int
1041 audio_ioc_start(struct audio_softc *sc)
1042 {
1043 	if (!sc->pause) {
1044 		DPRINTF("%s: can't start: already started\n", DEVNAME(sc));
1045 		return EBUSY;
1046 	}
1047 	if ((sc->mode & AUMODE_PLAY) && sc->play.used != sc->play.len) {
1048 		DPRINTF("%s: play buffer not ready\n", DEVNAME(sc));
1049 		return EBUSY;
1050 	}
1051 	if ((sc->mode & AUMODE_RECORD) && sc->rec.used != 0) {
1052 		DPRINTF("%s: record buffer not ready\n", DEVNAME(sc));
1053 		return EBUSY;
1054 	}
1055 	sc->pause = 0;
1056 	return audio_start(sc);
1057 }
1058 
1059 int
1060 audio_ioc_stop(struct audio_softc *sc)
1061 {
1062 	if (sc->pause) {
1063 		DPRINTF("%s: can't stop: not started\n", DEVNAME(sc));
1064 		return EBUSY;
1065 	}
1066 	sc->pause = 1;
1067 	if (sc->active)
1068 		return audio_stop(sc);
1069 	return 0;
1070 }
1071 
1072 int
1073 audio_ioc_getpar(struct audio_softc *sc, struct audio_swpar *p)
1074 {
1075 	p->rate = sc->rate;
1076 	p->sig = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE ||
1077 	    sc->sw_enc == AUDIO_ENCODING_SLINEAR_BE;
1078 	p->le = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE ||
1079 	    sc->sw_enc == AUDIO_ENCODING_ULINEAR_LE;
1080 	p->bits = sc->bits;
1081 	p->bps = sc->bps;
1082 	p->msb = sc->msb;
1083 	p->pchan = sc->pchan;
1084 	p->rchan = sc->rchan;
1085 	p->nblks = sc->play.nblks;
1086 	p->round = sc->round;
1087 	return 0;
1088 }
1089 
1090 int
1091 audio_ioc_setpar(struct audio_softc *sc, struct audio_swpar *p)
1092 {
1093 	int error, le, sig;
1094 
1095 	if (sc->active) {
1096 		DPRINTF("%s: can't change params during dma\n",
1097 		    DEVNAME(sc));
1098 		return EBUSY;
1099 	}
1100 
1101 	/*
1102 	 * copy desired parameters into the softc structure
1103 	 */
1104 	if (p->sig != ~0U || p->le != ~0U || p->bits != ~0U) {
1105 		sig = 1;
1106 		le = (BYTE_ORDER == LITTLE_ENDIAN);
1107 		sc->bits = 16;
1108 		sc->bps = 2;
1109 		sc->msb = 1;
1110 		if (p->sig != ~0U)
1111 			sig = p->sig;
1112 		if (p->le != ~0U)
1113 			le = p->le;
1114 		if (p->bits != ~0U) {
1115 			sc->bits = p->bits;
1116 			sc->bps = sc->bits <= 8 ?
1117 			    1 : (sc->bits <= 16 ? 2 : 4);
1118 			if (p->bps != ~0U)
1119 				sc->bps = p->bps;
1120 			if (p->msb != ~0U)
1121 				sc->msb = p->msb ? 1 : 0;
1122 		}
1123 		sc->sw_enc = (sig) ?
1124 		    (le ? AUDIO_ENCODING_SLINEAR_LE :
1125 			AUDIO_ENCODING_SLINEAR_BE) :
1126 		    (le ? AUDIO_ENCODING_ULINEAR_LE :
1127 			AUDIO_ENCODING_ULINEAR_BE);
1128 	}
1129 	if (p->rate != ~0)
1130 		sc->rate = p->rate;
1131 	if (p->pchan != ~0)
1132 		sc->pchan = p->pchan;
1133 	if (p->rchan != ~0)
1134 		sc->rchan = p->rchan;
1135 	if (p->round != ~0)
1136 		sc->round = p->round;
1137 	if (p->nblks != ~0)
1138 		sc->play.nblks = p->nblks;
1139 
1140 	/*
1141 	 * if the device is not opened for playback or recording don't
1142 	 * touch the hardware yet (ex. if this is /dev/audioctlN)
1143 	 */
1144 	if (sc->mode == 0)
1145 		return 0;
1146 
1147 	/*
1148 	 * negotiate parameters with the hardware
1149 	 */
1150 	error = audio_setpar(sc);
1151 	if (error)
1152 		return error;
1153 	audio_clear(sc);
1154 	if ((sc->mode & AUMODE_PLAY) && sc->ops->init_output) {
1155 		error = sc->ops->init_output(sc->arg,
1156 		    sc->play.data, sc->play.len);
1157 		if (error)
1158 			return error;
1159 	}
1160 	if ((sc->mode & AUMODE_RECORD) && sc->ops->init_input) {
1161 		error = sc->ops->init_input(sc->arg,
1162 		    sc->rec.data, sc->rec.len);
1163 		if (error)
1164 			return error;
1165 	}
1166 	return 0;
1167 }
1168 
1169 int
1170 audio_ioc_getstatus(struct audio_softc *sc, struct audio_status *p)
1171 {
1172 	p->mode = sc->mode;
1173 	p->pause = sc->pause;
1174 	p->active = sc->active;
1175 	return 0;
1176 }
1177 
1178 int
1179 audio_match(struct device *parent, void *match, void *aux)
1180 {
1181 	struct audio_attach_args *sa = aux;
1182 
1183 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
1184 }
1185 
1186 void
1187 audio_attach(struct device *parent, struct device *self, void *aux)
1188 {
1189 	struct audio_softc *sc = (void *)self;
1190 	struct audio_attach_args *sa = aux;
1191 	const struct audio_hw_if *ops = sa->hwif;
1192 	struct mixer_devinfo *mi;
1193 	struct mixer_ctrl *ent;
1194 	void *arg = sa->hdl;
1195 	int error;
1196 
1197 	printf("\n");
1198 
1199 #ifdef DIAGNOSTIC
1200 	if (ops == 0 ||
1201 	    ops->open == 0 ||
1202 	    ops->close == 0 ||
1203 	    ops->set_params == 0 ||
1204 	    (ops->start_output == 0 && ops->trigger_output == 0) ||
1205 	    (ops->start_input == 0 && ops->trigger_input == 0) ||
1206 	    ops->halt_output == 0 ||
1207 	    ops->halt_input == 0 ||
1208 	    ops->set_port == 0 ||
1209 	    ops->get_port == 0 ||
1210 	    ops->query_devinfo == 0) {
1211 		printf("%s: missing method\n", DEVNAME(sc));
1212 		sc->ops = 0;
1213 		return;
1214 	}
1215 #endif
1216 	sc->ops = ops;
1217 	sc->cookie = sa->cookie;
1218 	sc->arg = arg;
1219 
1220 #if NWSKBD > 0
1221 	wskbd_mixer_init(sc);
1222 #endif /* NWSKBD > 0 */
1223 
1224 	error = audio_buf_init(sc, &sc->play, AUMODE_PLAY);
1225 	if (error) {
1226 		sc->ops = 0;
1227 		printf("%s: could not allocate play buffer\n", DEVNAME(sc));
1228 		return;
1229 	}
1230 	error = audio_buf_init(sc, &sc->rec, AUMODE_RECORD);
1231 	if (error) {
1232 		audio_buf_done(sc, &sc->play);
1233 		sc->ops = 0;
1234 		printf("%s: could not allocate record buffer\n", DEVNAME(sc));
1235 		return;
1236 	}
1237 
1238 	klist_init_mutex(&sc->mix_klist, &audio_lock);
1239 
1240 	/* set defaults */
1241 #if BYTE_ORDER == LITTLE_ENDIAN
1242 	sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
1243 #else
1244 	sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
1245 #endif
1246 	sc->bits = 16;
1247 	sc->bps = 2;
1248 	sc->msb = 1;
1249 	sc->rate = 48000;
1250 	sc->pchan = 2;
1251 	sc->rchan = 2;
1252 	sc->round = 960;
1253 	sc->play.nblks = 2;
1254 	sc->play.pos = sc->play.xrun = sc->rec.pos = sc->rec.xrun = 0;
1255 	sc->record_enable = MIXER_RECORD_ENABLE_SYSCTL;
1256 
1257 	/*
1258 	 * allocate an array of mixer_ctrl structures to save the
1259 	 * mixer state and prefill them.
1260 	 */
1261 
1262 	mi = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK);
1263 
1264 	mi->index = 0;
1265 	while (1) {
1266 		if (sc->ops->query_devinfo(sc->arg, mi) != 0)
1267 			break;
1268 		mi->index++;
1269 	}
1270 	sc->mix_nent = mi->index;
1271 	sc->mix_ents = mallocarray(sc->mix_nent,
1272 	    sizeof(struct mixer_ctrl), M_DEVBUF, M_WAITOK);
1273 	sc->mix_evbuf = mallocarray(sc->mix_nent,
1274 	    sizeof(struct mixer_ev), M_DEVBUF, M_WAITOK | M_ZERO);
1275 
1276 	ent = sc->mix_ents;
1277 	mi->index = 0;
1278 	while (1) {
1279 		if (sc->ops->query_devinfo(sc->arg, mi) != 0)
1280 			break;
1281 		switch (mi->type) {
1282 		case AUDIO_MIXER_VALUE:
1283 			ent->un.value.num_channels = mi->un.v.num_channels;
1284 			/* FALLTHROUGH */
1285 		case AUDIO_MIXER_SET:
1286 		case AUDIO_MIXER_ENUM:
1287 			ent->dev = mi->index;
1288 			ent->type = mi->type;
1289 		}
1290 		mi->index++;
1291 		ent++;
1292 	}
1293 
1294 	free(mi, M_TEMP, sizeof(struct mixer_devinfo));
1295 }
1296 
1297 int
1298 audio_activate(struct device *self, int act)
1299 {
1300 	struct audio_softc *sc = (struct audio_softc *)self;
1301 	int i;
1302 
1303 	switch (act) {
1304 	case DVACT_QUIESCE:
1305 		/*
1306 		 * good drivers run play and rec handlers in a single
1307 		 * interrupt. Grab the lock to ensure we expose the same
1308 		 * sc->quiesce value to both play and rec handlers
1309 		 */
1310 		mtx_enter(&audio_lock);
1311 		sc->quiesce = 1;
1312 		mtx_leave(&audio_lock);
1313 
1314 		/*
1315 		 * once sc->quiesce is set, interrupts may occur, but
1316 		 * counters are not advanced and consequently processes
1317 		 * keep sleeping.
1318 		 *
1319 		 * XXX: ensure read/write/ioctl don't start/stop
1320 		 * DMA at the same time, this needs a "ready" condvar
1321 		 */
1322 		if (sc->mode != 0 && sc->active)
1323 			audio_stop_do(sc);
1324 
1325 		/*
1326 		 * save mixer state
1327 		 */
1328 		for (i = 0; i != sc->mix_nent; i++)
1329 			sc->ops->get_port(sc->arg, sc->mix_ents + i);
1330 
1331 		DPRINTF("%s: quiesce: active = %d\n", DEVNAME(sc), sc->active);
1332 		break;
1333 	case DVACT_WAKEUP:
1334 		DPRINTF("%s: wakeup: active = %d\n", DEVNAME(sc), sc->active);
1335 
1336 		/*
1337 		 * restore mixer state
1338 		 */
1339 		for (i = 0; i != sc->mix_nent; i++)
1340 			sc->ops->set_port(sc->arg, sc->mix_ents + i);
1341 
1342 		/*
1343 		 * keep buffer usage the same, but set start pointer to
1344 		 * the beginning of the buffer.
1345 		 *
1346 		 * No need to grab the audio_lock as DMA is stopped and
1347 		 * this is the only thread running (caller ensures this)
1348 		 */
1349 		sc->quiesce = 0;
1350 		wakeup(&sc->quiesce);
1351 
1352 		if (sc->mode != 0) {
1353 			if (audio_setpar(sc) != 0)
1354 				break;
1355 			if (sc->mode & AUMODE_PLAY) {
1356 				sc->play.start = 0;
1357 				audio_fill_sil(sc, sc->play.data, sc->play.len);
1358 			}
1359 			if (sc->mode & AUMODE_RECORD) {
1360 				sc->rec.start = sc->rec.len - sc->rec.used;
1361 				audio_fill_sil(sc, sc->rec.data, sc->rec.len);
1362 			}
1363 			if (sc->active)
1364 				audio_start_do(sc);
1365 		}
1366 		break;
1367 	}
1368 	return 0;
1369 }
1370 
1371 int
1372 audio_detach(struct device *self, int flags)
1373 {
1374 	struct audio_softc *sc = (struct audio_softc *)self;
1375 	int maj, mn;
1376 
1377 	DPRINTF("%s: audio_detach: flags = %d\n", DEVNAME(sc), flags);
1378 
1379 	wakeup(&sc->quiesce);
1380 
1381 	/* locate the major number */
1382 	for (maj = 0; maj < nchrdev; maj++)
1383 		if (cdevsw[maj].d_open == audioopen)
1384 			break;
1385 	/*
1386 	 * Nuke the vnodes for any open instances, calls close but as
1387 	 * close uses device_lookup, it returns EXIO and does nothing
1388 	 */
1389 	mn = self->dv_unit;
1390 	vdevgone(maj, mn | AUDIO_DEV_AUDIO, mn | AUDIO_DEV_AUDIO, VCHR);
1391 	vdevgone(maj, mn | AUDIO_DEV_AUDIOCTL, mn | AUDIO_DEV_AUDIOCTL, VCHR);
1392 
1393 	/*
1394 	 * The close() method did nothing, quickly halt DMA (normally
1395 	 * parent is already gone, and code below is no-op), and wake-up
1396 	 * user-land blocked in read/write/ioctl, which return EIO.
1397 	 */
1398 	if (sc->mode != 0) {
1399 		if (sc->active) {
1400 			wakeup(&sc->play.blocking);
1401 			wakeup(&sc->rec.blocking);
1402 			audio_stop(sc);
1403 		}
1404 		sc->ops->close(sc->arg);
1405 		sc->mode = 0;
1406 	}
1407 	if (sc->mix_isopen)
1408 		wakeup(&sc->mix_blocking);
1409 	klist_invalidate(&sc->play.klist);
1410 	klist_invalidate(&sc->rec.klist);
1411 	klist_invalidate(&sc->mix_klist);
1412 
1413 	/* free resources */
1414 	klist_free(&sc->mix_klist);
1415 	free(sc->mix_evbuf, M_DEVBUF, sc->mix_nent * sizeof(struct mixer_ev));
1416 	free(sc->mix_ents, M_DEVBUF, sc->mix_nent * sizeof(struct mixer_ctrl));
1417 	audio_buf_done(sc, &sc->play);
1418 	audio_buf_done(sc, &sc->rec);
1419 	return 0;
1420 }
1421 
1422 int
1423 audio_submatch(struct device *parent, void *match, void *aux)
1424 {
1425         struct cfdata *cf = match;
1426 
1427 	return (cf->cf_driver == &audio_cd);
1428 }
1429 
1430 struct device *
1431 audio_attach_mi(const struct audio_hw_if *ops, void *arg, void *cookie,
1432     struct device *dev)
1433 {
1434 	struct audio_attach_args aa;
1435 
1436 	aa.type = AUDIODEV_TYPE_AUDIO;
1437 	aa.hwif = ops;
1438 	aa.hdl = arg;
1439 	aa.cookie = cookie;
1440 
1441 	/*
1442 	 * attach this driver to the caller (hardware driver), this
1443 	 * checks the kernel config and possibly calls audio_attach()
1444 	 */
1445 	return config_found_sm(dev, &aa, audioprint, audio_submatch);
1446 }
1447 
1448 int
1449 audioprint(void *aux, const char *pnp)
1450 {
1451 	struct audio_attach_args *arg = aux;
1452 	const char *type;
1453 
1454 	if (pnp != NULL) {
1455 		switch (arg->type) {
1456 		case AUDIODEV_TYPE_AUDIO:
1457 			type = "audio";
1458 			break;
1459 		case AUDIODEV_TYPE_OPL:
1460 			type = "opl";
1461 			break;
1462 		case AUDIODEV_TYPE_MPU:
1463 			type = "mpu";
1464 			break;
1465 		default:
1466 			panic("audioprint: unknown type %d", arg->type);
1467 		}
1468 		printf("%s at %s", type, pnp);
1469 	}
1470 	return UNCONF;
1471 }
1472 
1473 int
1474 audio_open(struct audio_softc *sc, int flags)
1475 {
1476 	int error;
1477 
1478 	if (sc->mode)
1479 		return EBUSY;
1480 	error = sc->ops->open(sc->arg, flags);
1481 	if (error)
1482 		return error;
1483 	sc->active = 0;
1484 	sc->pause = 1;
1485 	sc->rec.blocking = 0;
1486 	sc->play.blocking = 0;
1487 	sc->mode = 0;
1488 	if (flags & FWRITE)
1489 		sc->mode |= AUMODE_PLAY;
1490 	if (flags & FREAD)
1491 		sc->mode |= AUMODE_RECORD;
1492 
1493 	error = audio_setpar(sc);
1494 	if (error)
1495 		goto bad;
1496 	audio_clear(sc);
1497 
1498 	/*
1499 	 * allow read(2)/write(2) to automatically start DMA, without
1500 	 * the need for ioctl(), to make /dev/audio usable in scripts
1501 	 */
1502 	sc->pause = 0;
1503 	return 0;
1504 bad:
1505 	sc->ops->close(sc->arg);
1506 	sc->mode = 0;
1507 	return error;
1508 }
1509 
1510 int
1511 audio_drain(struct audio_softc *sc)
1512 {
1513 	int error, xrun;
1514 	unsigned char *ptr;
1515 	size_t count, bpf;
1516 
1517 	DPRINTF("%s: drain: mode = %d, pause = %d, active = %d, used = %zu\n",
1518 	    DEVNAME(sc), sc->mode, sc->pause, sc->active, sc->play.used);
1519 	if (!(sc->mode & AUMODE_PLAY) || sc->pause)
1520 		return 0;
1521 
1522 	/* discard partial samples, required by audio_fill_sil() */
1523 	mtx_enter(&audio_lock);
1524 	bpf = sc->pchan * sc->bps;
1525 	sc->play.used -= sc->play.used % bpf;
1526 	if (sc->play.used == 0) {
1527 		mtx_leave(&audio_lock);
1528 		return 0;
1529 	}
1530 
1531 	if (!sc->active) {
1532 		/*
1533 		 * dma not started yet because buffer was not full
1534 		 * enough to start automatically. Pad it and start now.
1535 		 */
1536 		for (;;) {
1537 			ptr = audio_buf_wgetblk(&sc->play, &count);
1538 			if (count == 0)
1539 				break;
1540 			audio_fill_sil(sc, ptr, count);
1541 			audio_buf_wcommit(&sc->play, count);
1542 		}
1543 		mtx_leave(&audio_lock);
1544 		error = audio_start(sc);
1545 		if (error)
1546 			return error;
1547 		mtx_enter(&audio_lock);
1548 	}
1549 
1550 	xrun = sc->play.xrun;
1551 	while (sc->play.xrun == xrun) {
1552 		DPRINTF("%s: drain: used = %zu, xrun = %d\n",
1553 		    DEVNAME(sc), sc->play.used, sc->play.xrun);
1554 
1555 		/*
1556 		 * set a 5 second timeout, in case interrupts don't
1557 		 * work, useful only for debugging drivers
1558 		 */
1559 		sc->play.blocking = 1;
1560 		error = msleep_nsec(&sc->play.blocking, &audio_lock,
1561 		    PWAIT | PCATCH, "au_dr", SEC_TO_NSEC(5));
1562 		if (!(sc->dev.dv_flags & DVF_ACTIVE))
1563 			error = EIO;
1564 		if (error) {
1565 			DPRINTF("%s: drain, err = %d\n", DEVNAME(sc), error);
1566 			break;
1567 		}
1568 	}
1569 	mtx_leave(&audio_lock);
1570 	return error;
1571 }
1572 
1573 int
1574 audio_close(struct audio_softc *sc)
1575 {
1576 	audio_drain(sc);
1577 	if (sc->active)
1578 		audio_stop(sc);
1579 	sc->ops->close(sc->arg);
1580 	sc->mode = 0;
1581 	DPRINTF("%s: close: done\n", DEVNAME(sc));
1582 	return 0;
1583 }
1584 
1585 int
1586 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag)
1587 {
1588 	unsigned char *ptr;
1589 	size_t count;
1590 	int error;
1591 
1592 	DPRINTFN(1, "%s: read: resid = %zd\n", DEVNAME(sc), uio->uio_resid);
1593 
1594 	/* block if quiesced */
1595 	while (sc->quiesce)
1596 		tsleep_nsec(&sc->quiesce, 0, "au_qrd", INFSLP);
1597 
1598 	/* start automatically if audio_ioc_start() was never called */
1599 	if (audio_canstart(sc)) {
1600 		error = audio_start(sc);
1601 		if (error)
1602 			return error;
1603 	}
1604 
1605 	mtx_enter(&audio_lock);
1606 
1607 	/* if there is no data then sleep */
1608 	while (sc->rec.used == 0) {
1609 		if (ioflag & IO_NDELAY) {
1610 			mtx_leave(&audio_lock);
1611 			return EWOULDBLOCK;
1612 		}
1613 		DPRINTFN(1, "%s: read sleep\n", DEVNAME(sc));
1614 		sc->rec.blocking = 1;
1615 		error = msleep_nsec(&sc->rec.blocking,
1616 		    &audio_lock, PWAIT | PCATCH, "au_rd", INFSLP);
1617 		if (!(sc->dev.dv_flags & DVF_ACTIVE))
1618 			error = EIO;
1619 		if (error) {
1620 			DPRINTF("%s: read woke up error = %d\n",
1621 			    DEVNAME(sc), error);
1622 			mtx_leave(&audio_lock);
1623 			return error;
1624 		}
1625 	}
1626 
1627 	/* at this stage, there is data to transfer */
1628 	while (uio->uio_resid > 0 && sc->rec.used > 0) {
1629 		ptr = audio_buf_rgetblk(&sc->rec, &count);
1630 		if (count > uio->uio_resid)
1631 			count = uio->uio_resid;
1632 		mtx_leave(&audio_lock);
1633 		DPRINTFN(1, "%s: read: start = %zu, count = %zu\n",
1634 		    DEVNAME(sc), ptr - sc->rec.data, count);
1635 		if (sc->conv_dec)
1636 			sc->conv_dec(ptr, count);
1637 		error = uiomove(ptr, count, uio);
1638 		if (error)
1639 			return error;
1640 		mtx_enter(&audio_lock);
1641 		audio_buf_rdiscard(&sc->rec, count);
1642 	}
1643 	mtx_leave(&audio_lock);
1644 	return 0;
1645 }
1646 
1647 int
1648 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag)
1649 {
1650 	unsigned char *ptr;
1651 	size_t count;
1652 	int error;
1653 
1654 	DPRINTFN(1, "%s: write: resid = %zd\n",  DEVNAME(sc), uio->uio_resid);
1655 
1656 	/* block if quiesced */
1657 	while (sc->quiesce)
1658 		tsleep_nsec(&sc->quiesce, 0, "au_qwr", INFSLP);
1659 
1660 	/*
1661 	 * if IO_NDELAY flag is set then check if there is enough room
1662 	 * in the buffer to store at least one byte. If not then don't
1663 	 * start the write process.
1664 	 */
1665 	mtx_enter(&audio_lock);
1666 	if (uio->uio_resid > 0 && (ioflag & IO_NDELAY)) {
1667 		if (sc->play.used == sc->play.len) {
1668 			mtx_leave(&audio_lock);
1669 			return EWOULDBLOCK;
1670 		}
1671 	}
1672 
1673 	while (uio->uio_resid > 0) {
1674 		while (1) {
1675 			ptr = audio_buf_wgetblk(&sc->play, &count);
1676 			if (count > 0)
1677 				break;
1678 			if (ioflag & IO_NDELAY) {
1679 				/*
1680 				 * At this stage at least one byte is already
1681 				 * moved so we do not return EWOULDBLOCK
1682 				 */
1683 				mtx_leave(&audio_lock);
1684 				return 0;
1685 			}
1686 			DPRINTFN(1, "%s: write sleep\n", DEVNAME(sc));
1687 			sc->play.blocking = 1;
1688 			error = msleep_nsec(&sc->play.blocking,
1689 			    &audio_lock, PWAIT | PCATCH, "au_wr", INFSLP);
1690 			if (!(sc->dev.dv_flags & DVF_ACTIVE))
1691 				error = EIO;
1692 			if (error) {
1693 				DPRINTF("%s: write woke up error = %d\n",
1694 				    DEVNAME(sc), error);
1695 				mtx_leave(&audio_lock);
1696 				return error;
1697 			}
1698 		}
1699 		if (count > uio->uio_resid)
1700 			count = uio->uio_resid;
1701 		mtx_leave(&audio_lock);
1702 		error = uiomove(ptr, count, uio);
1703 		if (error)
1704 			return 0;
1705 		if (sc->conv_enc) {
1706 			sc->conv_enc(ptr, count);
1707 			DPRINTFN(1, "audio_write: converted count = %zu\n",
1708 			    count);
1709 		}
1710 		if (sc->ops->copy_output)
1711 			sc->ops->copy_output(sc->arg, count);
1712 
1713 		mtx_enter(&audio_lock);
1714 		audio_buf_wcommit(&sc->play, count);
1715 
1716 		/* start automatically if audio_ioc_start() was never called */
1717 		if (audio_canstart(sc)) {
1718 			mtx_leave(&audio_lock);
1719 			error = audio_start(sc);
1720 			if (error)
1721 				return error;
1722 			mtx_enter(&audio_lock);
1723 		}
1724 	}
1725 	mtx_leave(&audio_lock);
1726 	return 0;
1727 }
1728 
1729 int
1730 audio_getdev(struct audio_softc *sc, struct audio_device *adev)
1731 {
1732 	memset(adev, 0, sizeof(struct audio_device));
1733 	if (sc->dev.dv_parent == NULL)
1734 		return EIO;
1735 	strlcpy(adev->name, sc->dev.dv_parent->dv_xname, MAX_AUDIO_DEV_LEN);
1736 	return 0;
1737 }
1738 
1739 int
1740 audio_ioctl(struct audio_softc *sc, unsigned long cmd, void *addr)
1741 {
1742 	struct audio_pos *ap;
1743 	int error = 0;
1744 
1745 	/* block if quiesced */
1746 	while (sc->quiesce)
1747 		tsleep_nsec(&sc->quiesce, 0, "au_qio", INFSLP);
1748 
1749 	switch (cmd) {
1750 	case FIONBIO:
1751 		/* All handled in the upper FS layer. */
1752 		break;
1753 	case AUDIO_GETPOS:
1754 		mtx_enter(&audio_lock);
1755 		ap = (struct audio_pos *)addr;
1756 		ap->play_pos = sc->play.pos;
1757 		ap->play_xrun = sc->play.xrun;
1758 		ap->rec_pos = sc->rec.pos;
1759 		ap->rec_xrun = sc->rec.xrun;
1760 		mtx_leave(&audio_lock);
1761 		break;
1762 	case AUDIO_START:
1763 		return audio_ioc_start(sc);
1764 	case AUDIO_STOP:
1765 		return audio_ioc_stop(sc);
1766 	case AUDIO_SETPAR:
1767 		error = audio_ioc_setpar(sc, (struct audio_swpar *)addr);
1768 		break;
1769 	case AUDIO_GETPAR:
1770 		error = audio_ioc_getpar(sc, (struct audio_swpar *)addr);
1771 		break;
1772 	case AUDIO_GETSTATUS:
1773 		error = audio_ioc_getstatus(sc, (struct audio_status *)addr);
1774 		break;
1775 	case AUDIO_GETDEV:
1776 		error = audio_getdev(sc, (struct audio_device *)addr);
1777 		break;
1778 	default:
1779 		DPRINTF("%s: unknown ioctl 0x%lx\n", DEVNAME(sc), cmd);
1780 		error = ENOTTY;
1781 		break;
1782 	}
1783 	return error;
1784 }
1785 
1786 void
1787 audio_event(struct audio_softc *sc, int addr)
1788 {
1789 	struct mixer_ev *e;
1790 
1791 	mtx_enter(&audio_lock);
1792 	if (sc->mix_isopen) {
1793 		e = sc->mix_evbuf + addr;
1794 		if (!e->pending) {
1795 			e->pending = 1;
1796 			e->next = sc->mix_pending;
1797 			sc->mix_pending = e;
1798 		}
1799 		audio_mixer_wakeup(sc);
1800 	}
1801 	mtx_leave(&audio_lock);
1802 }
1803 
1804 int
1805 audio_mixer_devinfo(struct audio_softc *sc, struct mixer_devinfo *devinfo)
1806 {
1807 	if (devinfo->index < sc->mix_nent)
1808 		return sc->ops->query_devinfo(sc->arg, devinfo);
1809 
1810 	devinfo->next = -1;
1811 	devinfo->prev = -1;
1812 	switch (devinfo->index - sc->mix_nent) {
1813 	case MIXER_RECORD:
1814 		strlcpy(devinfo->label.name, AudioCrecord, MAX_AUDIO_DEV_LEN);
1815 		devinfo->type = AUDIO_MIXER_CLASS;
1816 		devinfo->mixer_class = -1;
1817 		break;
1818 	case MIXER_RECORD_ENABLE:
1819 		strlcpy(devinfo->label.name, "enable", MAX_AUDIO_DEV_LEN);
1820 		devinfo->type = AUDIO_MIXER_ENUM;
1821 		devinfo->mixer_class = MIXER_RECORD + sc->mix_nent;
1822 		devinfo->un.e.num_mem = 3;
1823 		devinfo->un.e.member[0].ord = MIXER_RECORD_ENABLE_OFF;
1824 		strlcpy(devinfo->un.e.member[0].label.name, "off",
1825 		    MAX_AUDIO_DEV_LEN);
1826 		devinfo->un.e.member[1].ord = MIXER_RECORD_ENABLE_ON;
1827 		strlcpy(devinfo->un.e.member[1].label.name, "on",
1828 		    MAX_AUDIO_DEV_LEN);
1829 		devinfo->un.e.member[2].ord = MIXER_RECORD_ENABLE_SYSCTL;
1830 		strlcpy(devinfo->un.e.member[2].label.name, "sysctl",
1831 		    MAX_AUDIO_DEV_LEN);
1832 		break;
1833 	default:
1834 		return EINVAL;
1835 	}
1836 
1837 	return 0;
1838 }
1839 
1840 int
1841 audio_mixer_get(struct audio_softc *sc, struct mixer_ctrl *c)
1842 {
1843 	if (c->dev < sc->mix_nent)
1844 		return sc->ops->get_port(sc->arg, c);
1845 
1846 	switch (c->dev - sc->mix_nent) {
1847 	case MIXER_RECORD:
1848 		return EBADF;
1849 	case MIXER_RECORD_ENABLE:
1850 		c->un.ord = sc->record_enable;
1851 		break;
1852 	default:
1853 		return EINVAL;
1854 	}
1855 
1856 	return 0;
1857 }
1858 
1859 int
1860 audio_mixer_set(struct audio_softc *sc, struct mixer_ctrl *c, struct proc *p)
1861 {
1862 	int error;
1863 
1864 	if (c->dev < sc->mix_nent) {
1865 		error = sc->ops->set_port(sc->arg, c);
1866 		if (error)
1867 			return error;
1868 		if (sc->ops->commit_settings)
1869 			return sc->ops->commit_settings(sc->arg);
1870 		audio_event(sc, c->dev);
1871 		return 0;
1872 	}
1873 
1874 	switch (c->dev - sc->mix_nent) {
1875 	case MIXER_RECORD:
1876 		return EBADF;
1877 	case MIXER_RECORD_ENABLE:
1878 		switch (c->un.ord) {
1879 		case MIXER_RECORD_ENABLE_OFF:
1880 		case MIXER_RECORD_ENABLE_ON:
1881 		case MIXER_RECORD_ENABLE_SYSCTL:
1882 			break;
1883 		default:
1884 			return EINVAL;
1885 		}
1886 		if (suser(p) == 0)
1887 			sc->record_enable = c->un.ord;
1888 		break;
1889 	default:
1890 		return EINVAL;
1891 	}
1892 
1893 	return 0;
1894 }
1895 
1896 int
1897 audio_ioctl_mixer(struct audio_softc *sc, unsigned long cmd, void *addr,
1898 	struct proc *p)
1899 {
1900 	/* block if quiesced */
1901 	while (sc->quiesce)
1902 		tsleep_nsec(&sc->quiesce, 0, "mix_qio", INFSLP);
1903 
1904 	switch (cmd) {
1905 	case FIONBIO:
1906 		/* All handled in the upper FS layer. */
1907 		break;
1908 	case AUDIO_MIXER_DEVINFO:
1909 		return audio_mixer_devinfo(sc, addr);
1910 	case AUDIO_MIXER_READ:
1911 		return audio_mixer_get(sc, addr);
1912 	case AUDIO_MIXER_WRITE:
1913 		return audio_mixer_set(sc, addr, p);
1914 	default:
1915 		return ENOTTY;
1916 	}
1917 	return 0;
1918 }
1919 
1920 int
1921 audio_mixer_read(struct audio_softc *sc, struct uio *uio, int ioflag)
1922 {
1923 	struct mixer_ev *e;
1924 	int data;
1925 	int error;
1926 
1927 	DPRINTF("%s: mixer read: resid = %zd\n", DEVNAME(sc), uio->uio_resid);
1928 
1929 	/* block if quiesced */
1930 	while (sc->quiesce)
1931 		tsleep_nsec(&sc->quiesce, 0, "mix_qrd", INFSLP);
1932 
1933 	mtx_enter(&audio_lock);
1934 
1935 	/* if there are no events then sleep */
1936 	while (!sc->mix_pending) {
1937 		if (ioflag & IO_NDELAY) {
1938 			mtx_leave(&audio_lock);
1939 			return EWOULDBLOCK;
1940 		}
1941 		DPRINTF("%s: mixer read sleep\n", DEVNAME(sc));
1942 		sc->mix_blocking = 1;
1943 		error = msleep_nsec(&sc->mix_blocking,
1944 		    &audio_lock, PWAIT | PCATCH, "mix_rd", INFSLP);
1945 		if (!(sc->dev.dv_flags & DVF_ACTIVE))
1946 			error = EIO;
1947 		if (error) {
1948 			DPRINTF("%s: mixer read woke up error = %d\n",
1949 			    DEVNAME(sc), error);
1950 			mtx_leave(&audio_lock);
1951 			return error;
1952 		}
1953 	}
1954 
1955 	/* at this stage, there is an event to transfer */
1956 	while (uio->uio_resid >= sizeof(int) && sc->mix_pending) {
1957 		e = sc->mix_pending;
1958 		sc->mix_pending = e->next;
1959 		e->pending = 0;
1960 		data = e - sc->mix_evbuf;
1961 		mtx_leave(&audio_lock);
1962 		DPRINTF("%s: mixer read: %u\n", DEVNAME(sc), data);
1963 		error = uiomove(&data, sizeof(int), uio);
1964 		if (error)
1965 			return error;
1966 		mtx_enter(&audio_lock);
1967 	}
1968 
1969 	mtx_leave(&audio_lock);
1970 	return 0;
1971 }
1972 
1973 int
1974 audio_mixer_open(struct audio_softc *sc, int flags)
1975 {
1976 	DPRINTF("%s: flags = 0x%x\n", __func__, flags);
1977 
1978 	if (flags & FREAD) {
1979 		if (sc->mix_isopen)
1980 			return EBUSY;
1981 		sc->mix_isopen = 1;
1982 	}
1983 	return 0;
1984 }
1985 
1986 int
1987 audio_mixer_close(struct audio_softc *sc, int flags)
1988 {
1989 	int i;
1990 
1991 	DPRINTF("%s: flags = 0x%x\n", __func__, flags);
1992 
1993 	if (flags & FREAD) {
1994 		sc->mix_isopen = 0;
1995 
1996 		mtx_enter(&audio_lock);
1997 		sc->mix_pending = NULL;
1998 		for (i = 0; i < sc->mix_nent; i++)
1999 			sc->mix_evbuf[i].pending = 0;
2000 		mtx_leave(&audio_lock);
2001 	}
2002 	return 0;
2003 }
2004 
2005 int
2006 audioopen(dev_t dev, int flags, int mode, struct proc *p)
2007 {
2008 	struct audio_softc *sc;
2009 	int error;
2010 
2011 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2012 	if (sc == NULL)
2013 		return ENXIO;
2014 	if (sc->ops == NULL)
2015 		error = ENXIO;
2016 	else {
2017 		switch (AUDIO_DEV(dev)) {
2018 		case AUDIO_DEV_AUDIO:
2019 			error = audio_open(sc, flags);
2020 			break;
2021 		case AUDIO_DEV_AUDIOCTL:
2022 			error = audio_mixer_open(sc, flags);
2023 			break;
2024 		default:
2025 			error = ENXIO;
2026 		}
2027 	}
2028 	device_unref(&sc->dev);
2029 	return error;
2030 }
2031 
2032 int
2033 audioclose(dev_t dev, int flags, int ifmt, struct proc *p)
2034 {
2035 	struct audio_softc *sc;
2036 	int error;
2037 
2038 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2039 	if (sc == NULL)
2040 		return ENXIO;
2041 	switch (AUDIO_DEV(dev)) {
2042 	case AUDIO_DEV_AUDIO:
2043 		error = audio_close(sc);
2044 		break;
2045 	case AUDIO_DEV_AUDIOCTL:
2046 		error = audio_mixer_close(sc, flags);
2047 		break;
2048 	default:
2049 		error = ENXIO;
2050 	}
2051 	device_unref(&sc->dev);
2052 	return error;
2053 }
2054 
2055 int
2056 audioread(dev_t dev, struct uio *uio, int ioflag)
2057 {
2058 	struct audio_softc *sc;
2059 	int error;
2060 
2061 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2062 	if (sc == NULL)
2063 		return ENXIO;
2064 	switch (AUDIO_DEV(dev)) {
2065 	case AUDIO_DEV_AUDIO:
2066 		error = audio_read(sc, uio, ioflag);
2067 		break;
2068 	case AUDIO_DEV_AUDIOCTL:
2069 		error = audio_mixer_read(sc, uio, ioflag);
2070 		break;
2071 	default:
2072 		error = ENXIO;
2073 	}
2074 	device_unref(&sc->dev);
2075 	return error;
2076 }
2077 
2078 int
2079 audiowrite(dev_t dev, struct uio *uio, int ioflag)
2080 {
2081 	struct audio_softc *sc;
2082 	int error;
2083 
2084 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2085 	if (sc == NULL)
2086 		return ENXIO;
2087 	switch (AUDIO_DEV(dev)) {
2088 	case AUDIO_DEV_AUDIO:
2089 		error = audio_write(sc, uio, ioflag);
2090 		break;
2091 	case AUDIO_DEV_AUDIOCTL:
2092 		error = ENODEV;
2093 		break;
2094 	default:
2095 		error = ENXIO;
2096 	}
2097 	device_unref(&sc->dev);
2098 	return error;
2099 }
2100 
2101 int
2102 audioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
2103 {
2104 	struct audio_softc *sc;
2105 	int error;
2106 
2107 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2108 	if (sc == NULL)
2109 		return ENXIO;
2110 	switch (AUDIO_DEV(dev)) {
2111 	case AUDIO_DEV_AUDIO:
2112 		error = audio_ioctl(sc, cmd, addr);
2113 		break;
2114 	case AUDIO_DEV_AUDIOCTL:
2115 		if (cmd == AUDIO_SETPAR && sc->mode != 0) {
2116 			error = EBUSY;
2117 			break;
2118 		}
2119 		if (cmd == AUDIO_START || cmd == AUDIO_STOP) {
2120 			error = ENXIO;
2121 			break;
2122 		}
2123 		if (cmd == AUDIO_MIXER_DEVINFO ||
2124 		    cmd == AUDIO_MIXER_READ ||
2125 		    cmd == AUDIO_MIXER_WRITE)
2126 			error = audio_ioctl_mixer(sc, cmd, addr, p);
2127 		else
2128 			error = audio_ioctl(sc, cmd, addr);
2129 		break;
2130 	default:
2131 		error = ENXIO;
2132 	}
2133 	device_unref(&sc->dev);
2134 	return error;
2135 }
2136 
2137 int
2138 audiokqfilter(dev_t dev, struct knote *kn)
2139 {
2140 	struct audio_softc *sc;
2141 	struct klist 	  *klist;
2142 	int error;
2143 
2144 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
2145 	if (sc == NULL)
2146 		return ENXIO;
2147 	error = 0;
2148 	switch (AUDIO_DEV(dev)) {
2149 	case AUDIO_DEV_AUDIO:
2150 		switch (kn->kn_filter) {
2151 		case EVFILT_READ:
2152 			klist = &sc->rec.klist;
2153 			kn->kn_fop = &audioread_filtops;
2154 			break;
2155 		case EVFILT_WRITE:
2156 			klist = &sc->play.klist;
2157 			kn->kn_fop = &audiowrite_filtops;
2158 			break;
2159 		default:
2160 			error = EINVAL;
2161 			goto done;
2162 		}
2163 		break;
2164 	case AUDIO_DEV_AUDIOCTL:
2165 		switch (kn->kn_filter) {
2166 		case EVFILT_READ:
2167 			klist = &sc->mix_klist;
2168 			kn->kn_fop = &audioctlread_filtops;
2169 			break;
2170 		default:
2171 			error = EINVAL;
2172 			goto done;
2173 		}
2174 		break;
2175 	}
2176 	kn->kn_hook = sc;
2177 
2178 	klist_insert(klist, kn);
2179 done:
2180 	device_unref(&sc->dev);
2181 	return error;
2182 }
2183 
2184 void
2185 filt_audiordetach(struct knote *kn)
2186 {
2187 	struct audio_softc *sc = kn->kn_hook;
2188 
2189 	klist_remove(&sc->rec.klist, kn);
2190 }
2191 
2192 int
2193 filt_audioread(struct knote *kn, long hint)
2194 {
2195 	struct audio_softc *sc = kn->kn_hook;
2196 
2197 	MUTEX_ASSERT_LOCKED(&audio_lock);
2198 
2199 	return (sc->mode & AUMODE_RECORD) && (sc->rec.used > 0);
2200 }
2201 
2202 void
2203 filt_audiowdetach(struct knote *kn)
2204 {
2205 	struct audio_softc *sc = kn->kn_hook;
2206 
2207 	klist_remove(&sc->play.klist, kn);
2208 }
2209 
2210 int
2211 filt_audiowrite(struct knote *kn, long hint)
2212 {
2213 	struct audio_softc *sc = kn->kn_hook;
2214 
2215 	MUTEX_ASSERT_LOCKED(&audio_lock);
2216 
2217 	return (sc->mode & AUMODE_PLAY) && (sc->play.used < sc->play.len);
2218 }
2219 
2220 void
2221 filt_audioctlrdetach(struct knote *kn)
2222 {
2223 	struct audio_softc *sc = kn->kn_hook;
2224 
2225 	klist_remove(&sc->mix_klist, kn);
2226 }
2227 
2228 int
2229 filt_audioctlread(struct knote *kn, long hint)
2230 {
2231 	struct audio_softc *sc = kn->kn_hook;
2232 
2233 	MUTEX_ASSERT_LOCKED(&audio_lock);
2234 
2235 	return (sc->mix_isopen && sc->mix_pending);
2236 }
2237 
2238 int
2239 filt_audiomodify(struct kevent *kev, struct knote *kn)
2240 {
2241 	int active;
2242 
2243 	mtx_enter(&audio_lock);
2244 	active = knote_modify(kev, kn);
2245 	mtx_leave(&audio_lock);
2246 
2247 	return active;
2248 }
2249 
2250 int
2251 filt_audioprocess(struct knote *kn, struct kevent *kev)
2252 {
2253 	int active;
2254 
2255 	mtx_enter(&audio_lock);
2256 	active = knote_process(kn, kev);
2257 	mtx_leave(&audio_lock);
2258 
2259 	return active;
2260 }
2261 
2262 #if NWSKBD > 0
2263 int
2264 wskbd_initmute(struct audio_softc *sc, struct mixer_devinfo *vol)
2265 {
2266 	struct mixer_devinfo *mi;
2267 	int index = -1;
2268 
2269 	mi = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK);
2270 
2271 	for (mi->index = vol->next; mi->index != -1; mi->index = mi->next) {
2272 		if (sc->ops->query_devinfo(sc->arg, mi) != 0)
2273 			break;
2274 		if (strcmp(mi->label.name, AudioNmute) == 0) {
2275 			index = mi->index;
2276 			break;
2277 		}
2278 	}
2279 
2280 	free(mi, M_TEMP, sizeof(struct mixer_devinfo));
2281 	return index;
2282 }
2283 
2284 int
2285 wskbd_initvol(struct audio_softc *sc, struct wskbd_vol *vol, char *cn, char *dn)
2286 {
2287 	struct mixer_devinfo *dev, *cls;
2288 
2289 	vol->val = vol->mute = -1;
2290 	dev = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK);
2291 	cls = malloc(sizeof(struct mixer_devinfo), M_TEMP, M_WAITOK);
2292 
2293 	for (dev->index = 0; ; dev->index++) {
2294 		if (sc->ops->query_devinfo(sc->arg, dev) != 0)
2295 			break;
2296 		if (dev->type != AUDIO_MIXER_VALUE)
2297 			continue;
2298 		cls->index = dev->mixer_class;
2299 		if (sc->ops->query_devinfo(sc->arg, cls) != 0)
2300 			continue;
2301 		if (strcmp(cls->label.name, cn) == 0 &&
2302 		    strcmp(dev->label.name, dn) == 0) {
2303 			vol->val = dev->index;
2304 			vol->nch = dev->un.v.num_channels;
2305 			vol->step = dev->un.v.delta > 8 ? dev->un.v.delta : 8;
2306 			vol->mute = wskbd_initmute(sc, dev);
2307 			vol->val_pending = vol->mute_pending = 0;
2308 			DPRINTF("%s: wskbd using %s.%s%s\n", DEVNAME(sc),
2309 			    cn, dn, vol->mute >= 0 ? ", mute control" : "");
2310 			break;
2311 		}
2312 	}
2313 
2314 	free(cls, M_TEMP, sizeof(struct mixer_devinfo));
2315 	free(dev, M_TEMP, sizeof(struct mixer_devinfo));
2316 	return (vol->val != -1);
2317 }
2318 
2319 void
2320 wskbd_mixer_init(struct audio_softc *sc)
2321 {
2322 	static struct {
2323 		char *cn, *dn;
2324 	} spkr_names[] = {
2325 		{AudioCoutputs, AudioNmaster},
2326 		{AudioCinputs,  AudioNdac},
2327 		{AudioCoutputs, AudioNdac},
2328 		{AudioCoutputs, AudioNoutput}
2329 	}, mic_names[] = {
2330 		{AudioCrecord, AudioNrecord},
2331 		{AudioCrecord, AudioNvolume},
2332 		{AudioCinputs, AudioNrecord},
2333 		{AudioCinputs, AudioNvolume},
2334 		{AudioCinputs, AudioNinput}
2335 	};
2336 	int i;
2337 
2338 	for (i = 0; i < sizeof(spkr_names) / sizeof(spkr_names[0]); i++) {
2339 		if (wskbd_initvol(sc, &sc->spkr,
2340 			spkr_names[i].cn, spkr_names[i].dn))
2341 			break;
2342 	}
2343 	for (i = 0; i < sizeof(mic_names) / sizeof(mic_names[0]); i++) {
2344 		if (wskbd_initvol(sc, &sc->mic,
2345 			mic_names[i].cn, mic_names[i].dn))
2346 			break;
2347 	}
2348 	task_set(&sc->wskbd_task, wskbd_mixer_cb, sc);
2349 }
2350 
2351 void
2352 wskbd_mixer_update(struct audio_softc *sc, struct wskbd_vol *vol)
2353 {
2354 	struct mixer_ctrl ctrl;
2355 	int val_pending, mute_pending, i, gain, error, s;
2356 
2357 	s = spltty();
2358 	val_pending = vol->val_pending;
2359 	vol->val_pending = 0;
2360 	mute_pending = vol->mute_pending;
2361 	vol->mute_pending = 0;
2362 	splx(s);
2363 
2364 	if (sc->ops == NULL)
2365 		return;
2366 	if (vol->mute >= 0 && mute_pending) {
2367 		ctrl.dev = vol->mute;
2368 		ctrl.type = AUDIO_MIXER_ENUM;
2369 		error = sc->ops->get_port(sc->arg, &ctrl);
2370 		if (error) {
2371 			DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
2372 			return;
2373 		}
2374 		switch (mute_pending) {
2375 		case WSKBD_MUTE_TOGGLE:
2376 			ctrl.un.ord = !ctrl.un.ord;
2377 			break;
2378 		case WSKBD_MUTE_DISABLE:
2379 			ctrl.un.ord = 0;
2380 			break;
2381 		case WSKBD_MUTE_ENABLE:
2382 			ctrl.un.ord = 1;
2383 			break;
2384 		}
2385 		DPRINTFN(1, "%s: wskbd mute setting to %d\n",
2386 		    DEVNAME(sc), ctrl.un.ord);
2387 		error = sc->ops->set_port(sc->arg, &ctrl);
2388 		if (error) {
2389 			DPRINTF("%s: set mute err = %d\n", DEVNAME(sc), error);
2390 			return;
2391 		}
2392 		audio_event(sc, vol->mute);
2393 	}
2394 	if (vol->val >= 0 && val_pending) {
2395 		ctrl.dev = vol->val;
2396 		ctrl.type = AUDIO_MIXER_VALUE;
2397 		ctrl.un.value.num_channels = vol->nch;
2398 		error = sc->ops->get_port(sc->arg, &ctrl);
2399 		if (error) {
2400 			DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
2401 			return;
2402 		}
2403 		for (i = 0; i < vol->nch; i++) {
2404 			gain = ctrl.un.value.level[i] + vol->step * val_pending;
2405 			if (gain > AUDIO_MAX_GAIN)
2406 				gain = AUDIO_MAX_GAIN;
2407 			else if (gain < AUDIO_MIN_GAIN)
2408 				gain = AUDIO_MIN_GAIN;
2409 			ctrl.un.value.level[i] = gain;
2410 			DPRINTFN(1, "%s: wskbd level %d set to %d\n",
2411 			    DEVNAME(sc), i, gain);
2412 		}
2413 		error = sc->ops->set_port(sc->arg, &ctrl);
2414 		if (error) {
2415 			DPRINTF("%s: set vol err = %d\n", DEVNAME(sc), error);
2416 			return;
2417 		}
2418 		audio_event(sc, vol->val);
2419 	}
2420 }
2421 
2422 void
2423 wskbd_mixer_cb(void *arg)
2424 {
2425 	struct audio_softc *sc = arg;
2426 
2427 	wskbd_mixer_update(sc, &sc->spkr);
2428 	wskbd_mixer_update(sc, &sc->mic);
2429 	device_unref(&sc->dev);
2430 }
2431 
2432 int
2433 wskbd_set_mixermute(long mute, long out)
2434 {
2435 	struct audio_softc *sc;
2436 	struct wskbd_vol *vol;
2437 
2438 	sc = (struct audio_softc *)device_lookup(&audio_cd, 0);
2439 	if (sc == NULL)
2440 		return ENODEV;
2441 	vol = out ? &sc->spkr : &sc->mic;
2442 	vol->mute_pending = mute ? WSKBD_MUTE_ENABLE : WSKBD_MUTE_DISABLE;
2443 	if (!task_add(systq, &sc->wskbd_task))
2444 		device_unref(&sc->dev);
2445 	return 0;
2446 }
2447 
2448 /*
2449  * Adjust the volume of the audio device associated with the given cookie.
2450  * Otherwise, fallback to audio0.
2451  */
2452 int
2453 wskbd_set_mixervolume_dev(void *cookie, long dir, long out)
2454 {
2455 	int unit = 0;
2456 	int i;
2457 
2458 	for (i = 0; i < audio_cd.cd_ndevs; i++) {
2459 		struct audio_softc *sc;
2460 
2461 		sc = (struct audio_softc *)device_lookup(&audio_cd, i);
2462 		if (sc == NULL)
2463 			continue;
2464 		if (sc->cookie != cookie) {
2465 			device_unref(&sc->dev);
2466 			continue;
2467 		}
2468 
2469 		device_unref(&sc->dev);
2470 		unit = i;
2471 		break;
2472 	}
2473 
2474 	return wskbd_set_mixervolume_unit(unit, dir, out);
2475 }
2476 
2477 int
2478 wskbd_set_mixervolume(long dir, long out)
2479 {
2480 	return wskbd_set_mixervolume_unit(0, dir, out);
2481 }
2482 
2483 int
2484 wskbd_set_mixervolume_unit(int unit, long dir, long out)
2485 {
2486 	struct audio_softc *sc;
2487 	struct wskbd_vol *vol;
2488 
2489 	sc = (struct audio_softc *)device_lookup(&audio_cd, unit);
2490 	if (sc == NULL)
2491 		return ENODEV;
2492 	vol = out ? &sc->spkr : &sc->mic;
2493 	if (dir == 0)
2494 		vol->mute_pending ^= WSKBD_MUTE_TOGGLE;
2495 	else
2496 		vol->val_pending += dir;
2497 	if (!task_add(systq, &sc->wskbd_task))
2498 		device_unref(&sc->dev);
2499 	return 0;
2500 }
2501 #endif /* NWSKBD > 0 */
2502