xref: /openbsd-src/sys/dev/audio.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: audio.c,v 1.153 2016/09/19 06:46:43 ratchov 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/poll.h>
23 #include <sys/kernel.h>
24 #include <sys/task.h>
25 #include <sys/vnode.h>
26 #include <sys/malloc.h>
27 #include <sys/device.h>
28 #include <sys/audioio.h>
29 #include <dev/audio_if.h>
30 #include <dev/mulaw.h>
31 #include "audio.h"
32 #include "wskbd.h"
33 
34 #ifdef AUDIO_DEBUG
35 #define DPRINTF(...)				\
36 	do {					\
37 		if (audio_debug)		\
38 			printf(__VA_ARGS__);	\
39 	} while(0)
40 #define DPRINTFN(n, ...)			\
41 	do {					\
42 		if (audio_debug > (n))		\
43 			printf(__VA_ARGS__);	\
44 	} while(0)
45 #else
46 #define DPRINTF(...) do {} while(0)
47 #define DPRINTFN(n, ...) do {} while(0)
48 #endif
49 
50 #define DEVNAME(sc)		((sc)->dev.dv_xname)
51 #define AUDIO_UNIT(n)		(minor(n) & 0x0f)
52 #define AUDIO_DEV(n)		(minor(n) & 0xf0)
53 #define AUDIO_DEV_SOUND		0	/* minor of /dev/sound0 */
54 #define AUDIO_DEV_MIXER		0x10	/* minor of /dev/mixer0 */
55 #define AUDIO_DEV_AUDIO		0x80	/* minor of /dev/audio0 */
56 #define AUDIO_DEV_AUDIOCTL	0xc0	/* minor of /dev/audioctl */
57 #define AUDIO_BUFSZ		65536	/* buffer size in bytes */
58 
59 /*
60  * dma buffer
61  */
62 struct audio_buf {
63 	unsigned char *data;		/* DMA memory block */
64 	size_t datalen;			/* size of DMA memory block */
65 	size_t len;			/* size of DMA FIFO */
66 	size_t start;			/* first byte used in the FIFO */
67 	size_t used;			/* bytes used in the FIFO */
68 	size_t blksz;			/* DMA block size */
69 	struct selinfo sel;		/* to record & wakeup poll(2) */
70 	unsigned int pos;		/* bytes transferred */
71 	unsigned int xrun;		/* bytes lost by xruns */
72 	int blocking;			/* read/write blocking */
73 };
74 
75 #if NWSKBD > 0
76 struct wskbd_vol
77 {
78 	int val;			/* index of the value control */
79 	int mute;			/* index of the mute control */
80 	int step;			/* increment/decrement step */
81 	int nch;			/* channels in the value control */
82 	int val_pending;		/* pending change of val */
83 	int mute_pending;		/* pending mute toggles */
84 };
85 #endif
86 
87 /*
88  * device structure
89  */
90 struct audio_softc {
91 	struct device dev;
92 	struct audio_hw_if *ops;	/* driver funcs */
93 	void *arg;			/* first arg to driver funcs */
94 	int mode;			/* bitmask of AUMODE_* */
95 	int quiesce;			/* device suspended */
96 	struct audio_buf play, rec;
97 	unsigned int sw_enc;		/* user exposed AUDIO_ENCODING_* */
98 	unsigned int hw_enc;		/* hardware AUDIO_ENCODING_* */
99 	unsigned int bits;		/* bits per sample */
100 	unsigned int bps;		/* bytes-per-sample */
101 	unsigned int msb;		/* sample are MSB aligned */
102 	unsigned int rate;		/* rate in Hz */
103 	unsigned int round;		/* block size in frames */
104 	unsigned int nblks;		/* number of play blocks */
105 	unsigned int pchan, rchan;	/* number of channels */
106 	unsigned char silence[4];	/* a sample of silence */
107 	int pause;			/* not trying to start DMA */
108 	int active;			/* DMA in process */
109 	int offs;			/* offset between play & rec dir */
110 	void (*conv_enc)(unsigned char *, int);	/* encode to native */
111 	void (*conv_dec)(unsigned char *, int);	/* decode to user */
112 #if NWSKBD > 0
113 	struct wskbd_vol spkr, mic;
114 	struct task wskbd_task;
115 	int wskbd_taskset;
116 #endif
117 };
118 
119 int audio_match(struct device *, void *, void *);
120 void audio_attach(struct device *, struct device *, void *);
121 int audio_activate(struct device *, int);
122 int audio_detach(struct device *, int);
123 void audio_pintr(void *);
124 void audio_rintr(void *);
125 #if NWSKBD > 0
126 void wskbd_mixer_init(struct audio_softc *);
127 #endif
128 
129 const struct cfattach audio_ca = {
130 	sizeof(struct audio_softc), audio_match, audio_attach,
131 	audio_detach, audio_activate
132 };
133 
134 struct cfdriver audio_cd = {
135 	NULL, "audio", DV_DULL
136 };
137 
138 /*
139  * This mutex protects data structures (including registers on the
140  * sound-card) that are manipulated by both the interrupt handler and
141  * syscall code-paths.
142  *
143  * Note that driver methods may sleep (e.g. in malloc); consequently the
144  * audio layer calls them with the mutex unlocked. Driver methods are
145  * responsible for locking the mutex when they manipulate data used by
146  * the interrupt handler and interrupts may occur.
147  *
148  * Similarly, the driver is responsible for locking the mutex in its
149  * interrupt handler and to call the audio layer call-backs (i.e.
150  * audio_{p,r}int()) with the mutex locked.
151  */
152 struct mutex audio_lock = MUTEX_INITIALIZER(IPL_AUDIO);
153 
154 #ifdef AUDIO_DEBUG
155 /*
156  * 0 - nothing, as if AUDIO_DEBUG isn't defined
157  * 1 - initialisations & setup
158  * 2 - blocks & interrupts
159  */
160 int audio_debug = 1;
161 #endif
162 
163 unsigned int
164 audio_gcd(unsigned int a, unsigned int b)
165 {
166 	unsigned int r;
167 
168 	while (b > 0) {
169 		r = a % b;
170 		a = b;
171 		b = r;
172 	}
173 	return a;
174 }
175 
176 int
177 audio_buf_init(struct audio_softc *sc, struct audio_buf *buf, int dir)
178 {
179 	if (sc->ops->round_buffersize) {
180 		buf->datalen = sc->ops->round_buffersize(sc->arg,
181 		    dir, AUDIO_BUFSZ);
182 	} else
183 		buf->datalen = AUDIO_BUFSZ;
184 	if (sc->ops->allocm) {
185 		buf->data = sc->ops->allocm(sc->arg, dir, buf->datalen,
186 		    M_DEVBUF, M_WAITOK);
187 	} else
188 		buf->data = malloc(buf->datalen, M_DEVBUF, M_WAITOK);
189 	if (buf->data == NULL)
190 		return ENOMEM;
191 	return 0;
192 }
193 
194 void
195 audio_buf_done(struct audio_softc *sc, struct audio_buf *buf)
196 {
197 	if (sc->ops->freem)
198 		sc->ops->freem(sc->arg, buf->data, M_DEVBUF);
199 	else
200 		free(buf->data, M_DEVBUF, buf->datalen);
201 }
202 
203 /*
204  * return the reader pointer and the number of bytes available
205  */
206 unsigned char *
207 audio_buf_rgetblk(struct audio_buf *buf, size_t *rsize)
208 {
209 	size_t count;
210 
211 	count = buf->len - buf->start;
212 	if (count > buf->used)
213 		count = buf->used;
214 	*rsize = count;
215 	return buf->data + buf->start;
216 }
217 
218 /*
219  * discard "count" bytes at the start postion.
220  */
221 void
222 audio_buf_rdiscard(struct audio_buf *buf, size_t count)
223 {
224 #ifdef AUDIO_DEBUG
225 	if (count > buf->used) {
226 		panic("audio_buf_rdiscard: bad count = %zu, "
227 		    "start = %zu, used = %zu\n", count, buf->start, buf->used);
228 	}
229 #endif
230 	buf->used -= count;
231 	buf->start += count;
232 	if (buf->start >= buf->len)
233 		buf->start -= buf->len;
234 }
235 
236 /*
237  * advance the writer pointer by "count" bytes
238  */
239 void
240 audio_buf_wcommit(struct audio_buf *buf, size_t count)
241 {
242 #ifdef AUDIO_DEBUG
243 	if (count > (buf->len - buf->used)) {
244 		panic("audio_buf_wcommit: bad count = %zu, "
245 		    "start = %zu, used = %zu\n", count, buf->start, buf->used);
246 	}
247 #endif
248 	buf->used += count;
249 }
250 
251 /*
252  * get writer pointer and the number of bytes writable
253  */
254 unsigned char *
255 audio_buf_wgetblk(struct audio_buf *buf, size_t *rsize)
256 {
257 	size_t end, avail, count;
258 
259 	end = buf->start + buf->used;
260 	if (end >= buf->len)
261 		end -= buf->len;
262 	avail = buf->len - buf->used;
263 	count = buf->len - end;
264 	if (count > avail)
265 		count = avail;
266 	*rsize = count;
267 	return buf->data + end;
268 }
269 
270 void
271 audio_calc_sil(struct audio_softc *sc)
272 {
273 	unsigned char *q;
274 	unsigned int s, i;
275 	int d, e;
276 
277 	e = sc->sw_enc;
278 #ifdef AUDIO_DEBUG
279 	switch (e) {
280 	case AUDIO_ENCODING_SLINEAR_LE:
281 	case AUDIO_ENCODING_ULINEAR_LE:
282 	case AUDIO_ENCODING_SLINEAR_BE:
283 	case AUDIO_ENCODING_ULINEAR_BE:
284 		break;
285 	default:
286 		printf("%s: unhandled play encoding %d\n", DEVNAME(sc), e);
287 		memset(sc->silence, 0, sc->bps);
288 		return;
289 	}
290 #endif
291 	if (e == AUDIO_ENCODING_SLINEAR_BE || e == AUDIO_ENCODING_ULINEAR_BE) {
292 		d = -1;
293 		q = sc->silence + sc->bps - 1;
294 	} else {
295 		d = 1;
296 		q = sc->silence;
297 	}
298 	if (e == AUDIO_ENCODING_SLINEAR_LE || e == AUDIO_ENCODING_SLINEAR_BE) {
299 		s = 0;
300 	} else {
301 		s = 0x80000000;
302 		if (sc->msb)
303 			s >>= 32 - 8 * sc->bps;
304 		else
305 			s >>= 32 - sc->bits;
306 	}
307 	for (i = 0; i < sc->bps; i++) {
308 		*q = s;
309 		q += d;
310 		s >>= 8;
311 	}
312 	if (sc->conv_enc)
313 		sc->conv_enc(sc->silence, sc->bps);
314 }
315 
316 void
317 audio_fill_sil(struct audio_softc *sc, unsigned char *ptr, size_t count)
318 {
319 	unsigned char *q, *p;
320 	size_t i, j;
321 
322 	q = ptr;
323 	for (j = count / sc->bps; j > 0; j--) {
324 		p = sc->silence;
325 		for (i = sc->bps; i > 0; i--)
326 			*q++ = *p++;
327 	}
328 }
329 
330 void
331 audio_clear(struct audio_softc *sc)
332 {
333 	if (sc->mode & AUMODE_PLAY) {
334 		sc->play.used = sc->play.start = 0;
335 		sc->play.pos = sc->play.xrun = 0;
336 		audio_fill_sil(sc, sc->play.data, sc->play.len);
337 	}
338 	if (sc->mode & AUMODE_RECORD) {
339 		sc->rec.used = sc->rec.start = 0;
340 		sc->rec.pos = sc->rec.xrun = 0;
341 		audio_fill_sil(sc, sc->rec.data, sc->rec.len);
342 	}
343 }
344 
345 /*
346  * called whenever a block is consumed by the driver
347  */
348 void
349 audio_pintr(void *addr)
350 {
351 	struct audio_softc *sc = addr;
352 	unsigned char *ptr;
353 	size_t count;
354 	int error, nblk, todo;
355 
356 	MUTEX_ASSERT_LOCKED(&audio_lock);
357 	if (!(sc->mode & AUMODE_PLAY) || !sc->active) {
358 		printf("%s: play interrupt but not playing\n", DEVNAME(sc));
359 		return;
360 	}
361 	if (sc->quiesce) {
362 		DPRINTF("%s: quesced, skipping play intr\n", DEVNAME(sc));
363 		return;
364 	}
365 
366 	/*
367 	 * check if record pointer wrapped, see explanation
368 	 * in audio_rintr()
369 	 */
370 	if (sc->mode & AUMODE_RECORD) {
371 		sc->offs--;
372 		nblk = sc->rec.len / sc->rec.blksz;
373 		todo = -sc->offs;
374 		if (todo >= nblk) {
375 			todo -= todo % nblk;
376 			DPRINTFN(1, "%s: rec ptr wrapped, moving %d blocks\n",
377 			    DEVNAME(sc), todo);
378 			while (todo-- > 0)
379 				audio_rintr(sc);
380 		}
381 	}
382 
383 	sc->play.pos += sc->play.blksz;
384 	audio_fill_sil(sc, sc->play.data + sc->play.start, sc->play.blksz);
385 	audio_buf_rdiscard(&sc->play, sc->play.blksz);
386 	if (sc->play.used < sc->play.blksz) {
387 		DPRINTFN(1, "%s: play underrun\n", DEVNAME(sc));
388 		sc->play.xrun += sc->play.blksz;
389 		audio_buf_wcommit(&sc->play, sc->play.blksz);
390 	}
391 
392 	DPRINTFN(1, "%s: play intr, used -> %zu, start -> %zu\n",
393 	    DEVNAME(sc), sc->play.used, sc->play.start);
394 
395 	if (!sc->ops->trigger_output) {
396 		ptr = audio_buf_rgetblk(&sc->play, &count);
397 		error = sc->ops->start_output(sc->arg,
398 		    ptr, sc->play.blksz, audio_pintr, (void *)sc);
399 		if (error) {
400 			printf("%s: play restart failed: %d\n",
401 			    DEVNAME(sc), error);
402 		}
403 	}
404 
405 	if (sc->play.used < sc->play.len) {
406 		DPRINTFN(1, "%s: play wakeup, chan = %d\n",
407 		    DEVNAME(sc), sc->play.blocking);
408 		if (sc->play.blocking) {
409 			wakeup(&sc->play.blocking);
410 			sc->play.blocking = 0;
411 		}
412 		selwakeup(&sc->play.sel);
413 	}
414 }
415 
416 /*
417  * called whenever a block is produced by the driver
418  */
419 void
420 audio_rintr(void *addr)
421 {
422 	struct audio_softc *sc = addr;
423 	unsigned char *ptr;
424 	size_t count;
425 	int error, nblk, todo;
426 
427 	MUTEX_ASSERT_LOCKED(&audio_lock);
428 	if (!(sc->mode & AUMODE_RECORD) || !sc->active) {
429 		printf("%s: rec interrupt but not recording\n", DEVNAME(sc));
430 		return;
431 	}
432 	if (sc->quiesce) {
433 		DPRINTF("%s: quesced, skipping rec intr\n", DEVNAME(sc));
434 		return;
435 	}
436 
437 	/*
438 	 * Interrupts may be masked by other sub-systems during 320ms
439 	 * and more. During such a delay the hardware doesn't stop
440 	 * playing and the play buffer pointers may wrap, this can't be
441 	 * detected and corrected by low level drivers. This makes the
442 	 * record stream ahead of the play stream; this is detected as a
443 	 * hardware anomaly by userland and cause programs to misbehave.
444 	 *
445 	 * We fix this by advancing play position by an integer count of
446 	 * full buffers, so it reaches the record position.
447 	 */
448 	if (sc->mode & AUMODE_PLAY) {
449 		sc->offs++;
450 		nblk = sc->play.len / sc->play.blksz;
451 		todo = sc->offs;
452 		if (todo >= nblk) {
453 			todo -= todo % nblk;
454 			DPRINTFN(1, "%s: play ptr wrapped, moving %d blocks\n",
455 			    DEVNAME(sc), todo);
456 			while (todo-- > 0)
457 				audio_pintr(sc);
458 		}
459 	}
460 
461 	sc->rec.pos += sc->rec.blksz;
462 	audio_buf_wcommit(&sc->rec, sc->rec.blksz);
463 	if (sc->rec.used == sc->rec.len) {
464 		DPRINTFN(1, "%s: rec overrun\n", DEVNAME(sc));
465 		sc->rec.xrun += sc->rec.blksz;
466 		audio_buf_rdiscard(&sc->rec, sc->rec.blksz);
467 	}
468 	DPRINTFN(1, "%s: rec intr, used -> %zu\n", DEVNAME(sc), sc->rec.used);
469 
470 	if (!sc->ops->trigger_input) {
471 		ptr = audio_buf_wgetblk(&sc->rec, &count);
472 		error = sc->ops->start_input(sc->arg,
473 		    ptr, sc->rec.blksz, audio_rintr, (void *)sc);
474 		if (error) {
475 			printf("%s: rec restart failed: %d\n",
476 			    DEVNAME(sc), error);
477 		}
478 	}
479 
480 	if (sc->rec.used > 0) {
481 		DPRINTFN(1, "%s: rec wakeup, chan = %d\n",
482 		    DEVNAME(sc), sc->rec.blocking);
483 		if (sc->rec.blocking) {
484 			wakeup(&sc->rec.blocking);
485 			sc->rec.blocking = 0;
486 		}
487 		selwakeup(&sc->rec.sel);
488 	}
489 }
490 
491 int
492 audio_start_do(struct audio_softc *sc)
493 {
494 	int error;
495 	struct audio_params p;
496 	unsigned char *ptr;
497 	size_t count;
498 
499 	DPRINTFN(1, "%s: start play: "
500 	    "start = %zu, used = %zu, "
501 	    "len = %zu, blksz = %zu\n",
502 	    DEVNAME(sc), sc->play.start, sc->play.used,
503 	    sc->play.len, sc->play.blksz);
504 	DPRINTFN(1, "%s: start rec: "
505 	    "start = %zu, used = %zu, "
506 	    "len = %zu, blksz = %zu\n",
507 	    DEVNAME(sc), sc->rec.start, sc->rec.used,
508 	    sc->rec.len, sc->rec.blksz);
509 
510 	error = 0;
511 	sc->offs = 0;
512 	if (sc->mode & AUMODE_PLAY) {
513 		if (sc->ops->trigger_output) {
514 			p.encoding = sc->hw_enc;
515 			p.precision = sc->bits;
516 			p.bps = sc->bps;
517 			p.msb = sc->msb;
518 			p.sample_rate = sc->rate;
519 			p.channels = sc->pchan;
520 			error = sc->ops->trigger_output(sc->arg,
521 			    sc->play.data,
522 			    sc->play.data + sc->play.len,
523 			    sc->play.blksz,
524 			    audio_pintr, (void *)sc, &p);
525 		} else {
526 			mtx_enter(&audio_lock);
527 			ptr = audio_buf_rgetblk(&sc->play, &count);
528 			error = sc->ops->start_output(sc->arg,
529 			    ptr, sc->play.blksz, audio_pintr, (void *)sc);
530 			mtx_leave(&audio_lock);
531 		}
532 		if (error)
533 			printf("%s: failed to start playback\n", DEVNAME(sc));
534 	}
535 	if (sc->mode & AUMODE_RECORD) {
536 		if (sc->ops->trigger_input) {
537 			p.encoding = sc->hw_enc;
538 			p.precision = sc->bits;
539 			p.bps = sc->bps;
540 			p.msb = sc->msb;
541 			p.sample_rate = sc->rate;
542 			p.channels = sc->rchan;
543 			error = sc->ops->trigger_input(sc->arg,
544 			    sc->rec.data,
545 			    sc->rec.data + sc->rec.len,
546 			    sc->rec.blksz,
547 			    audio_rintr, (void *)sc, &p);
548 		} else {
549 			mtx_enter(&audio_lock);
550 			ptr = audio_buf_wgetblk(&sc->rec, &count);
551 			error = sc->ops->start_input(sc->arg,
552 			    ptr, sc->rec.blksz, audio_rintr, (void *)sc);
553 			mtx_leave(&audio_lock);
554 		}
555 		if (error)
556 			printf("%s: failed to start recording\n", DEVNAME(sc));
557 	}
558 	return error;
559 }
560 
561 int
562 audio_stop_do(struct audio_softc *sc)
563 {
564 	if (sc->mode & AUMODE_PLAY)
565 		sc->ops->halt_output(sc->arg);
566 	if (sc->mode & AUMODE_RECORD)
567 		sc->ops->halt_input(sc->arg);
568 	return 0;
569 }
570 
571 int
572 audio_start(struct audio_softc *sc)
573 {
574 	sc->active = 1;
575 	sc->play.xrun = sc->play.pos = sc->rec.xrun = sc->rec.pos = 0;
576 	return audio_start_do(sc);
577 }
578 
579 int
580 audio_stop(struct audio_softc *sc)
581 {
582 	int error;
583 
584 	error = audio_stop_do(sc);
585 	if (error)
586 		return error;
587 	audio_clear(sc);
588 	sc->active = 0;
589 	return 0;
590 }
591 
592 int
593 audio_setpar(struct audio_softc *sc)
594 {
595 	struct audio_params p, r;
596 	unsigned int nr, np, max, min, mult;
597 	unsigned int blk_mult, blk_max;
598 	int error;
599 
600 	DPRINTF("%s: setpar: req enc=%d bits=%d, bps=%d, msb=%d "
601 	    "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
602 	    DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
603 	    sc->rate, sc->pchan, sc->rchan, sc->round, sc->nblks);
604 
605 	/*
606 	 * AUDIO_ENCODING_SLINEAR and AUDIO_ENCODING_ULINEAR are not
607 	 * used anymore, promote them to the _LE and _BE equivalents
608 	 */
609 	if (sc->sw_enc == AUDIO_ENCODING_SLINEAR) {
610 #if BYTE_ORDER == LITTLE_ENDIAN
611 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
612 #else
613 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
614 #endif
615 	}
616 	if (sc->sw_enc == AUDIO_ENCODING_ULINEAR) {
617 #if BYTE_ORDER == LITTLE_ENDIAN
618 		sc->sw_enc = AUDIO_ENCODING_ULINEAR_LE;
619 #else
620 		sc->sw_enc = AUDIO_ENCODING_ULINEAR_BE;
621 #endif
622 	}
623 
624 	/*
625 	 * check if requested parameters are in the allowed ranges
626 	 */
627 	if (sc->mode & AUMODE_PLAY) {
628 		if (sc->pchan < 1)
629 			sc->pchan = 1;
630 		if (sc->pchan > 64)
631 			sc->pchan = 64;
632 	}
633 	if (sc->mode & AUMODE_RECORD) {
634 		if (sc->rchan < 1)
635 			sc->rchan = 1;
636 		if (sc->rchan > 64)
637 			sc->rchan = 64;
638 	}
639 	switch (sc->sw_enc) {
640 	case AUDIO_ENCODING_ULAW:
641 	case AUDIO_ENCODING_ALAW:
642 	case AUDIO_ENCODING_SLINEAR_LE:
643 	case AUDIO_ENCODING_SLINEAR_BE:
644 	case AUDIO_ENCODING_ULINEAR_LE:
645 	case AUDIO_ENCODING_ULINEAR_BE:
646 		break;
647 	default:
648 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
649 	}
650 	if (sc->bits < 8)
651 		sc->bits = 8;
652 	if (sc->bits > 32)
653 		sc->bits = 32;
654 	if (sc->bps < 1)
655 		sc->bps = 1;
656 	if (sc->bps > 4)
657 		sc->bps = 4;
658 	if (sc->rate < 4000)
659 		sc->rate = 4000;
660 	if (sc->rate > 192000)
661 		sc->rate = 192000;
662 
663 	/*
664 	 * copy into struct audio_params, required by drivers
665 	 */
666 	p.encoding = r.encoding = sc->sw_enc;
667 	p.precision = r.precision = sc->bits;
668 	p.bps = r.bps = sc->bps;
669 	p.msb = r.msb = sc->msb;
670 	p.sample_rate = r.sample_rate = sc->rate;
671 	p.channels = sc->pchan;
672 	r.channels = sc->rchan;
673 
674 	/*
675 	 * set parameters
676 	 */
677 	error = sc->ops->set_params(sc->arg, sc->mode, sc->mode, &p, &r);
678 	if (error)
679 		return error;
680 	if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) {
681 		if (p.encoding != r.encoding ||
682 		    p.precision != r.precision ||
683 		    p.bps != r.bps ||
684 		    p.msb != r.msb ||
685 		    p.sample_rate != r.sample_rate) {
686 			printf("%s: different play and record parameters "
687 			    "returned by hardware\n", DEVNAME(sc));
688 			return ENODEV;
689 		}
690 	}
691 	if (sc->mode & AUMODE_PLAY) {
692 		sc->hw_enc = p.encoding;
693 		sc->bits = p.precision;
694 		sc->bps = p.bps;
695 		sc->msb = p.msb;
696 		sc->rate = p.sample_rate;
697 		sc->pchan = p.channels;
698 	}
699 	if (sc->mode & AUMODE_RECORD) {
700 		sc->hw_enc = r.encoding;
701 		sc->bits = r.precision;
702 		sc->bps = r.bps;
703 		sc->msb = r.msb;
704 		sc->rate = r.sample_rate;
705 		sc->rchan = r.channels;
706 	}
707 	if (sc->rate == 0 || sc->bps == 0 || sc->bits == 0) {
708 		printf("%s: invalid parameters returned by hardware\n",
709 		    DEVNAME(sc));
710 		return ENODEV;
711 	}
712 	if (sc->ops->commit_settings) {
713 		error = sc->ops->commit_settings(sc->arg);
714 		if (error)
715 			return error;
716 	}
717 
718 	/*
719 	 * conversion from/to exotic/dead encoding, for drivers not supporting
720 	 * linear
721 	 */
722 	switch (sc->hw_enc) {
723 	case AUDIO_ENCODING_SLINEAR_LE:
724 	case AUDIO_ENCODING_SLINEAR_BE:
725 	case AUDIO_ENCODING_ULINEAR_LE:
726 	case AUDIO_ENCODING_ULINEAR_BE:
727 		sc->sw_enc = sc->hw_enc;
728 		sc->conv_dec = sc->conv_enc = NULL;
729 		break;
730 	case AUDIO_ENCODING_ULAW:
731 #if BYTE_ORDER == LITTLE_ENDIAN
732 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
733 #else
734 		sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
735 #endif
736 		if (sc->bits == 8) {
737 			sc->conv_enc = slinear8_to_mulaw;
738 			sc->conv_dec = mulaw_to_slinear8;
739 			break;
740 		} else if (sc->bits == 24) {
741 			sc->conv_enc = slinear24_to_mulaw24;
742 			sc->conv_dec = mulaw24_to_slinear24;
743 			break;
744 		}
745 		sc->sw_enc = sc->hw_enc;
746 		sc->conv_dec = sc->conv_enc = NULL;
747 		break;
748 	default:
749 		printf("%s: setpar: enc = %d, bits = %d: emulation skipped\n",
750 		    DEVNAME(sc), sc->hw_enc, sc->bits);
751 		sc->sw_enc = sc->hw_enc;
752 		sc->conv_dec = sc->conv_enc = NULL;
753 	}
754 	audio_calc_sil(sc);
755 
756 	/*
757 	 * get least multiplier of the number of frames per block
758 	 */
759 	if (sc->ops->round_blocksize) {
760 		blk_mult = sc->ops->round_blocksize(sc->arg, 1);
761 		if (blk_mult == 0) {
762 			printf("%s: 0x%x: bad block size multiplier\n",
763 			    DEVNAME(sc), blk_mult);
764 			return ENODEV;
765 		}
766 	} else
767 		blk_mult = 1;
768 	DPRINTF("%s: hw block size multiplier: %u\n", DEVNAME(sc), blk_mult);
769 	if (sc->mode & AUMODE_PLAY) {
770 		np = blk_mult / audio_gcd(sc->pchan * sc->bps, blk_mult);
771 		if (!(sc->mode & AUMODE_RECORD))
772 			nr = np;
773 		DPRINTF("%s: play number of frames multiplier: %u\n",
774 		    DEVNAME(sc), np);
775 	}
776 	if (sc->mode & AUMODE_RECORD) {
777 		nr = blk_mult / audio_gcd(sc->rchan * sc->bps, blk_mult);
778 		if (!(sc->mode & AUMODE_PLAY))
779 			np = nr;
780 		DPRINTF("%s: record number of frames multiplier: %u\n",
781 		    DEVNAME(sc), nr);
782 	}
783 	mult = nr * np / audio_gcd(nr, np);
784 	DPRINTF("%s: least common number of frames multiplier: %u\n",
785 	    DEVNAME(sc), mult);
786 
787 	/*
788 	 * get minumum and maximum frames per block
789 	 */
790 	if (sc->ops->round_blocksize)
791 		blk_max = sc->ops->round_blocksize(sc->arg, AUDIO_BUFSZ);
792 	else
793 		blk_max = AUDIO_BUFSZ;
794 	if ((sc->mode & AUMODE_PLAY) && blk_max > sc->play.datalen / 2)
795 		blk_max = sc->play.datalen / 2;
796 	if ((sc->mode & AUMODE_RECORD) && blk_max > sc->rec.datalen / 2)
797 		blk_max = sc->rec.datalen / 2;
798 	if (sc->mode & AUMODE_PLAY) {
799 		np = blk_max / (sc->pchan * sc->bps);
800 		if (!(sc->mode & AUMODE_RECORD))
801 			nr = np;
802 	}
803 	if (sc->mode & AUMODE_RECORD) {
804 		nr = blk_max / (sc->rchan * sc->bps);
805 		if (!(sc->mode & AUMODE_PLAY))
806 			np = nr;
807 	}
808 	max = np < nr ? np : nr;
809 	max -= max % mult;
810 	min = sc->rate / 1000 + mult - 1;
811 	min -= min % mult;
812 	DPRINTF("%s: frame number range: %u..%u\n", DEVNAME(sc), min, max);
813 	if (max < min) {
814 		printf("%s: %u: bad max frame number\n", DEVNAME(sc), max);
815 		return EIO;
816 	}
817 
818 	/*
819 	 * adjust the frame per block to match our constraints
820 	 */
821 	sc->round += mult / 2;
822 	sc->round -= sc->round % mult;
823 	if (sc->round > max)
824 		sc->round = max;
825 	if (sc->round < min)
826 		sc->round = min;
827 	sc->round = sc->round;
828 
829 	/*
830 	 * set buffer size (number of blocks)
831 	 */
832 	if (sc->mode & AUMODE_PLAY) {
833 		sc->play.blksz = sc->round * sc->pchan * sc->bps;
834 		max = sc->play.datalen / sc->play.blksz;
835 		if (sc->nblks > max)
836 			sc->nblks = max;
837 		if (sc->nblks < 2)
838 			sc->nblks = 2;
839 		sc->play.len = sc->nblks * sc->play.blksz;
840 		sc->nblks = sc->nblks;
841 	}
842 	if (sc->mode & AUMODE_RECORD) {
843 		/*
844 		 * for recording, buffer size is not the latency (it's
845 		 * exactly one block), so let's get the maximum buffer
846 		 * size of maximum reliability during xruns
847 		 */
848 		sc->rec.blksz = sc->round * sc->rchan * sc->bps;
849 		sc->rec.len = sc->rec.datalen;
850 		sc->rec.len -= sc->rec.datalen % sc->rec.blksz;
851 	}
852 
853 	DPRINTF("%s: setpar: new enc=%d bits=%d, bps=%d, msb=%d "
854 	    "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
855 	    DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
856 	    sc->rate, sc->pchan, sc->rchan, sc->round, sc->nblks);
857 	return 0;
858 }
859 
860 int
861 audio_ioc_start(struct audio_softc *sc)
862 {
863 	if (!sc->pause) {
864 		DPRINTF("%s: can't start: already started\n", DEVNAME(sc));
865 		return EBUSY;
866 	}
867 	if ((sc->mode & AUMODE_PLAY) && sc->play.used != sc->play.len) {
868 		DPRINTF("%s: play buffer not ready\n", DEVNAME(sc));
869 		return EBUSY;
870 	}
871 	if ((sc->mode & AUMODE_RECORD) && sc->rec.used != 0) {
872 		DPRINTF("%s: record buffer not ready\n", DEVNAME(sc));
873 		return EBUSY;
874 	}
875 	sc->pause = 0;
876 	return audio_start(sc);
877 }
878 
879 int
880 audio_ioc_stop(struct audio_softc *sc)
881 {
882 	if (sc->pause) {
883 		DPRINTF("%s: can't stop: not started\n", DEVNAME(sc));
884 		return EBUSY;
885 	}
886 	sc->pause = 1;
887 	if (sc->active)
888 		return audio_stop(sc);
889 	return 0;
890 }
891 
892 int
893 audio_ioc_getpar(struct audio_softc *sc, struct audio_swpar *p)
894 {
895 	p->rate = sc->rate;
896 	p->sig = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE ||
897 	    sc->sw_enc == AUDIO_ENCODING_SLINEAR_BE;
898 	p->le = sc->sw_enc == AUDIO_ENCODING_SLINEAR_LE ||
899 	    sc->sw_enc == AUDIO_ENCODING_ULINEAR_LE;
900 	p->bits = sc->bits;
901 	p->bps = sc->bps;
902 	p->msb = sc->msb;
903 	p->pchan = sc->pchan;
904 	p->rchan = sc->rchan;
905 	p->nblks = sc->nblks;
906 	p->round = sc->round;
907 	return 0;
908 }
909 
910 int
911 audio_ioc_setpar(struct audio_softc *sc, struct audio_swpar *p)
912 {
913 	int error, le, sig;
914 
915 	if (sc->active) {
916 		DPRINTF("%s: can't change params during dma\n",
917 		    DEVNAME(sc));
918 		return EBUSY;
919 	}
920 
921 	/*
922 	 * copy desired parameters into the softc structure
923 	 */
924 	if (p->sig != ~0U || p->le != ~0U || p->bits != ~0U) {
925 		sig = 1;
926 		le = (BYTE_ORDER == LITTLE_ENDIAN);
927 		sc->bits = 16;
928 		sc->bps = 2;
929 		sc->msb = 1;
930 		if (p->sig != ~0U)
931 			sig = p->sig;
932 		if (p->le != ~0U)
933 			le = p->le;
934 		if (p->bits != ~0U) {
935 			sc->bits = p->bits;
936 			sc->bps = sc->bits <= 8 ?
937 			    1 : (sc->bits <= 16 ? 2 : 4);
938 			if (p->bps != ~0U)
939 				sc->bps = p->bps;
940 			if (p->msb != ~0U)
941 				sc->msb = p->msb ? 1 : 0;
942 		}
943 		sc->sw_enc = (sig) ?
944 		    (le ? AUDIO_ENCODING_SLINEAR_LE :
945 			AUDIO_ENCODING_SLINEAR_BE) :
946 		    (le ? AUDIO_ENCODING_ULINEAR_LE :
947 			AUDIO_ENCODING_ULINEAR_BE);
948 	}
949 	if (p->rate != ~0)
950 		sc->rate = p->rate;
951 	if (p->pchan != ~0)
952 		sc->pchan = p->pchan;
953 	if (p->rchan != ~0)
954 		sc->rchan = p->rchan;
955 	if (p->round != ~0)
956 		sc->round = p->round;
957 	if (p->nblks != ~0)
958 		sc->nblks = p->nblks;
959 
960 	/*
961 	 * if the device is not opened for playback or recording don't
962 	 * touch the hardware yet (ex. if this is /dev/audioctlN)
963 	 */
964 	if (sc->mode == 0)
965 		return 0;
966 
967 	/*
968 	 * negociate parameters with the hardware
969 	 */
970 	error = audio_setpar(sc);
971 	if (error)
972 		return error;
973 	audio_clear(sc);
974 	if ((sc->mode & AUMODE_PLAY) && sc->ops->init_output) {
975 		error = sc->ops->init_output(sc->arg,
976 		    sc->play.data, sc->play.len);
977 		if (error)
978 			return error;
979 	}
980 	if ((sc->mode & AUMODE_RECORD) && sc->ops->init_input) {
981 		error = sc->ops->init_input(sc->arg,
982 		    sc->rec.data, sc->rec.len);
983 		if (error)
984 			return error;
985 	}
986 	return 0;
987 }
988 
989 int
990 audio_ioc_getstatus(struct audio_softc *sc, struct audio_status *p)
991 {
992 	p->mode = sc->mode;
993 	p->pause = sc->pause;
994 	p->active = sc->active;
995 	return 0;
996 }
997 
998 int
999 audio_match(struct device *parent, void *match, void *aux)
1000 {
1001 	struct audio_attach_args *sa = aux;
1002 
1003 	return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
1004 }
1005 
1006 void
1007 audio_attach(struct device *parent, struct device *self, void *aux)
1008 {
1009 	struct audio_softc *sc = (void *)self;
1010 	struct audio_attach_args *sa = aux;
1011 	struct audio_hw_if *ops = sa->hwif;
1012 	void *arg = sa->hdl;
1013 	int error;
1014 
1015 	printf("\n");
1016 
1017 #ifdef DIAGNOSTIC
1018 	if (ops == 0 ||
1019 	    ops->open == 0 ||
1020 	    ops->close == 0 ||
1021 	    ops->set_params == 0 ||
1022 	    (ops->start_output == 0 && ops->trigger_output == 0) ||
1023 	    (ops->start_input == 0 && ops->trigger_input == 0) ||
1024 	    ops->halt_output == 0 ||
1025 	    ops->halt_input == 0 ||
1026 	    ops->set_port == 0 ||
1027 	    ops->get_port == 0 ||
1028 	    ops->query_devinfo == 0 ||
1029 	    ops->get_props == 0) {
1030 		printf("%s: missing method\n", DEVNAME(sc));
1031 		sc->ops = 0;
1032 		return;
1033 	}
1034 #endif
1035 	sc->ops = ops;
1036 	sc->arg = arg;
1037 
1038 #if NWSKBD > 0
1039 	wskbd_mixer_init(sc);
1040 #endif /* NWSKBD > 0 */
1041 
1042 	error = audio_buf_init(sc, &sc->play, AUMODE_PLAY);
1043 	if (error) {
1044 		sc->ops = 0;
1045 		printf("%s: could not allocate play buffer\n", DEVNAME(sc));
1046 		return;
1047 	}
1048 	error = audio_buf_init(sc, &sc->rec, AUMODE_RECORD);
1049 	if (error) {
1050 		audio_buf_done(sc, &sc->play);
1051 		sc->ops = 0;
1052 		printf("%s: could not allocate record buffer\n", DEVNAME(sc));
1053 		return;
1054 	}
1055 
1056 	/* set defaults */
1057 #if BYTE_ORDER == LITTLE_ENDIAN
1058 	sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
1059 #else
1060 	sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
1061 #endif
1062 	sc->bits = 16;
1063 	sc->bps = 2;
1064 	sc->msb = 1;
1065 	sc->rate = 48000;
1066 	sc->pchan = 2;
1067 	sc->rchan = 2;
1068 	sc->round = 960;
1069 	sc->nblks = 2;
1070 	sc->play.pos = sc->play.xrun = sc->rec.pos = sc->rec.xrun = 0;
1071 }
1072 
1073 int
1074 audio_activate(struct device *self, int act)
1075 {
1076 	struct audio_softc *sc = (struct audio_softc *)self;
1077 
1078 	switch (act) {
1079 	case DVACT_QUIESCE:
1080 		/*
1081 		 * good drivers run play and rec handlers in a single
1082 		 * interrupt. Grab the lock to ensure we expose the same
1083 		 * sc->quiesce value to both play and rec handlers
1084 		 */
1085 		mtx_enter(&audio_lock);
1086 		sc->quiesce = 1;
1087 		mtx_leave(&audio_lock);
1088 
1089 		/*
1090 		 * once sc->quiesce is set, interrupts may occur, but
1091 		 * counters are not advanced and consequently processes
1092 		 * keep sleeping.
1093 		 *
1094 		 * XXX: ensure read/write/ioctl don't start/stop
1095 		 * DMA at the same time, this needs a "ready" condvar
1096 		 */
1097 		if (sc->mode != 0 && sc->active)
1098 			audio_stop_do(sc);
1099 		DPRINTF("%s: quesce: active = %d\n", DEVNAME(sc), sc->active);
1100 		break;
1101 	case DVACT_WAKEUP:
1102 		DPRINTF("%s: wakeup: active = %d\n", DEVNAME(sc), sc->active);
1103 
1104 		/*
1105 		 * keep buffer usage the same, but set start pointer to
1106 		 * the beginning of the buffer.
1107 		 *
1108 		 * No need to grab the audio_lock as DMA is stopped and
1109 		 * this is the only thread running (caller ensures this)
1110 		 */
1111 		sc->quiesce = 0;
1112 		wakeup(&sc->quiesce);
1113 
1114 		if(sc->mode != 0) {
1115 			if (audio_setpar(sc) != 0)
1116 				break;
1117 			if (sc->mode & AUMODE_PLAY) {
1118 				sc->play.start = 0;
1119 				audio_fill_sil(sc, sc->play.data, sc->play.len);
1120 			}
1121 			if (sc->mode & AUMODE_RECORD) {
1122 				sc->rec.start = sc->rec.len - sc->rec.used;
1123 				audio_fill_sil(sc, sc->rec.data, sc->rec.len);
1124 			}
1125 			if (sc->active)
1126 				audio_start_do(sc);
1127 		}
1128 		break;
1129 	}
1130 	return 0;
1131 }
1132 
1133 int
1134 audio_detach(struct device *self, int flags)
1135 {
1136 	struct audio_softc *sc = (struct audio_softc *)self;
1137 	int maj, mn;
1138 
1139 	DPRINTF("%s: audio_detach: flags = %d\n", DEVNAME(sc), flags);
1140 
1141 	wakeup(&sc->quiesce);
1142 
1143 	/* locate the major number */
1144 	for (maj = 0; maj < nchrdev; maj++)
1145 		if (cdevsw[maj].d_open == audioopen)
1146 			break;
1147 	/*
1148 	 * Nuke the vnodes for any open instances, calls close but as
1149 	 * close uses device_lookup, it returns EXIO and does nothing
1150 	 */
1151 	mn = self->dv_unit;
1152 	vdevgone(maj, mn | AUDIO_DEV_SOUND, mn | AUDIO_DEV_SOUND, VCHR);
1153 	vdevgone(maj, mn | AUDIO_DEV_AUDIO, mn | AUDIO_DEV_AUDIO, VCHR);
1154 	vdevgone(maj, mn | AUDIO_DEV_AUDIOCTL, mn | AUDIO_DEV_AUDIOCTL, VCHR);
1155 	vdevgone(maj, mn | AUDIO_DEV_MIXER, mn | AUDIO_DEV_MIXER, VCHR);
1156 
1157 	/*
1158 	 * The close() method did nothing, quickly halt DMA (normally
1159 	 * parent is already gone, and code below is no-op), and wake-up
1160 	 * user-land blocked in read/write/ioctl, which return EIO.
1161 	 */
1162 	if (sc->mode != 0) {
1163 		if (sc->active) {
1164 			wakeup(&sc->play.blocking);
1165 			selwakeup(&sc->play.sel);
1166 			wakeup(&sc->rec.blocking);
1167 			selwakeup(&sc->rec.sel);
1168 			audio_stop(sc);
1169 		}
1170 		sc->ops->close(sc->arg);
1171 		sc->mode = 0;
1172 	}
1173 
1174 	/* free resources */
1175 	audio_buf_done(sc, &sc->play);
1176 	audio_buf_done(sc, &sc->rec);
1177 	return 0;
1178 }
1179 
1180 int
1181 audio_submatch(struct device *parent, void *match, void *aux)
1182 {
1183         struct cfdata *cf = match;
1184 
1185 	return (cf->cf_driver == &audio_cd);
1186 }
1187 
1188 struct device *
1189 audio_attach_mi(struct audio_hw_if *ops, void *arg, struct device *dev)
1190 {
1191 	struct audio_attach_args aa;
1192 
1193 	aa.type = AUDIODEV_TYPE_AUDIO;
1194 	aa.hwif = ops;
1195 	aa.hdl = arg;
1196 
1197 	/*
1198 	 * attach this driver to the caller (hardware driver), this
1199 	 * checks the kernel config and possibly calls audio_attach()
1200 	 */
1201 	return config_found_sm(dev, &aa, audioprint, audio_submatch);
1202 }
1203 
1204 int
1205 audioprint(void *aux, const char *pnp)
1206 {
1207 	struct audio_attach_args *arg = aux;
1208 	const char *type;
1209 
1210 	if (pnp != NULL) {
1211 		switch (arg->type) {
1212 		case AUDIODEV_TYPE_AUDIO:
1213 			type = "audio";
1214 			break;
1215 		case AUDIODEV_TYPE_OPL:
1216 			type = "opl";
1217 			break;
1218 		case AUDIODEV_TYPE_MPU:
1219 			type = "mpu";
1220 			break;
1221 		default:
1222 			panic("audioprint: unknown type %d", arg->type);
1223 		}
1224 		printf("%s at %s", type, pnp);
1225 	}
1226 	return UNCONF;
1227 }
1228 
1229 int
1230 audio_open(struct audio_softc *sc, int flags)
1231 {
1232 	int error;
1233 	int props;
1234 
1235 	if (sc->mode)
1236 		return EBUSY;
1237 	error = sc->ops->open(sc->arg, flags);
1238 	if (error)
1239 		return error;
1240 	sc->active = 0;
1241 	sc->pause = 1;
1242 	sc->rec.blocking = 0;
1243 	sc->play.blocking = 0;
1244 	sc->mode = 0;
1245 	if (flags & FWRITE)
1246 		sc->mode |= AUMODE_PLAY;
1247 	if (flags & FREAD)
1248 		sc->mode |= AUMODE_RECORD;
1249 	props = sc->ops->get_props(sc->arg);
1250 	if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) {
1251 		if (!(props & AUDIO_PROP_FULLDUPLEX)) {
1252 			error = ENOTTY;
1253 			goto bad;
1254 		}
1255 		if (sc->ops->setfd) {
1256 			error = sc->ops->setfd(sc->arg, 1);
1257 			if (error)
1258 				goto bad;
1259 		}
1260 	}
1261 
1262 	if (sc->ops->speaker_ctl) {
1263 		/*
1264 		 * XXX: what is this used for?
1265 		 */
1266 		sc->ops->speaker_ctl(sc->arg,
1267 		    (sc->mode & AUMODE_PLAY) ? SPKR_ON : SPKR_OFF);
1268 	}
1269 
1270 	error = audio_setpar(sc);
1271 	if (error)
1272 		goto bad;
1273 	audio_clear(sc);
1274 
1275 	/*
1276 	 * allow read(2)/write(2) to automatically start DMA, without
1277 	 * the need for ioctl(), to make /dev/audio usable in scripts
1278 	 */
1279 	sc->pause = 0;
1280 	return 0;
1281 bad:
1282 	sc->ops->close(sc->arg);
1283 	sc->mode = 0;
1284 	return error;
1285 }
1286 
1287 int
1288 audio_drain(struct audio_softc *sc)
1289 {
1290 	int error, xrun;
1291 	unsigned char *ptr;
1292 	size_t count, bpf;
1293 
1294 	DPRINTF("%s: drain: mode = %d, pause = %d, active = %d, used = %zu\n",
1295 	    DEVNAME(sc), sc->mode, sc->pause, sc->active, sc->play.used);
1296 	if (!(sc->mode & AUMODE_PLAY) || sc->pause)
1297 		return 0;
1298 
1299 	/* discard partial samples, required by audio_fill_sil() */
1300 	mtx_enter(&audio_lock);
1301 	bpf = sc->pchan * sc->bps;
1302 	sc->play.used -= sc->play.used % bpf;
1303 	if (sc->play.used == 0) {
1304 		mtx_leave(&audio_lock);
1305 		return 0;
1306 	}
1307 
1308 	if (!sc->active) {
1309 		/*
1310 		 * dma not started yet because buffer was not full
1311 		 * enough to start automatically. Pad it and start now.
1312 		 */
1313 		for (;;) {
1314 			ptr = audio_buf_wgetblk(&sc->play, &count);
1315 			if (count == 0)
1316 				break;
1317 			audio_fill_sil(sc, ptr, count);
1318 			audio_buf_wcommit(&sc->play, count);
1319 		}
1320 		mtx_leave(&audio_lock);
1321 		error = audio_start(sc);
1322 		if (error)
1323 			return error;
1324 		mtx_enter(&audio_lock);
1325 	}
1326 
1327 	xrun = sc->play.xrun;
1328 	while (sc->play.xrun == xrun) {
1329 		DPRINTF("%s: drain: used = %zu, xrun = %d\n",
1330 		    DEVNAME(sc), sc->play.used, sc->play.xrun);
1331 
1332 		/*
1333 		 * set a 5 second timeout, in case interrupts don't
1334 		 * work, useful only for debugging drivers
1335 		 */
1336 		sc->play.blocking = 1;
1337 		error = msleep(&sc->play.blocking, &audio_lock,
1338 		    PWAIT | PCATCH, "au_dr", 5 * hz);
1339 		if (!(sc->dev.dv_flags & DVF_ACTIVE))
1340 			error = EIO;
1341 		if (error) {
1342 			DPRINTF("%s: drain, err = %d\n", DEVNAME(sc), error);
1343 			break;
1344 		}
1345 	}
1346 	mtx_leave(&audio_lock);
1347 	return error;
1348 }
1349 
1350 int
1351 audio_close(struct audio_softc *sc)
1352 {
1353 	audio_drain(sc);
1354 	if (sc->active)
1355 		audio_stop(sc);
1356 	sc->ops->close(sc->arg);
1357 	sc->mode = 0;
1358 	DPRINTF("%s: close: done\n", DEVNAME(sc));
1359 	return 0;
1360 }
1361 
1362 int
1363 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag)
1364 {
1365 	unsigned char *ptr;
1366 	size_t count;
1367 	int error;
1368 
1369 	DPRINTFN(1, "%s: read: resid = %zd\n", DEVNAME(sc), uio->uio_resid);
1370 
1371 	/* block if quiesced */
1372 	while (sc->quiesce)
1373 		tsleep(&sc->quiesce, 0, "au_qrd", 0);
1374 
1375 	/* start automatically if audio_ioc_start() was never called */
1376 	mtx_enter(&audio_lock);
1377 	if (!sc->active && !sc->pause && sc->rec.used == 0) {
1378 		mtx_leave(&audio_lock);
1379 		error = audio_start(sc);
1380 		if (error)
1381 			return error;
1382 		mtx_enter(&audio_lock);
1383 	}
1384 
1385 	/* if there is no data then sleep */
1386 	while (sc->rec.used == 0) {
1387 		if (ioflag & IO_NDELAY) {
1388 			mtx_leave(&audio_lock);
1389 			return EWOULDBLOCK;
1390 		}
1391 		DPRINTFN(1, "%s: read sleep\n", DEVNAME(sc));
1392 		sc->rec.blocking = 1;
1393 		error = msleep(&sc->rec.blocking,
1394 		    &audio_lock, PWAIT | PCATCH, "au_rd", 0);
1395 		if (!(sc->dev.dv_flags & DVF_ACTIVE))
1396 			error = EIO;
1397 #ifdef AUDIO_DEBUG
1398 		if (error) {
1399 			DPRINTF("%s: read woke up error = %d\n",
1400 			    DEVNAME(sc), error);
1401 		}
1402 #endif
1403 		if (error) {
1404 			mtx_leave(&audio_lock);
1405 			return error;
1406 		}
1407 	}
1408 
1409 	/* at this stage, there is data to transfer */
1410 	while (uio->uio_resid > 0 && sc->rec.used > 0) {
1411 		ptr = audio_buf_rgetblk(&sc->rec, &count);
1412 		if (count > uio->uio_resid)
1413 			count = uio->uio_resid;
1414 		mtx_leave(&audio_lock);
1415 		DPRINTFN(1, "%s: read: start = %zu, count = %zu\n",
1416 		    DEVNAME(sc), ptr - sc->rec.data, count);
1417 		if (sc->conv_dec)
1418 			sc->conv_dec(ptr, count);
1419 		error = uiomove(ptr, count, uio);
1420 		if (error)
1421 			return error;
1422 		mtx_enter(&audio_lock);
1423 		audio_buf_rdiscard(&sc->rec, count);
1424 	}
1425 	mtx_leave(&audio_lock);
1426 	return 0;
1427 }
1428 
1429 int
1430 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag)
1431 {
1432 	unsigned char *ptr;
1433 	size_t count;
1434 	int error;
1435 
1436 	DPRINTFN(1, "%s: write: resid = %zd\n",  DEVNAME(sc), uio->uio_resid);
1437 
1438 	/* block if quiesced */
1439 	while (sc->quiesce)
1440 		tsleep(&sc->quiesce, 0, "au_qwr", 0);
1441 
1442 	/*
1443 	 * if IO_NDELAY flag is set then check if there is enough room
1444 	 * in the buffer to store at least one byte. If not then dont
1445 	 * start the write process.
1446 	 */
1447 	mtx_enter(&audio_lock);
1448 	if (uio->uio_resid > 0 && (ioflag & IO_NDELAY)) {
1449 		if (sc->play.used == sc->play.len) {
1450 			mtx_leave(&audio_lock);
1451 			return EWOULDBLOCK;
1452 		}
1453 	}
1454 
1455 	while (uio->uio_resid > 0) {
1456 		while (1) {
1457 			ptr = audio_buf_wgetblk(&sc->play, &count);
1458 			if (count > 0)
1459 				break;
1460 			if (ioflag & IO_NDELAY) {
1461 				/*
1462 				 * At this stage at least one byte is already
1463 				 * moved so we do not return EWOULDBLOCK
1464 				 */
1465 				mtx_leave(&audio_lock);
1466 				return 0;
1467 			}
1468 			DPRINTFN(1, "%s: write sleep\n", DEVNAME(sc));
1469 			sc->play.blocking = 1;
1470 			error = msleep(&sc->play.blocking,
1471 			    &audio_lock, PWAIT | PCATCH, "au_wr", 0);
1472 			if (!(sc->dev.dv_flags & DVF_ACTIVE))
1473 				error = EIO;
1474 #ifdef AUDIO_DEBUG
1475 			if (error) {
1476 				DPRINTF("%s: write woke up error = %d\n",
1477 				    DEVNAME(sc), error);
1478 			}
1479 #endif
1480 			if (error) {
1481 				mtx_leave(&audio_lock);
1482 				return error;
1483 			}
1484 		}
1485 		if (count > uio->uio_resid)
1486 			count = uio->uio_resid;
1487 		mtx_leave(&audio_lock);
1488 		error = uiomove(ptr, count, uio);
1489 		if (error)
1490 			return 0;
1491 		if (sc->conv_enc) {
1492 			sc->conv_enc(ptr, count);
1493 			DPRINTFN(1, "audio_write: converted count = %zu\n",
1494 			    count);
1495 		}
1496 		mtx_enter(&audio_lock);
1497 		audio_buf_wcommit(&sc->play, count);
1498 
1499 		/* start automatically if audio_ioc_start() was never called */
1500 		if (!sc->active && !sc->pause &&
1501 		    sc->play.used == sc->play.len) {
1502 			mtx_leave(&audio_lock);
1503 			error = audio_start(sc);
1504 			if (error)
1505 				return error;
1506 			mtx_enter(&audio_lock);
1507 		}
1508 	}
1509 	mtx_leave(&audio_lock);
1510 	return 0;
1511 }
1512 
1513 int
1514 audio_getdev(struct audio_softc *sc, struct audio_device *adev)
1515 {
1516 	memset(adev, 0, sizeof(struct audio_device));
1517 	if (sc->dev.dv_parent == NULL)
1518 		return EIO;
1519 	strlcpy(adev->name, sc->dev.dv_parent->dv_xname, MAX_AUDIO_DEV_LEN);
1520 	return 0;
1521 }
1522 
1523 int
1524 audio_ioctl(struct audio_softc *sc, unsigned long cmd, void *addr)
1525 {
1526 	struct audio_pos *ap;
1527 	int error = 0;
1528 
1529 	/* block if quiesced */
1530 	while (sc->quiesce)
1531 		tsleep(&sc->quiesce, 0, "au_qio", 0);
1532 
1533 	switch (cmd) {
1534 	case FIONBIO:
1535 		/* All handled in the upper FS layer. */
1536 		break;
1537 	case AUDIO_GETPOS:
1538 		mtx_enter(&audio_lock);
1539 		ap = (struct audio_pos *)addr;
1540 		ap->play_pos = sc->play.pos;
1541 		ap->play_xrun = sc->play.xrun;
1542 		ap->rec_pos = sc->rec.pos;
1543 		ap->rec_xrun = sc->rec.xrun;
1544 		mtx_leave(&audio_lock);
1545 		break;
1546 	case AUDIO_START:
1547 		return audio_ioc_start(sc);
1548 	case AUDIO_STOP:
1549 		return audio_ioc_stop(sc);
1550 	case AUDIO_SETPAR:
1551 		error = audio_ioc_setpar(sc, (struct audio_swpar *)addr);
1552 		break;
1553 	case AUDIO_GETPAR:
1554 		error = audio_ioc_getpar(sc, (struct audio_swpar *)addr);
1555 		break;
1556 	case AUDIO_GETSTATUS:
1557 		error = audio_ioc_getstatus(sc, (struct audio_status *)addr);
1558 		break;
1559 	case AUDIO_GETDEV:
1560 		error = audio_getdev(sc, (struct audio_device *)addr);
1561 		break;
1562 	default:
1563 		DPRINTF("%s: unknown ioctl 0x%lx\n", DEVNAME(sc), cmd);
1564 		error = ENOTTY;
1565 		break;
1566 	}
1567 	return error;
1568 }
1569 
1570 int
1571 audio_ioctl_mixer(struct audio_softc *sc, unsigned long cmd, void *addr)
1572 {
1573 	int error;
1574 
1575 	/* block if quiesced */
1576 	while (sc->quiesce)
1577 		tsleep(&sc->quiesce, 0, "mix_qio", 0);
1578 
1579 	switch (cmd) {
1580 	case FIONBIO:
1581 		/* All handled in the upper FS layer. */
1582 		break;
1583 	case AUDIO_MIXER_DEVINFO:
1584 		((mixer_devinfo_t *)addr)->un.v.delta = 0;
1585 		return sc->ops->query_devinfo(sc->arg, (mixer_devinfo_t *)addr);
1586 	case AUDIO_MIXER_READ:
1587 		return sc->ops->get_port(sc->arg, (mixer_ctrl_t *)addr);
1588 	case AUDIO_MIXER_WRITE:
1589 		error = sc->ops->set_port(sc->arg, (mixer_ctrl_t *)addr);
1590 		if (error)
1591 			return error;
1592 		if (sc->ops->commit_settings)
1593 			return sc->ops->commit_settings(sc->arg);
1594 		break;
1595 	default:
1596 		return ENOTTY;
1597 	}
1598 	return 0;
1599 }
1600 
1601 int
1602 audio_poll(struct audio_softc *sc, int events, struct proc *p)
1603 {
1604 	int revents = 0;
1605 
1606 	mtx_enter(&audio_lock);
1607 	if ((sc->mode & AUMODE_RECORD) && sc->rec.used > 0)
1608 		revents |= events & (POLLIN | POLLRDNORM);
1609 	if ((sc->mode & AUMODE_PLAY) && sc->play.used < sc->play.len)
1610 		revents |= events & (POLLOUT | POLLWRNORM);
1611 	if (revents == 0) {
1612 		if (events & (POLLIN | POLLRDNORM))
1613 			selrecord(p, &sc->rec.sel);
1614 		if (events & (POLLOUT | POLLWRNORM))
1615 			selrecord(p, &sc->play.sel);
1616 	}
1617 	mtx_leave(&audio_lock);
1618 	return revents;
1619 }
1620 
1621 int
1622 audioopen(dev_t dev, int flags, int mode, struct proc *p)
1623 {
1624 	struct audio_softc *sc;
1625 	int error;
1626 
1627 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1628 	if (sc == NULL)
1629 		return ENXIO;
1630 	if (sc->ops == NULL)
1631 		error = ENXIO;
1632 	else {
1633 		switch (AUDIO_DEV(dev)) {
1634 		case AUDIO_DEV_SOUND:
1635 		case AUDIO_DEV_AUDIO:
1636 			error = audio_open(sc, flags);
1637 			break;
1638 		case AUDIO_DEV_AUDIOCTL:
1639 		case AUDIO_DEV_MIXER:
1640 			error = 0;
1641 			break;
1642 		default:
1643 			error = ENXIO;
1644 		}
1645 	}
1646 	device_unref(&sc->dev);
1647 	return error;
1648 }
1649 
1650 int
1651 audioclose(dev_t dev, int flags, int ifmt, struct proc *p)
1652 {
1653 	struct audio_softc *sc;
1654 	int error;
1655 
1656 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1657 	if (sc == NULL)
1658 		return ENXIO;
1659 	switch (AUDIO_DEV(dev)) {
1660 	case AUDIO_DEV_SOUND:
1661 	case AUDIO_DEV_AUDIO:
1662 		error = audio_close(sc);
1663 		break;
1664 	case AUDIO_DEV_MIXER:
1665 	case AUDIO_DEV_AUDIOCTL:
1666 		error = 0;
1667 		break;
1668 	default:
1669 		error = ENXIO;
1670 	}
1671 	device_unref(&sc->dev);
1672 	return error;
1673 }
1674 
1675 int
1676 audioread(dev_t dev, struct uio *uio, int ioflag)
1677 {
1678 	struct audio_softc *sc;
1679 	int error;
1680 
1681 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1682 	if (sc == NULL)
1683 		return ENXIO;
1684 	switch (AUDIO_DEV(dev)) {
1685 	case AUDIO_DEV_SOUND:
1686 	case AUDIO_DEV_AUDIO:
1687 		error = audio_read(sc, uio, ioflag);
1688 		break;
1689 	case AUDIO_DEV_AUDIOCTL:
1690 	case AUDIO_DEV_MIXER:
1691 		error = ENODEV;
1692 		break;
1693 	default:
1694 		error = ENXIO;
1695 	}
1696 	device_unref(&sc->dev);
1697 	return error;
1698 }
1699 
1700 int
1701 audiowrite(dev_t dev, struct uio *uio, int ioflag)
1702 {
1703 	struct audio_softc *sc;
1704 	int error;
1705 
1706 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1707 	if (sc == NULL)
1708 		return ENXIO;
1709 	switch (AUDIO_DEV(dev)) {
1710 	case AUDIO_DEV_SOUND:
1711 	case AUDIO_DEV_AUDIO:
1712 		error = audio_write(sc, uio, ioflag);
1713 		break;
1714 	case AUDIO_DEV_AUDIOCTL:
1715 	case AUDIO_DEV_MIXER:
1716 		error = ENODEV;
1717 		break;
1718 	default:
1719 		error = ENXIO;
1720 	}
1721 	device_unref(&sc->dev);
1722 	return error;
1723 }
1724 
1725 int
1726 audioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1727 {
1728 	struct audio_softc *sc;
1729 	int error;
1730 
1731 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1732 	if (sc == NULL)
1733 		return ENXIO;
1734 	switch (AUDIO_DEV(dev)) {
1735 	case AUDIO_DEV_SOUND:
1736 	case AUDIO_DEV_AUDIO:
1737 		error = audio_ioctl(sc, cmd, addr);
1738 		break;
1739 	case AUDIO_DEV_AUDIOCTL:
1740 		if (cmd == AUDIO_SETPAR && sc->mode != 0) {
1741 			error = EBUSY;
1742 			break;
1743 		}
1744 		if (cmd == AUDIO_START || cmd == AUDIO_STOP) {
1745 			error = ENXIO;
1746 			break;
1747 		}
1748 		error = audio_ioctl(sc, cmd, addr);
1749 		break;
1750 	case AUDIO_DEV_MIXER:
1751 		error = audio_ioctl_mixer(sc, cmd, addr);
1752 		break;
1753 	default:
1754 		error = ENXIO;
1755 	}
1756 	device_unref(&sc->dev);
1757 	return error;
1758 }
1759 
1760 int
1761 audiopoll(dev_t dev, int events, struct proc *p)
1762 {
1763 	struct audio_softc *sc;
1764 	int revents;
1765 
1766 	sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
1767 	if (sc == NULL)
1768 		return POLLERR;
1769 	switch (AUDIO_DEV(dev)) {
1770 	case AUDIO_DEV_SOUND:
1771 	case AUDIO_DEV_AUDIO:
1772 		revents = audio_poll(sc, events, p);
1773 		break;
1774 	case AUDIO_DEV_AUDIOCTL:
1775 	case AUDIO_DEV_MIXER:
1776 		revents = 0;
1777 		break;
1778 	default:
1779 		revents = 0;
1780 	}
1781 	device_unref(&sc->dev);
1782 	return revents;
1783 }
1784 
1785 #if NWSKBD > 0
1786 int
1787 wskbd_initmute(struct audio_softc *sc, struct mixer_devinfo *vol)
1788 {
1789 	struct mixer_devinfo mi;
1790 
1791 	mi.index = vol->next;
1792 	for (mi.index = vol->next; mi.index != -1; mi.index = mi.next) {
1793 		if (sc->ops->query_devinfo(sc->arg, &mi) != 0)
1794 			break;
1795 		if (strcmp(mi.label.name, AudioNmute) == 0)
1796 			return mi.index;
1797 	}
1798 	return -1;
1799 }
1800 
1801 int
1802 wskbd_initvol(struct audio_softc *sc, struct wskbd_vol *vol, char *cn, char *dn)
1803 {
1804 	struct mixer_devinfo dev, cls;
1805 
1806 	for (dev.index = 0; ; dev.index++) {
1807 		if (sc->ops->query_devinfo(sc->arg, &dev) != 0)
1808 			break;
1809 		if (dev.type != AUDIO_MIXER_VALUE)
1810 			continue;
1811 		cls.index = dev.mixer_class;
1812 		if (sc->ops->query_devinfo(sc->arg, &cls) != 0)
1813 			continue;
1814 		if (strcmp(cls.label.name, cn) == 0 &&
1815 		    strcmp(dev.label.name, dn) == 0) {
1816 			vol->val = dev.index;
1817 			vol->nch = dev.un.v.num_channels;
1818 			vol->step = dev.un.v.delta > 8 ? dev.un.v.delta : 8;
1819 			vol->mute = wskbd_initmute(sc, &dev);
1820 			vol->val_pending = vol->mute_pending = 0;
1821 			DPRINTF("%s: wskbd using %s.%s%s\n",
1822 			    DEVNAME(sc), cn, dn, vol->mute >= 0 ? ", mute control" : "");
1823 			return 1;
1824 		}
1825 	}
1826 	vol->val = vol->mute = -1;
1827 	return 0;
1828 }
1829 
1830 void
1831 wskbd_mixer_init(struct audio_softc *sc)
1832 {
1833 	static struct {
1834 		char *cn, *dn;
1835 	} spkr_names[] = {
1836 		{AudioCoutputs, AudioNmaster},
1837 		{AudioCinputs,  AudioNdac},
1838 		{AudioCoutputs, AudioNdac},
1839 		{AudioCoutputs, AudioNoutput}
1840 	}, mic_names[] = {
1841 		{AudioCrecord, AudioNrecord},
1842 		{AudioCrecord, AudioNvolume},
1843 		{AudioCinputs, AudioNrecord},
1844 		{AudioCinputs, AudioNvolume},
1845 		{AudioCinputs, AudioNinput}
1846 	};
1847 	int i;
1848 
1849 	if (sc->dev.dv_unit != 0) {
1850 		DPRINTF("%s: not configuring wskbd keys\n", DEVNAME(sc));
1851 		return;
1852 	}
1853 	for (i = 0; i < sizeof(spkr_names) / sizeof(spkr_names[0]); i++) {
1854 		if (wskbd_initvol(sc, &sc->spkr,
1855 			spkr_names[i].cn, spkr_names[i].dn))
1856 			break;
1857 	}
1858 	for (i = 0; i < sizeof(mic_names) / sizeof(mic_names[0]); i++) {
1859 		if (wskbd_initvol(sc, &sc->mic,
1860 			mic_names[i].cn, mic_names[i].dn))
1861 			break;
1862 	}
1863 }
1864 
1865 void
1866 wskbd_mixer_update(struct audio_softc *sc, struct wskbd_vol *vol)
1867 {
1868 	struct mixer_ctrl ctrl;
1869 	int val_pending, mute_pending, i, gain, error, s;
1870 
1871 	s = spltty();
1872 	val_pending = vol->val_pending;
1873 	vol->val_pending = 0;
1874 	mute_pending = vol->mute_pending;
1875 	vol->mute_pending = 0;
1876 	splx(s);
1877 
1878 	if (sc->ops == NULL)
1879 		return;
1880 	if (vol->mute >= 0 && mute_pending) {
1881 		ctrl.dev = vol->mute;
1882 		ctrl.type = AUDIO_MIXER_ENUM;
1883 		error = sc->ops->get_port(sc->arg, &ctrl);
1884 		if (error) {
1885 			DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
1886 			return;
1887 		}
1888 		ctrl.un.ord = ctrl.un.ord ^ mute_pending;
1889 		DPRINTFN(1, "%s: wskbd mute setting to %d\n",
1890 		    DEVNAME(sc), ctrl.un.ord);
1891 		error = sc->ops->set_port(sc->arg, &ctrl);
1892 		if (error) {
1893 			DPRINTF("%s: set mute err = %d\n", DEVNAME(sc), error);
1894 			return;
1895 		}
1896 	}
1897 	if (vol->val >= 0 && val_pending) {
1898 		ctrl.dev = vol->val;
1899 		ctrl.type = AUDIO_MIXER_VALUE;
1900 		ctrl.un.value.num_channels = vol->nch;
1901 		error = sc->ops->get_port(sc->arg, &ctrl);
1902 		if (error) {
1903 			DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
1904 			return;
1905 		}
1906 		for (i = 0; i < vol->nch; i++) {
1907 			gain = ctrl.un.value.level[i] + vol->step * val_pending;
1908 			if (gain > AUDIO_MAX_GAIN)
1909 				gain = AUDIO_MAX_GAIN;
1910 			if (gain < AUDIO_MIN_GAIN)
1911 				gain = AUDIO_MIN_GAIN;
1912 			ctrl.un.value.level[i] = gain;
1913 			DPRINTFN(1, "%s: wskbd level %d set to %d\n",
1914 			    DEVNAME(sc), i, gain);
1915 		}
1916 		error = sc->ops->set_port(sc->arg, &ctrl);
1917 		if (error) {
1918 			DPRINTF("%s: set vol err = %d\n", DEVNAME(sc), error);
1919 			return;
1920 		}
1921 	}
1922 }
1923 
1924 void
1925 wskbd_mixer_cb(void *addr)
1926 {
1927 	struct audio_softc *sc = addr;
1928 	int s;
1929 
1930 	wskbd_mixer_update(sc, &sc->spkr);
1931 	wskbd_mixer_update(sc, &sc->mic);
1932 	s = spltty();
1933 	sc->wskbd_taskset = 0;
1934 	splx(s);
1935 	device_unref(&sc->dev);
1936 }
1937 
1938 int
1939 wskbd_set_mixervolume(long dir, long out)
1940 {
1941 	struct audio_softc *sc;
1942 	struct wskbd_vol *vol;
1943 
1944 	sc = (struct audio_softc *)device_lookup(&audio_cd, 0);
1945 	if (sc == NULL)
1946 		return ENODEV;
1947 	vol = out ? &sc->spkr : &sc->mic;
1948 	if (dir == 0)
1949 		vol->mute_pending ^= 1;
1950 	else
1951 		vol->val_pending += dir;
1952 	if (!sc->wskbd_taskset) {
1953 		task_set(&sc->wskbd_task, wskbd_mixer_cb, sc);
1954 		task_add(systq, &sc->wskbd_task);
1955 		sc->wskbd_taskset = 1;
1956 	}
1957 	return 0;
1958 }
1959 #endif /* NWSKBD > 0 */
1960