xref: /freebsd/sys/dev/sound/pcm/channel.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7  * Portions Copyright (c) Luigi Rizzo <luigi@FreeBSD.org> - 1997-99
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_isa.h"
33 
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include "opt_snd.h"
36 #endif
37 
38 #include <dev/sound/pcm/sound.h>
39 #include <dev/sound/pcm/vchan.h>
40 
41 #include "feeder_if.h"
42 
43 SND_DECLARE_FILE("$FreeBSD$");
44 
45 int report_soft_formats = 1;
46 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
47 	&report_soft_formats, 0, "report software-emulated formats");
48 
49 int report_soft_matrix = 1;
50 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_matrix, CTLFLAG_RW,
51 	&report_soft_matrix, 0, "report software-emulated channel matrixing");
52 
53 int chn_latency = CHN_LATENCY_DEFAULT;
54 
55 static int
56 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS)
57 {
58 	int err, val;
59 
60 	val = chn_latency;
61 	err = sysctl_handle_int(oidp, &val, 0, req);
62 	if (err != 0 || req->newptr == NULL)
63 		return err;
64 	if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX)
65 		err = EINVAL;
66 	else
67 		chn_latency = val;
68 
69 	return err;
70 }
71 SYSCTL_PROC(_hw_snd, OID_AUTO, latency,
72     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
73     sysctl_hw_snd_latency, "I",
74     "buffering latency (0=low ... 10=high)");
75 
76 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
77 
78 static int
79 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS)
80 {
81 	int err, val;
82 
83 	val = chn_latency_profile;
84 	err = sysctl_handle_int(oidp, &val, 0, req);
85 	if (err != 0 || req->newptr == NULL)
86 		return err;
87 	if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX)
88 		err = EINVAL;
89 	else
90 		chn_latency_profile = val;
91 
92 	return err;
93 }
94 SYSCTL_PROC(_hw_snd, OID_AUTO, latency_profile,
95     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
96     sysctl_hw_snd_latency_profile, "I",
97     "buffering latency profile (0=aggressive 1=safe)");
98 
99 static int chn_timeout = CHN_TIMEOUT;
100 
101 static int
102 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
103 {
104 	int err, val;
105 
106 	val = chn_timeout;
107 	err = sysctl_handle_int(oidp, &val, 0, req);
108 	if (err != 0 || req->newptr == NULL)
109 		return err;
110 	if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
111 		err = EINVAL;
112 	else
113 		chn_timeout = val;
114 
115 	return err;
116 }
117 SYSCTL_PROC(_hw_snd, OID_AUTO, timeout,
118     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
119     sysctl_hw_snd_timeout, "I",
120     "interrupt timeout (1 - 10) seconds");
121 
122 static int chn_vpc_autoreset = 1;
123 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_autoreset, CTLFLAG_RWTUN,
124 	&chn_vpc_autoreset, 0, "automatically reset channels volume to 0db");
125 
126 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
127 
128 static void
129 chn_vpc_proc(int reset, int db)
130 {
131 	struct snddev_info *d;
132 	struct pcm_channel *c;
133 	int i;
134 
135 	for (i = 0; pcm_devclass != NULL &&
136 	    i < devclass_get_maxunit(pcm_devclass); i++) {
137 		d = devclass_get_softc(pcm_devclass, i);
138 		if (!PCM_REGISTERED(d))
139 			continue;
140 		PCM_LOCK(d);
141 		PCM_WAIT(d);
142 		PCM_ACQUIRE(d);
143 		CHN_FOREACH(c, d, channels.pcm) {
144 			CHN_LOCK(c);
145 			CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
146 			if (reset != 0)
147 				chn_vpc_reset(c, SND_VOL_C_PCM, 1);
148 			CHN_UNLOCK(c);
149 		}
150 		PCM_RELEASE(d);
151 		PCM_UNLOCK(d);
152 	}
153 }
154 
155 static int
156 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
157 {
158 	int err, val;
159 
160 	val = chn_vol_0db_pcm;
161 	err = sysctl_handle_int(oidp, &val, 0, req);
162 	if (err != 0 || req->newptr == NULL)
163 		return (err);
164 	if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
165 		return (EINVAL);
166 
167 	chn_vol_0db_pcm = val;
168 	chn_vpc_proc(0, val);
169 
170 	return (0);
171 }
172 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_0db,
173     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
174     sysctl_hw_snd_vpc_0db, "I",
175     "0db relative level");
176 
177 static int
178 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS)
179 {
180 	int err, val;
181 
182 	val = 0;
183 	err = sysctl_handle_int(oidp, &val, 0, req);
184 	if (err != 0 || req->newptr == NULL || val == 0)
185 		return (err);
186 
187 	chn_vol_0db_pcm = SND_VOL_0DB_PCM;
188 	chn_vpc_proc(1, SND_VOL_0DB_PCM);
189 
190 	return (0);
191 }
192 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_reset,
193     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int),
194     sysctl_hw_snd_vpc_reset, "I",
195     "reset volume on all channels");
196 
197 static int chn_usefrags = 0;
198 static int chn_syncdelay = -1;
199 
200 SYSCTL_INT(_hw_snd, OID_AUTO, usefrags, CTLFLAG_RWTUN,
201 	&chn_usefrags, 0, "prefer setfragments() over setblocksize()");
202 SYSCTL_INT(_hw_snd, OID_AUTO, syncdelay, CTLFLAG_RWTUN,
203 	&chn_syncdelay, 0,
204 	"append (0-1000) millisecond trailing buffer delay on each sync");
205 
206 /**
207  * @brief Channel sync group lock
208  *
209  * Clients should acquire this lock @b without holding any channel locks
210  * before touching syncgroups or the main syncgroup list.
211  */
212 struct mtx snd_pcm_syncgroups_mtx;
213 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
214 /**
215  * @brief syncgroups' master list
216  *
217  * Each time a channel syncgroup is created, it's added to this list.  This
218  * list should only be accessed with @sa snd_pcm_syncgroups_mtx held.
219  *
220  * See SNDCTL_DSP_SYNCGROUP for more information.
221  */
222 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
223 
224 static void
225 chn_lockinit(struct pcm_channel *c, int dir)
226 {
227 	switch (dir) {
228 	case PCMDIR_PLAY:
229 		c->lock = snd_mtxcreate(c->name, "pcm play channel");
230 		cv_init(&c->intr_cv, "pcmwr");
231 		break;
232 	case PCMDIR_PLAY_VIRTUAL:
233 		c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
234 		cv_init(&c->intr_cv, "pcmwrv");
235 		break;
236 	case PCMDIR_REC:
237 		c->lock = snd_mtxcreate(c->name, "pcm record channel");
238 		cv_init(&c->intr_cv, "pcmrd");
239 		break;
240 	case PCMDIR_REC_VIRTUAL:
241 		c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
242 		cv_init(&c->intr_cv, "pcmrdv");
243 		break;
244 	default:
245 		panic("%s(): Invalid direction=%d", __func__, dir);
246 		break;
247 	}
248 
249 	cv_init(&c->cv, "pcmchn");
250 }
251 
252 static void
253 chn_lockdestroy(struct pcm_channel *c)
254 {
255 	CHN_LOCKASSERT(c);
256 
257 	CHN_BROADCAST(&c->cv);
258 	CHN_BROADCAST(&c->intr_cv);
259 
260 	cv_destroy(&c->cv);
261 	cv_destroy(&c->intr_cv);
262 
263 	snd_mtxfree(c->lock);
264 }
265 
266 /**
267  * @brief Determine channel is ready for I/O
268  *
269  * @retval 1 = ready for I/O
270  * @retval 0 = not ready for I/O
271  */
272 static int
273 chn_polltrigger(struct pcm_channel *c)
274 {
275 	struct snd_dbuf *bs = c->bufsoft;
276 	u_int delta;
277 
278 	CHN_LOCKASSERT(c);
279 
280 	if (c->flags & CHN_F_MMAP) {
281 		if (sndbuf_getprevtotal(bs) < c->lw)
282 			delta = c->lw;
283 		else
284 			delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
285 	} else {
286 		if (c->direction == PCMDIR_PLAY)
287 			delta = sndbuf_getfree(bs);
288 		else
289 			delta = sndbuf_getready(bs);
290 	}
291 
292 	return ((delta < c->lw) ? 0 : 1);
293 }
294 
295 static void
296 chn_pollreset(struct pcm_channel *c)
297 {
298 
299 	CHN_LOCKASSERT(c);
300 	sndbuf_updateprevtotal(c->bufsoft);
301 }
302 
303 static void
304 chn_wakeup(struct pcm_channel *c)
305 {
306 	struct snd_dbuf *bs;
307 	struct pcm_channel *ch;
308 
309 	CHN_LOCKASSERT(c);
310 
311 	bs = c->bufsoft;
312 
313 	if (CHN_EMPTY(c, children.busy)) {
314 		if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))
315 			selwakeuppri(sndbuf_getsel(bs), PRIBIO);
316 		if (c->flags & CHN_F_SLEEPING) {
317 			/*
318 			 * Ok, I can just panic it right here since it is
319 			 * quite obvious that we never allow multiple waiters
320 			 * from userland. I'm too generous...
321 			 */
322 			CHN_BROADCAST(&c->intr_cv);
323 		}
324 	} else {
325 		CHN_FOREACH(ch, c, children.busy) {
326 			CHN_LOCK(ch);
327 			chn_wakeup(ch);
328 			CHN_UNLOCK(ch);
329 		}
330 	}
331 }
332 
333 static int
334 chn_sleep(struct pcm_channel *c, int timeout)
335 {
336 	int ret;
337 
338 	CHN_LOCKASSERT(c);
339 
340 	if (c->flags & CHN_F_DEAD)
341 		return (EINVAL);
342 
343 	c->flags |= CHN_F_SLEEPING;
344 	ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
345 	c->flags &= ~CHN_F_SLEEPING;
346 
347 	return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
348 }
349 
350 /*
351  * chn_dmaupdate() tracks the status of a dma transfer,
352  * updating pointers.
353  */
354 
355 static unsigned int
356 chn_dmaupdate(struct pcm_channel *c)
357 {
358 	struct snd_dbuf *b = c->bufhard;
359 	unsigned int delta, old, hwptr, amt;
360 
361 	KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
362 	CHN_LOCKASSERT(c);
363 
364 	old = sndbuf_gethwptr(b);
365 	hwptr = chn_getptr(c);
366 	delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
367 	sndbuf_sethwptr(b, hwptr);
368 
369 	if (c->direction == PCMDIR_PLAY) {
370 		amt = min(delta, sndbuf_getready(b));
371 		amt -= amt % sndbuf_getalign(b);
372 		if (amt > 0)
373 			sndbuf_dispose(b, NULL, amt);
374 	} else {
375 		amt = min(delta, sndbuf_getfree(b));
376 		amt -= amt % sndbuf_getalign(b);
377 		if (amt > 0)
378 		       sndbuf_acquire(b, NULL, amt);
379 	}
380 	if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) {
381 		device_printf(c->dev, "WARNING: %s DMA completion "
382 			"too fast/slow ! hwptr=%u, old=%u "
383 			"delta=%u amt=%u ready=%u free=%u\n",
384 			CHN_DIRSTR(c), hwptr, old, delta, amt,
385 			sndbuf_getready(b), sndbuf_getfree(b));
386 	}
387 
388 	return delta;
389 }
390 
391 static void
392 chn_wrfeed(struct pcm_channel *c)
393 {
394     	struct snd_dbuf *b = c->bufhard;
395     	struct snd_dbuf *bs = c->bufsoft;
396 	unsigned int amt, want, wasfree;
397 
398 	CHN_LOCKASSERT(c);
399 
400 	if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
401 		sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
402 
403 	wasfree = sndbuf_getfree(b);
404 	want = min(sndbuf_getsize(b),
405 	    imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) -
406 	     sndbuf_getready(b)));
407 	amt = min(wasfree, want);
408 	if (amt > 0)
409 		sndbuf_feed(bs, b, c, c->feeder, amt);
410 
411 	/*
412 	 * Possible xruns. There should be no empty space left in buffer.
413 	 */
414 	if (sndbuf_getready(b) < want)
415 		c->xruns++;
416 
417 	if (sndbuf_getfree(b) < wasfree)
418 		chn_wakeup(c);
419 }
420 
421 #if 0
422 static void
423 chn_wrupdate(struct pcm_channel *c)
424 {
425 
426 	CHN_LOCKASSERT(c);
427 	KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
428 
429 	if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
430 		return;
431 	chn_dmaupdate(c);
432 	chn_wrfeed(c);
433 	/* tell the driver we've updated the primary buffer */
434 	chn_trigger(c, PCMTRIG_EMLDMAWR);
435 }
436 #endif
437 
438 static void
439 chn_wrintr(struct pcm_channel *c)
440 {
441 
442 	CHN_LOCKASSERT(c);
443 	/* update pointers in primary buffer */
444 	chn_dmaupdate(c);
445 	/* ...and feed from secondary to primary */
446 	chn_wrfeed(c);
447 	/* tell the driver we've updated the primary buffer */
448 	chn_trigger(c, PCMTRIG_EMLDMAWR);
449 }
450 
451 /*
452  * user write routine - uiomove data into secondary buffer, trigger if necessary
453  * if blocking, sleep, rinse and repeat.
454  *
455  * called externally, so must handle locking
456  */
457 
458 int
459 chn_write(struct pcm_channel *c, struct uio *buf)
460 {
461 	struct snd_dbuf *bs = c->bufsoft;
462 	void *off;
463 	int ret, timeout, sz, t, p;
464 
465 	CHN_LOCKASSERT(c);
466 
467 	ret = 0;
468 	timeout = chn_timeout * hz;
469 
470 	while (ret == 0 && buf->uio_resid > 0) {
471 		sz = min(buf->uio_resid, sndbuf_getfree(bs));
472 		if (sz > 0) {
473 			/*
474 			 * The following assumes that the free space in
475 			 * the buffer can never be less around the
476 			 * unlock-uiomove-lock sequence.
477 			 */
478 			while (ret == 0 && sz > 0) {
479 				p = sndbuf_getfreeptr(bs);
480 				t = min(sz, sndbuf_getsize(bs) - p);
481 				off = sndbuf_getbufofs(bs, p);
482 				CHN_UNLOCK(c);
483 				ret = uiomove(off, t, buf);
484 				CHN_LOCK(c);
485 				sz -= t;
486 				sndbuf_acquire(bs, NULL, t);
487 			}
488 			ret = 0;
489 			if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
490 				ret = chn_start(c, 0);
491 				if (ret != 0)
492 					c->flags |= CHN_F_DEAD;
493 			}
494 		} else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
495 			/**
496 			 * @todo Evaluate whether EAGAIN is truly desirable.
497 			 * 	 4Front drivers behave like this, but I'm
498 			 * 	 not sure if it at all violates the "write
499 			 * 	 should be allowed to block" model.
500 			 *
501 			 * 	 The idea is that, while set with CHN_F_NOTRIGGER,
502 			 * 	 a channel isn't playing, *but* without this we
503 			 * 	 end up with "interrupt timeout / channel dead".
504 			 */
505 			ret = EAGAIN;
506 		} else {
507    			ret = chn_sleep(c, timeout);
508 			if (ret == EAGAIN) {
509 				ret = EINVAL;
510 				c->flags |= CHN_F_DEAD;
511 				device_printf(c->dev, "%s(): %s: "
512 				    "play interrupt timeout, channel dead\n",
513 				    __func__, c->name);
514 			} else if (ret == ERESTART || ret == EINTR)
515 				c->flags |= CHN_F_ABORTING;
516 		}
517 	}
518 
519 	return (ret);
520 }
521 
522 /*
523  * Feed new data from the read buffer. Can be called in the bottom half.
524  */
525 static void
526 chn_rdfeed(struct pcm_channel *c)
527 {
528     	struct snd_dbuf *b = c->bufhard;
529     	struct snd_dbuf *bs = c->bufsoft;
530 	unsigned int amt;
531 
532 	CHN_LOCKASSERT(c);
533 
534 	if (c->flags & CHN_F_MMAP)
535 		sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
536 
537 	amt = sndbuf_getfree(bs);
538 	if (amt > 0)
539 		sndbuf_feed(b, bs, c, c->feeder, amt);
540 
541 	amt = sndbuf_getready(b);
542 	if (amt > 0) {
543 		c->xruns++;
544 		sndbuf_dispose(b, NULL, amt);
545 	}
546 
547 	if (sndbuf_getready(bs) > 0)
548 		chn_wakeup(c);
549 }
550 
551 #if 0
552 static void
553 chn_rdupdate(struct pcm_channel *c)
554 {
555 
556 	CHN_LOCKASSERT(c);
557 	KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
558 
559 	if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
560 		return;
561 	chn_trigger(c, PCMTRIG_EMLDMARD);
562 	chn_dmaupdate(c);
563 	chn_rdfeed(c);
564 }
565 #endif
566 
567 /* read interrupt routine. Must be called with interrupts blocked. */
568 static void
569 chn_rdintr(struct pcm_channel *c)
570 {
571 
572 	CHN_LOCKASSERT(c);
573 	/* tell the driver to update the primary buffer if non-dma */
574 	chn_trigger(c, PCMTRIG_EMLDMARD);
575 	/* update pointers in primary buffer */
576 	chn_dmaupdate(c);
577 	/* ...and feed from primary to secondary */
578 	chn_rdfeed(c);
579 }
580 
581 /*
582  * user read routine - trigger if necessary, uiomove data from secondary buffer
583  * if blocking, sleep, rinse and repeat.
584  *
585  * called externally, so must handle locking
586  */
587 
588 int
589 chn_read(struct pcm_channel *c, struct uio *buf)
590 {
591 	struct snd_dbuf *bs = c->bufsoft;
592 	void *off;
593 	int ret, timeout, sz, t, p;
594 
595 	CHN_LOCKASSERT(c);
596 
597 	if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
598 		ret = chn_start(c, 0);
599 		if (ret != 0) {
600 			c->flags |= CHN_F_DEAD;
601 			return (ret);
602 		}
603 	}
604 
605 	ret = 0;
606 	timeout = chn_timeout * hz;
607 
608 	while (ret == 0 && buf->uio_resid > 0) {
609 		sz = min(buf->uio_resid, sndbuf_getready(bs));
610 		if (sz > 0) {
611 			/*
612 			 * The following assumes that the free space in
613 			 * the buffer can never be less around the
614 			 * unlock-uiomove-lock sequence.
615 			 */
616 			while (ret == 0 && sz > 0) {
617 				p = sndbuf_getreadyptr(bs);
618 				t = min(sz, sndbuf_getsize(bs) - p);
619 				off = sndbuf_getbufofs(bs, p);
620 				CHN_UNLOCK(c);
621 				ret = uiomove(off, t, buf);
622 				CHN_LOCK(c);
623 				sz -= t;
624 				sndbuf_dispose(bs, NULL, t);
625 			}
626 			ret = 0;
627 		} else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
628 			ret = EAGAIN;
629 		else {
630    			ret = chn_sleep(c, timeout);
631 			if (ret == EAGAIN) {
632 				ret = EINVAL;
633 				c->flags |= CHN_F_DEAD;
634 				device_printf(c->dev, "%s(): %s: "
635 				    "record interrupt timeout, channel dead\n",
636 				    __func__, c->name);
637 			} else if (ret == ERESTART || ret == EINTR)
638 				c->flags |= CHN_F_ABORTING;
639 		}
640 	}
641 
642 	return (ret);
643 }
644 
645 void
646 chn_intr_locked(struct pcm_channel *c)
647 {
648 
649 	CHN_LOCKASSERT(c);
650 
651 	c->interrupts++;
652 
653 	if (c->direction == PCMDIR_PLAY)
654 		chn_wrintr(c);
655 	else
656 		chn_rdintr(c);
657 }
658 
659 void
660 chn_intr(struct pcm_channel *c)
661 {
662 
663 	if (CHN_LOCKOWNED(c)) {
664 		chn_intr_locked(c);
665 		return;
666 	}
667 
668 	CHN_LOCK(c);
669 	chn_intr_locked(c);
670 	CHN_UNLOCK(c);
671 }
672 
673 u_int32_t
674 chn_start(struct pcm_channel *c, int force)
675 {
676 	u_int32_t i, j;
677 	struct snd_dbuf *b = c->bufhard;
678 	struct snd_dbuf *bs = c->bufsoft;
679 	int err;
680 
681 	CHN_LOCKASSERT(c);
682 	/* if we're running, or if we're prevented from triggering, bail */
683 	if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
684 		return (EINVAL);
685 
686 	err = 0;
687 
688 	if (force) {
689 		i = 1;
690 		j = 0;
691 	} else {
692 		if (c->direction == PCMDIR_REC) {
693 			i = sndbuf_getfree(bs);
694 			j = (i > 0) ? 1 : sndbuf_getready(b);
695 		} else {
696 			if (sndbuf_getfree(bs) == 0) {
697 				i = 1;
698 				j = 0;
699 			} else {
700 				struct snd_dbuf *pb;
701 
702 				pb = CHN_BUF_PARENT(c, b);
703 				i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
704 				j = sndbuf_getalign(pb);
705 			}
706 		}
707 		if (snd_verbose > 3 && CHN_EMPTY(c, children))
708 			device_printf(c->dev, "%s(): %s (%s) threshold "
709 			    "i=%d j=%d\n", __func__, CHN_DIRSTR(c),
710 			    (c->flags & CHN_F_VIRTUAL) ? "virtual" :
711 			    "hardware", i, j);
712 	}
713 
714 	if (i >= j) {
715 		c->flags |= CHN_F_TRIGGERED;
716 		sndbuf_setrun(b, 1);
717 		if (c->flags & CHN_F_CLOSING)
718 			c->feedcount = 2;
719 		else {
720 			c->feedcount = 0;
721 			c->interrupts = 0;
722 			c->xruns = 0;
723 		}
724 		if (c->parentchannel == NULL) {
725 			if (c->direction == PCMDIR_PLAY)
726 				sndbuf_fillsilence_rl(b,
727 				    sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
728 			if (snd_verbose > 3)
729 				device_printf(c->dev,
730 				    "%s(): %s starting! (%s/%s) "
731 				    "(ready=%d force=%d i=%d j=%d "
732 				    "intrtimeout=%u latency=%dms)\n",
733 				    __func__,
734 				    (c->flags & CHN_F_HAS_VCHAN) ?
735 				    "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
736 				    (c->flags & CHN_F_CLOSING) ? "closing" :
737 				    "running",
738 				    sndbuf_getready(b),
739 				    force, i, j, c->timeout,
740 				    (sndbuf_getsize(b) * 1000) /
741 				    (sndbuf_getalign(b) * sndbuf_getspd(b)));
742 		}
743 		err = chn_trigger(c, PCMTRIG_START);
744 	}
745 
746 	return (err);
747 }
748 
749 void
750 chn_resetbuf(struct pcm_channel *c)
751 {
752 	struct snd_dbuf *b = c->bufhard;
753 	struct snd_dbuf *bs = c->bufsoft;
754 
755 	c->blocks = 0;
756 	sndbuf_reset(b);
757 	sndbuf_reset(bs);
758 }
759 
760 /*
761  * chn_sync waits until the space in the given channel goes above
762  * a threshold. The threshold is checked against fl or rl respectively.
763  * Assume that the condition can become true, do not check here...
764  */
765 int
766 chn_sync(struct pcm_channel *c, int threshold)
767 {
768     	struct snd_dbuf *b, *bs;
769 	int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
770 	u_int32_t cflag;
771 
772 	CHN_LOCKASSERT(c);
773 
774 	if (c->direction != PCMDIR_PLAY)
775 		return (EINVAL);
776 
777 	bs = c->bufsoft;
778 
779 	if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
780 	    (threshold < 1 && sndbuf_getready(bs) < 1))
781 		return (0);
782 
783 	/* if we haven't yet started and nothing is buffered, else start*/
784 	if (CHN_STOPPED(c)) {
785 		if (threshold > 0 || sndbuf_getready(bs) > 0) {
786 			ret = chn_start(c, 1);
787 			if (ret != 0)
788 				return (ret);
789 		} else
790 			return (0);
791 	}
792 
793 	b = CHN_BUF_PARENT(c, c->bufhard);
794 
795 	minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
796 
797 	syncdelay = chn_syncdelay;
798 
799 	if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
800 		minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
801 
802 	/*
803 	 * Append (0-1000) millisecond trailing buffer (if needed)
804 	 * for slower / high latency hardwares (notably USB audio)
805 	 * to avoid audible truncation.
806 	 */
807 	if (syncdelay > 0)
808 		minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
809 		    ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
810 
811 	minflush -= minflush % sndbuf_getalign(bs);
812 
813 	if (minflush > 0) {
814 		threshold = min(minflush, sndbuf_getfree(bs));
815 		sndbuf_clear(bs, threshold);
816 		sndbuf_acquire(bs, NULL, threshold);
817 		minflush -= threshold;
818 	}
819 
820 	resid = sndbuf_getready(bs);
821 	residp = resid;
822 	blksz = sndbuf_getblksz(b);
823 	if (blksz < 1) {
824 		device_printf(c->dev,
825 		    "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n",
826 		    __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b),
827 		    sndbuf_getblksz(b), sndbuf_getblkcnt(b));
828 		if (sndbuf_getblkcnt(b) > 0)
829 			blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b);
830 		if (blksz < 1)
831 			blksz = 1;
832 	}
833 	count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
834 	hcount = count;
835 	ret = 0;
836 
837 	if (snd_verbose > 3)
838 		device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
839 		    "minflush=%d resid=%d\n", __func__, c->timeout, count,
840 		    minflush, resid);
841 
842 	cflag = c->flags & CHN_F_CLOSING;
843 	c->flags |= CHN_F_CLOSING;
844 	while (count > 0 && (resid > 0 || minflush > 0)) {
845 		ret = chn_sleep(c, c->timeout);
846     		if (ret == ERESTART || ret == EINTR) {
847 			c->flags |= CHN_F_ABORTING;
848 			break;
849 		} else if (ret == 0 || ret == EAGAIN) {
850 			resid = sndbuf_getready(bs);
851 			if (resid == residp) {
852 				--count;
853 				if (snd_verbose > 3)
854 					device_printf(c->dev,
855 					    "%s(): [stalled] timeout=%d "
856 					    "count=%d hcount=%d "
857 					    "resid=%d minflush=%d\n",
858 					    __func__, c->timeout, count,
859 					    hcount, resid, minflush);
860 			} else if (resid < residp && count < hcount) {
861 				++count;
862 				if (snd_verbose > 3)
863 					device_printf(c->dev,
864 					    "%s((): [resume] timeout=%d "
865 					    "count=%d hcount=%d "
866 					    "resid=%d minflush=%d\n",
867 					    __func__, c->timeout, count,
868 					    hcount, resid, minflush);
869 			}
870 			if (minflush > 0 && sndbuf_getfree(bs) > 0) {
871 				threshold = min(minflush,
872 				    sndbuf_getfree(bs));
873 				sndbuf_clear(bs, threshold);
874 				sndbuf_acquire(bs, NULL, threshold);
875 				resid = sndbuf_getready(bs);
876 				minflush -= threshold;
877 			}
878 			residp = resid;
879 		} else
880 			break;
881 	}
882 	c->flags &= ~CHN_F_CLOSING;
883 	c->flags |= cflag;
884 
885 	if (snd_verbose > 3)
886 		device_printf(c->dev,
887 		    "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d "
888 		    "minflush=%d ret=%d\n",
889 		    __func__, c->timeout, count, hcount, resid, residp,
890 		    minflush, ret);
891 
892     	return (0);
893 }
894 
895 /* called externally, handle locking */
896 int
897 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
898 {
899 	struct snd_dbuf *bs = c->bufsoft;
900 	int ret;
901 
902 	CHN_LOCKASSERT(c);
903 
904     	if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
905 		ret = chn_start(c, 1);
906 		if (ret != 0)
907 			return (0);
908 	}
909 
910 	ret = 0;
911 	if (chn_polltrigger(c)) {
912 		chn_pollreset(c);
913 		ret = ev;
914 	} else
915 		selrecord(td, sndbuf_getsel(bs));
916 
917 	return (ret);
918 }
919 
920 /*
921  * chn_abort terminates a running dma transfer.  it may sleep up to 200ms.
922  * it returns the number of bytes that have not been transferred.
923  *
924  * called from: dsp_close, dsp_ioctl, with channel locked
925  */
926 int
927 chn_abort(struct pcm_channel *c)
928 {
929     	int missing = 0;
930     	struct snd_dbuf *b = c->bufhard;
931     	struct snd_dbuf *bs = c->bufsoft;
932 
933 	CHN_LOCKASSERT(c);
934 	if (CHN_STOPPED(c))
935 		return 0;
936 	c->flags |= CHN_F_ABORTING;
937 
938 	c->flags &= ~CHN_F_TRIGGERED;
939 	/* kill the channel */
940 	chn_trigger(c, PCMTRIG_ABORT);
941 	sndbuf_setrun(b, 0);
942 	if (!(c->flags & CHN_F_VIRTUAL))
943 		chn_dmaupdate(c);
944     	missing = sndbuf_getready(bs);
945 
946 	c->flags &= ~CHN_F_ABORTING;
947 	return missing;
948 }
949 
950 /*
951  * this routine tries to flush the dma transfer. It is called
952  * on a close of a playback channel.
953  * first, if there is data in the buffer, but the dma has not yet
954  * begun, we need to start it.
955  * next, we wait for the play buffer to drain
956  * finally, we stop the dma.
957  *
958  * called from: dsp_close, not valid for record channels.
959  */
960 
961 int
962 chn_flush(struct pcm_channel *c)
963 {
964     	struct snd_dbuf *b = c->bufhard;
965 
966 	CHN_LOCKASSERT(c);
967 	KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
968     	DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
969 
970 	c->flags |= CHN_F_CLOSING;
971 	chn_sync(c, 0);
972 	c->flags &= ~CHN_F_TRIGGERED;
973 	/* kill the channel */
974 	chn_trigger(c, PCMTRIG_ABORT);
975 	sndbuf_setrun(b, 0);
976 
977     	c->flags &= ~CHN_F_CLOSING;
978     	return 0;
979 }
980 
981 int
982 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
983 {
984 	int i;
985 
986 	for (i = 0; fmtlist[i] != 0; i++) {
987 		if (fmt == fmtlist[i] ||
988 		    ((fmt & AFMT_PASSTHROUGH) &&
989 		    (AFMT_ENCODING(fmt) & fmtlist[i])))
990 			return (1);
991 	}
992 
993 	return (0);
994 }
995 
996 static const struct {
997 	char *name, *alias1, *alias2;
998 	uint32_t afmt;
999 } afmt_tab[] = {
1000 	{  "alaw",  NULL, NULL, AFMT_A_LAW  },
1001 	{ "mulaw",  NULL, NULL, AFMT_MU_LAW },
1002 	{    "u8",   "8", NULL, AFMT_U8     },
1003 	{    "s8",  NULL, NULL, AFMT_S8     },
1004 #if BYTE_ORDER == LITTLE_ENDIAN
1005 	{ "s16le", "s16", "16", AFMT_S16_LE },
1006 	{ "s16be",  NULL, NULL, AFMT_S16_BE },
1007 #else
1008 	{ "s16le",  NULL, NULL, AFMT_S16_LE },
1009 	{ "s16be", "s16", "16", AFMT_S16_BE },
1010 #endif
1011 	{ "u16le",  NULL, NULL, AFMT_U16_LE },
1012 	{ "u16be",  NULL, NULL, AFMT_U16_BE },
1013 	{ "s24le",  NULL, NULL, AFMT_S24_LE },
1014 	{ "s24be",  NULL, NULL, AFMT_S24_BE },
1015 	{ "u24le",  NULL, NULL, AFMT_U24_LE },
1016 	{ "u24be",  NULL, NULL, AFMT_U24_BE },
1017 #if BYTE_ORDER == LITTLE_ENDIAN
1018 	{ "s32le", "s32", "32", AFMT_S32_LE },
1019 	{ "s32be",  NULL, NULL, AFMT_S32_BE },
1020 #else
1021 	{ "s32le",  NULL, NULL, AFMT_S32_LE },
1022 	{ "s32be", "s32", "32", AFMT_S32_BE },
1023 #endif
1024 	{ "u32le",  NULL, NULL, AFMT_U32_LE },
1025 	{ "u32be",  NULL, NULL, AFMT_U32_BE },
1026 	{   "ac3",  NULL, NULL, AFMT_AC3    },
1027 	{    NULL,  NULL, NULL, 0           }
1028 };
1029 
1030 uint32_t
1031 snd_str2afmt(const char *req)
1032 {
1033 	int ext;
1034 	int ch;
1035 	int i;
1036 	char b1[8];
1037 	char b2[8];
1038 
1039 	memset(b1, 0, sizeof(b1));
1040 	memset(b2, 0, sizeof(b2));
1041 
1042 	i = sscanf(req, "%5[^:]:%6s", b1, b2);
1043 
1044 	if (i == 1) {
1045 		if (strlen(req) != strlen(b1))
1046 			return (0);
1047 		strlcpy(b2, "2.0", sizeof(b2));
1048 	} else if (i == 2) {
1049 		if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
1050 			return (0);
1051 	} else
1052 		return (0);
1053 
1054 	i = sscanf(b2, "%d.%d", &ch, &ext);
1055 
1056 	if (i == 0) {
1057 		if (strcasecmp(b2, "mono") == 0) {
1058 			ch = 1;
1059 			ext = 0;
1060 		} else if (strcasecmp(b2, "stereo") == 0) {
1061 			ch = 2;
1062 			ext = 0;
1063 		} else if (strcasecmp(b2, "quad") == 0) {
1064 			ch = 4;
1065 			ext = 0;
1066 		} else
1067 			return (0);
1068 	} else if (i == 1) {
1069 		if (ch < 1 || ch > AFMT_CHANNEL_MAX)
1070 			return (0);
1071 		ext = 0;
1072 	} else if (i == 2) {
1073 		if (ext < 0 || ext > AFMT_EXTCHANNEL_MAX)
1074 			return (0);
1075 		if (ch < 1 || (ch + ext) > AFMT_CHANNEL_MAX)
1076 			return (0);
1077 	} else
1078 		return (0);
1079 
1080 	for (i = 0; afmt_tab[i].name != NULL; i++) {
1081 		if (strcasecmp(afmt_tab[i].name, b1) != 0) {
1082 			if (afmt_tab[i].alias1 == NULL)
1083 				continue;
1084 			if (strcasecmp(afmt_tab[i].alias1, b1) != 0) {
1085 				if (afmt_tab[i].alias2 == NULL)
1086 					continue;
1087 				if (strcasecmp(afmt_tab[i].alias2, b1) != 0)
1088 					continue;
1089 			}
1090 		}
1091 		/* found a match */
1092 		return (SND_FORMAT(afmt_tab[i].afmt, ch + ext, ext));
1093 	}
1094 	/* not a valid format */
1095 	return (0);
1096 }
1097 
1098 uint32_t
1099 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
1100 {
1101 	uint32_t enc;
1102 	uint32_t ext;
1103 	uint32_t ch;
1104 	int i;
1105 
1106 	if (buf == NULL || len < AFMTSTR_LEN)
1107 		return (0);
1108 
1109 	memset(buf, 0, len);
1110 
1111 	enc = AFMT_ENCODING(afmt);
1112 	ch = AFMT_CHANNEL(afmt);
1113 	ext = AFMT_EXTCHANNEL(afmt);
1114 	/* check there is at least one channel */
1115 	if (ch <= ext)
1116 		return (0);
1117 	for (i = 0; afmt_tab[i].name != NULL; i++) {
1118 		if (enc != afmt_tab[i].afmt)
1119 			continue;
1120 		/* found a match */
1121 		snprintf(buf, len, "%s:%d.%d",
1122 		    afmt_tab[i].name, ch - ext, ext);
1123 		return (SND_FORMAT(enc, ch, ext));
1124 	}
1125 	return (0);
1126 }
1127 
1128 int
1129 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
1130 {
1131 	int r;
1132 
1133 	CHN_LOCKASSERT(c);
1134 	c->feedcount = 0;
1135 	c->flags &= CHN_F_RESET;
1136 	c->interrupts = 0;
1137 	c->timeout = 1;
1138 	c->xruns = 0;
1139 
1140 	c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
1141 	    CHN_F_BITPERFECT : 0;
1142 
1143 	r = CHANNEL_RESET(c->methods, c->devinfo);
1144 	if (r == 0 && fmt != 0 && spd != 0) {
1145 		r = chn_setparam(c, fmt, spd);
1146 		fmt = 0;
1147 		spd = 0;
1148 	}
1149 	if (r == 0 && fmt != 0)
1150 		r = chn_setformat(c, fmt);
1151 	if (r == 0 && spd != 0)
1152 		r = chn_setspeed(c, spd);
1153 	if (r == 0)
1154 		r = chn_setlatency(c, chn_latency);
1155 	if (r == 0) {
1156 		chn_resetbuf(c);
1157 		r = CHANNEL_RESETDONE(c->methods, c->devinfo);
1158 	}
1159 	return r;
1160 }
1161 
1162 int
1163 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
1164 {
1165 	struct feeder_class *fc;
1166 	struct snd_dbuf *b, *bs;
1167 	int i, ret;
1168 
1169 	if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
1170 		chn_timeout = CHN_TIMEOUT;
1171 
1172 	chn_lockinit(c, dir);
1173 
1174 	b = NULL;
1175 	bs = NULL;
1176 	CHN_INIT(c, children);
1177 	CHN_INIT(c, children.busy);
1178 	c->devinfo = NULL;
1179 	c->feeder = NULL;
1180 	c->latency = -1;
1181 	c->timeout = 1;
1182 
1183 	ret = ENOMEM;
1184 	b = sndbuf_create(c->dev, c->name, "primary", c);
1185 	if (b == NULL)
1186 		goto out;
1187 	bs = sndbuf_create(c->dev, c->name, "secondary", c);
1188 	if (bs == NULL)
1189 		goto out;
1190 
1191 	CHN_LOCK(c);
1192 
1193 	ret = EINVAL;
1194 	fc = feeder_getclass(NULL);
1195 	if (fc == NULL)
1196 		goto out;
1197 	if (chn_addfeeder(c, fc, NULL))
1198 		goto out;
1199 
1200 	/*
1201 	 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
1202 	 *	 with the channel unlocked because they are also called
1203 	 *	 from driver methods that don't know about locking
1204 	 */
1205 	CHN_UNLOCK(c);
1206 	sndbuf_setup(bs, NULL, 0);
1207 	CHN_LOCK(c);
1208 	c->bufhard = b;
1209 	c->bufsoft = bs;
1210 	c->flags = 0;
1211 	c->feederflags = 0;
1212 	c->sm = NULL;
1213 	c->format = SND_FORMAT(AFMT_U8, 1, 0);
1214 	c->speed = DSP_DEFAULT_SPEED;
1215 
1216 	c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1217 	c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1218 
1219 	for (i = 0; i < SND_CHN_T_MAX; i++) {
1220 		c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1221 	}
1222 
1223 	c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1224 	c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1225 
1226 	chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1227 
1228 	ret = ENODEV;
1229 	CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
1230 	c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1231 	CHN_LOCK(c);
1232 	if (c->devinfo == NULL)
1233 		goto out;
1234 
1235 	ret = ENOMEM;
1236 	if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
1237 		goto out;
1238 
1239 	ret = 0;
1240 	c->direction = direction;
1241 
1242 	sndbuf_setfmt(b, c->format);
1243 	sndbuf_setspd(b, c->speed);
1244 	sndbuf_setfmt(bs, c->format);
1245 	sndbuf_setspd(bs, c->speed);
1246 
1247 	/**
1248 	 * @todo Should this be moved somewhere else?  The primary buffer
1249 	 * 	 is allocated by the driver or via DMA map setup, and tmpbuf
1250 	 * 	 seems to only come into existence in sndbuf_resize().
1251 	 */
1252 	if (c->direction == PCMDIR_PLAY) {
1253 		bs->sl = sndbuf_getmaxsize(bs);
1254 		bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
1255 		if (bs->shadbuf == NULL) {
1256 			ret = ENOMEM;
1257 			goto out;
1258 		}
1259 	}
1260 
1261 out:
1262 	CHN_UNLOCK(c);
1263 	if (ret) {
1264 		if (c->devinfo) {
1265 			if (CHANNEL_FREE(c->methods, c->devinfo))
1266 				sndbuf_free(b);
1267 		}
1268 		if (bs)
1269 			sndbuf_destroy(bs);
1270 		if (b)
1271 			sndbuf_destroy(b);
1272 		CHN_LOCK(c);
1273 		c->flags |= CHN_F_DEAD;
1274 		chn_lockdestroy(c);
1275 
1276 		return ret;
1277 	}
1278 
1279 	return 0;
1280 }
1281 
1282 int
1283 chn_kill(struct pcm_channel *c)
1284 {
1285     	struct snd_dbuf *b = c->bufhard;
1286     	struct snd_dbuf *bs = c->bufsoft;
1287 
1288 	if (CHN_STARTED(c)) {
1289 		CHN_LOCK(c);
1290 		chn_trigger(c, PCMTRIG_ABORT);
1291 		CHN_UNLOCK(c);
1292 	}
1293 	while (chn_removefeeder(c) == 0)
1294 		;
1295 	if (CHANNEL_FREE(c->methods, c->devinfo))
1296 		sndbuf_free(b);
1297 	sndbuf_destroy(bs);
1298 	sndbuf_destroy(b);
1299 	CHN_LOCK(c);
1300 	c->flags |= CHN_F_DEAD;
1301 	chn_lockdestroy(c);
1302 
1303 	return (0);
1304 }
1305 
1306 /* XXX Obsolete. Use *_matrix() variant instead. */
1307 int
1308 chn_setvolume(struct pcm_channel *c, int left, int right)
1309 {
1310 	int ret;
1311 
1312 	ret = chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FL, left);
1313 	ret |= chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FR,
1314 	    right) << 8;
1315 
1316 	return (ret);
1317 }
1318 
1319 int
1320 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1321     int center)
1322 {
1323 	int i, ret;
1324 
1325 	ret = 0;
1326 
1327 	for (i = 0; i < SND_CHN_T_MAX; i++) {
1328 		if ((1 << i) & SND_CHN_LEFT_MASK)
1329 			ret |= chn_setvolume_matrix(c, vc, i, left);
1330 		else if ((1 << i) & SND_CHN_RIGHT_MASK)
1331 			ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1332 		else
1333 			ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1334 	}
1335 
1336 	return (ret);
1337 }
1338 
1339 int
1340 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1341 {
1342 	int i;
1343 
1344 	KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1345 	    (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1346 	    (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1347 	    vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1348 	    (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1349 	    ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1350 	    __func__, c, vc, vt, val));
1351 	CHN_LOCKASSERT(c);
1352 
1353 	if (val < 0)
1354 		val = 0;
1355 	if (val > 100)
1356 		val = 100;
1357 
1358 	c->volume[vc][vt] = val;
1359 
1360 	/*
1361 	 * Do relative calculation here and store it into class + 1
1362 	 * to ease the job of feeder_volume.
1363 	 */
1364 	if (vc == SND_VOL_C_MASTER) {
1365 		for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1366 		    vc += SND_VOL_C_STEP)
1367 			c->volume[SND_VOL_C_VAL(vc)][vt] =
1368 			    SND_VOL_CALC_VAL(c->volume, vc, vt);
1369 	} else if (vc & 1) {
1370 		if (vt == SND_CHN_T_VOL_0DB)
1371 			for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1372 			    i += SND_CHN_T_STEP) {
1373 				c->volume[SND_VOL_C_VAL(vc)][i] =
1374 				    SND_VOL_CALC_VAL(c->volume, vc, i);
1375 			}
1376 		else
1377 			c->volume[SND_VOL_C_VAL(vc)][vt] =
1378 			    SND_VOL_CALC_VAL(c->volume, vc, vt);
1379 	}
1380 
1381 	return (val);
1382 }
1383 
1384 int
1385 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1386 {
1387 	KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1388 	    (vt == SND_CHN_T_VOL_0DB ||
1389 	    (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1390 	    ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1391 	    __func__, c, vc, vt));
1392 	CHN_LOCKASSERT(c);
1393 
1394 	return (c->volume[vc][vt]);
1395 }
1396 
1397 struct pcmchan_matrix *
1398 chn_getmatrix(struct pcm_channel *c)
1399 {
1400 
1401 	KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1402 	CHN_LOCKASSERT(c);
1403 
1404 	if (!(c->format & AFMT_CONVERTIBLE))
1405 		return (NULL);
1406 
1407 	return (&c->matrix);
1408 }
1409 
1410 int
1411 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1412 {
1413 
1414 	KASSERT(c != NULL && m != NULL,
1415 	    ("%s(): NULL channel or matrix", __func__));
1416 	CHN_LOCKASSERT(c);
1417 
1418 	if (!(c->format & AFMT_CONVERTIBLE))
1419 		return (EINVAL);
1420 
1421 	c->matrix = *m;
1422 	c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1423 
1424 	return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1425 }
1426 
1427 /*
1428  * XXX chn_oss_* exists for the sake of compatibility.
1429  */
1430 int
1431 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1432 {
1433 
1434 	KASSERT(c != NULL && map != NULL,
1435 	    ("%s(): NULL channel or map", __func__));
1436 	CHN_LOCKASSERT(c);
1437 
1438 	if (!(c->format & AFMT_CONVERTIBLE))
1439 		return (EINVAL);
1440 
1441 	return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1442 }
1443 
1444 int
1445 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1446 {
1447 	struct pcmchan_matrix m;
1448 	int ret;
1449 
1450 	KASSERT(c != NULL && map != NULL,
1451 	    ("%s(): NULL channel or map", __func__));
1452 	CHN_LOCKASSERT(c);
1453 
1454 	if (!(c->format & AFMT_CONVERTIBLE))
1455 		return (EINVAL);
1456 
1457 	m = c->matrix;
1458 	ret = feeder_matrix_oss_set_channel_order(&m, map);
1459 	if (ret != 0)
1460 		return (ret);
1461 
1462 	return (chn_setmatrix(c, &m));
1463 }
1464 
1465 #define SND_CHN_OSS_FRONT	(SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1466 #define SND_CHN_OSS_SURR	(SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1467 #define SND_CHN_OSS_CENTER_LFE	(SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1468 #define SND_CHN_OSS_REAR	(SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1469 
1470 int
1471 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1472 {
1473 	struct pcmchan_matrix *m;
1474 	struct pcmchan_caps *caps;
1475 	uint32_t i, format;
1476 
1477 	KASSERT(c != NULL && retmask != NULL,
1478 	    ("%s(): NULL channel or retmask", __func__));
1479 	CHN_LOCKASSERT(c);
1480 
1481 	caps = chn_getcaps(c);
1482 	if (caps == NULL || caps->fmtlist == NULL)
1483 		return (ENODEV);
1484 
1485 	for (i = 0; caps->fmtlist[i] != 0; i++) {
1486 		format = caps->fmtlist[i];
1487 		if (!(format & AFMT_CONVERTIBLE)) {
1488 			*retmask |= DSP_BIND_SPDIF;
1489 			continue;
1490 		}
1491 		m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1492 		if (m == NULL)
1493 			continue;
1494 		if (m->mask & SND_CHN_OSS_FRONT)
1495 			*retmask |= DSP_BIND_FRONT;
1496 		if (m->mask & SND_CHN_OSS_SURR)
1497 			*retmask |= DSP_BIND_SURR;
1498 		if (m->mask & SND_CHN_OSS_CENTER_LFE)
1499 			*retmask |= DSP_BIND_CENTER_LFE;
1500 		if (m->mask & SND_CHN_OSS_REAR)
1501 			*retmask |= DSP_BIND_REAR;
1502 	}
1503 
1504 	/* report software-supported binding mask */
1505 	if (!CHN_BITPERFECT(c) && report_soft_matrix)
1506 		*retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1507 		    DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1508 
1509 	return (0);
1510 }
1511 
1512 void
1513 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1514 {
1515 	int i;
1516 
1517 	KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1518 	    ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1519 	CHN_LOCKASSERT(c);
1520 
1521 	if (force == 0 && chn_vpc_autoreset == 0)
1522 		return;
1523 
1524 	for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1525 		CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1526 }
1527 
1528 static u_int32_t
1529 round_pow2(u_int32_t v)
1530 {
1531 	u_int32_t ret;
1532 
1533 	if (v < 2)
1534 		v = 2;
1535 	ret = 0;
1536 	while (v >> ret)
1537 		ret++;
1538 	ret = 1 << (ret - 1);
1539 	while (ret < v)
1540 		ret <<= 1;
1541 	return ret;
1542 }
1543 
1544 static u_int32_t
1545 round_blksz(u_int32_t v, int round)
1546 {
1547 	u_int32_t ret, tmp;
1548 
1549 	if (round < 1)
1550 		round = 1;
1551 
1552 	ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1553 
1554 	if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1555 		ret >>= 1;
1556 
1557 	tmp = ret - (ret % round);
1558 	while (tmp < 16 || tmp < round) {
1559 		ret <<= 1;
1560 		tmp = ret - (ret % round);
1561 	}
1562 
1563 	return ret;
1564 }
1565 
1566 /*
1567  * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1568  * is to keep 2nd buffer short so that it doesn't cause long queue during
1569  * buffer transfer.
1570  *
1571  *    Latency reference table for 48khz stereo 16bit: (PLAY)
1572  *
1573  *      +---------+------------+-----------+------------+
1574  *      | Latency | Blockcount | Blocksize | Buffersize |
1575  *      +---------+------------+-----------+------------+
1576  *      |     0   |       2    |   64      |    128     |
1577  *      +---------+------------+-----------+------------+
1578  *      |     1   |       4    |   128     |    512     |
1579  *      +---------+------------+-----------+------------+
1580  *      |     2   |       8    |   512     |    4096    |
1581  *      +---------+------------+-----------+------------+
1582  *      |     3   |      16    |   512     |    8192    |
1583  *      +---------+------------+-----------+------------+
1584  *      |     4   |      32    |   512     |    16384   |
1585  *      +---------+------------+-----------+------------+
1586  *      |     5   |      32    |   1024    |    32768   |
1587  *      +---------+------------+-----------+------------+
1588  *      |     6   |      16    |   2048    |    32768   |
1589  *      +---------+------------+-----------+------------+
1590  *      |     7   |       8    |   4096    |    32768   |
1591  *      +---------+------------+-----------+------------+
1592  *      |     8   |       4    |   8192    |    32768   |
1593  *      +---------+------------+-----------+------------+
1594  *      |     9   |       2    |   16384   |    32768   |
1595  *      +---------+------------+-----------+------------+
1596  *      |    10   |       2    |   32768   |    65536   |
1597  *      +---------+------------+-----------+------------+
1598  *
1599  * Recording need a different reference table. All we care is
1600  * gobbling up everything within reasonable buffering threshold.
1601  *
1602  *    Latency reference table for 48khz stereo 16bit: (REC)
1603  *
1604  *      +---------+------------+-----------+------------+
1605  *      | Latency | Blockcount | Blocksize | Buffersize |
1606  *      +---------+------------+-----------+------------+
1607  *      |     0   |     512    |   32      |    16384   |
1608  *      +---------+------------+-----------+------------+
1609  *      |     1   |     256    |   64      |    16384   |
1610  *      +---------+------------+-----------+------------+
1611  *      |     2   |     128    |   128     |    16384   |
1612  *      +---------+------------+-----------+------------+
1613  *      |     3   |      64    |   256     |    16384   |
1614  *      +---------+------------+-----------+------------+
1615  *      |     4   |      32    |   512     |    16384   |
1616  *      +---------+------------+-----------+------------+
1617  *      |     5   |      32    |   1024    |    32768   |
1618  *      +---------+------------+-----------+------------+
1619  *      |     6   |      16    |   2048    |    32768   |
1620  *      +---------+------------+-----------+------------+
1621  *      |     7   |       8    |   4096    |    32768   |
1622  *      +---------+------------+-----------+------------+
1623  *      |     8   |       4    |   8192    |    32768   |
1624  *      +---------+------------+-----------+------------+
1625  *      |     9   |       2    |   16384   |    32768   |
1626  *      +---------+------------+-----------+------------+
1627  *      |    10   |       2    |   32768   |    65536   |
1628  *      +---------+------------+-----------+------------+
1629  *
1630  * Calculations for other data rate are entirely based on these reference
1631  * tables. For normal operation, Latency 5 seems give the best, well
1632  * balanced performance for typical workload. Anything below 5 will
1633  * eat up CPU to keep up with increasing context switches because of
1634  * shorter buffer space and usually require the application to handle it
1635  * aggresively through possibly real time programming technique.
1636  *
1637  */
1638 #define CHN_LATENCY_PBLKCNT_REF				\
1639 	{{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1},		\
1640 	{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
1641 #define CHN_LATENCY_PBUFSZ_REF				\
1642 	{{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16},	\
1643 	{11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
1644 
1645 #define CHN_LATENCY_RBLKCNT_REF				\
1646 	{{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1},		\
1647 	{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
1648 #define CHN_LATENCY_RBUFSZ_REF				\
1649 	{{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},	\
1650 	{15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
1651 
1652 #define CHN_LATENCY_DATA_REF	192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1653 
1654 static int
1655 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1656 				u_int32_t max, int *rblksz, int *rblkcnt)
1657 {
1658 	static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1659 	    CHN_LATENCY_PBLKCNT_REF;
1660 	static int  pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1661 	    CHN_LATENCY_PBUFSZ_REF;
1662 	static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1663 	    CHN_LATENCY_RBLKCNT_REF;
1664 	static int  rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1665 	    CHN_LATENCY_RBUFSZ_REF;
1666 	u_int32_t bufsz;
1667 	int lprofile, blksz, blkcnt;
1668 
1669 	if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1670 	    bps < 1 || datarate < 1 ||
1671 	    !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1672 		if (rblksz != NULL)
1673 			*rblksz = CHN_2NDBUFMAXSIZE >> 1;
1674 		if (rblkcnt != NULL)
1675 			*rblkcnt = 2;
1676 		printf("%s(): FAILED dir=%d latency=%d bps=%d "
1677 		    "datarate=%u max=%u\n",
1678 		    __func__, dir, latency, bps, datarate, max);
1679 		return CHN_2NDBUFMAXSIZE;
1680 	}
1681 
1682 	lprofile = chn_latency_profile;
1683 
1684 	if (dir == PCMDIR_PLAY) {
1685 		blkcnt = pblkcnts[lprofile][latency];
1686 		bufsz = pbufszs[lprofile][latency];
1687 	} else {
1688 		blkcnt = rblkcnts[lprofile][latency];
1689 		bufsz = rbufszs[lprofile][latency];
1690 	}
1691 
1692 	bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1693 	    datarate));
1694 	if (bufsz > max)
1695 		bufsz = max;
1696 	blksz = round_blksz(bufsz >> blkcnt, bps);
1697 
1698 	if (rblksz != NULL)
1699 		*rblksz = blksz;
1700 	if (rblkcnt != NULL)
1701 		*rblkcnt = 1 << blkcnt;
1702 
1703 	return blksz << blkcnt;
1704 }
1705 
1706 static int
1707 chn_resizebuf(struct pcm_channel *c, int latency,
1708 					int blkcnt, int blksz)
1709 {
1710 	struct snd_dbuf *b, *bs, *pb;
1711 	int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1712 	int ret;
1713 
1714 	CHN_LOCKASSERT(c);
1715 
1716 	if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1717 	    !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1718 		return EINVAL;
1719 
1720 	if (latency == -1) {
1721 		c->latency = -1;
1722 		latency = chn_latency;
1723 	} else if (latency == -2) {
1724 		latency = c->latency;
1725 		if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1726 			latency = chn_latency;
1727 	} else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1728 		return EINVAL;
1729 	else {
1730 		c->latency = latency;
1731 	}
1732 
1733 	bs = c->bufsoft;
1734 	b = c->bufhard;
1735 
1736 	if (!(blksz == 0 || blkcnt == -1) &&
1737 	    (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
1738 	    (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1739 		return EINVAL;
1740 
1741 	chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
1742 	    sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
1743 	    &sblksz, &sblkcnt);
1744 
1745 	if (blksz == 0 || blkcnt == -1) {
1746 		if (blkcnt == -1)
1747 			c->flags &= ~CHN_F_HAS_SIZE;
1748 		if (c->flags & CHN_F_HAS_SIZE) {
1749 			blksz = sndbuf_getblksz(bs);
1750 			blkcnt = sndbuf_getblkcnt(bs);
1751 		}
1752 	} else
1753 		c->flags |= CHN_F_HAS_SIZE;
1754 
1755 	if (c->flags & CHN_F_HAS_SIZE) {
1756 		/*
1757 		 * The application has requested their own blksz/blkcnt.
1758 		 * Just obey with it, and let them toast alone. We can
1759 		 * clamp it to the nearest latency profile, but that would
1760 		 * defeat the purpose of having custom control. The least
1761 		 * we can do is round it to the nearest ^2 and align it.
1762 		 */
1763 		sblksz = round_blksz(blksz, sndbuf_getalign(bs));
1764 		sblkcnt = round_pow2(blkcnt);
1765 	}
1766 
1767 	if (c->parentchannel != NULL) {
1768 		pb = c->parentchannel->bufsoft;
1769 		CHN_UNLOCK(c);
1770 		CHN_LOCK(c->parentchannel);
1771 		chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1772 		CHN_UNLOCK(c->parentchannel);
1773 		CHN_LOCK(c);
1774 		if (c->direction == PCMDIR_PLAY) {
1775 			limit = (pb != NULL) ?
1776 			    sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
1777 		} else {
1778 			limit = (pb != NULL) ?
1779 			    sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
1780 		}
1781 	} else {
1782 		hblkcnt = 2;
1783 		if (c->flags & CHN_F_HAS_SIZE) {
1784 			hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1785 			    sndbuf_getalign(b));
1786 			hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
1787 		} else
1788 			chn_calclatency(c->direction, latency,
1789 			    sndbuf_getalign(b),
1790 			    sndbuf_getalign(b) * sndbuf_getspd(b),
1791 			    CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1792 
1793 		if ((hblksz << 1) > sndbuf_getmaxsize(b))
1794 			hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
1795 			    sndbuf_getalign(b));
1796 
1797 		while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
1798 			if (hblkcnt < 4)
1799 				hblksz >>= 1;
1800 			else
1801 				hblkcnt >>= 1;
1802 		}
1803 
1804 		hblksz -= hblksz % sndbuf_getalign(b);
1805 
1806 #if 0
1807 		hblksz = sndbuf_getmaxsize(b) >> 1;
1808 		hblksz -= hblksz % sndbuf_getalign(b);
1809 		hblkcnt = 2;
1810 #endif
1811 
1812 		CHN_UNLOCK(c);
1813 		if (chn_usefrags == 0 ||
1814 		    CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1815 		    hblksz, hblkcnt) != 0)
1816 			sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
1817 			    c->devinfo, hblksz));
1818 		CHN_LOCK(c);
1819 
1820 		if (!CHN_EMPTY(c, children)) {
1821 			nsblksz = round_blksz(
1822 			    sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
1823 			    sndbuf_getalign(bs));
1824 			nsblkcnt = sndbuf_getblkcnt(b);
1825 			if (c->direction == PCMDIR_PLAY) {
1826 				do {
1827 					nsblkcnt--;
1828 				} while (nsblkcnt >= 2 &&
1829 				    nsblksz * nsblkcnt >= sblksz * sblkcnt);
1830 				nsblkcnt++;
1831 			}
1832 			sblksz = nsblksz;
1833 			sblkcnt = nsblkcnt;
1834 			limit = 0;
1835 		} else
1836 			limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
1837 	}
1838 
1839 	if (limit > CHN_2NDBUFMAXSIZE)
1840 		limit = CHN_2NDBUFMAXSIZE;
1841 
1842 #if 0
1843 	while (limit > 0 && (sblksz * sblkcnt) > limit) {
1844 		if (sblkcnt < 4)
1845 			break;
1846 		sblkcnt >>= 1;
1847 	}
1848 #endif
1849 
1850 	while ((sblksz * sblkcnt) < limit)
1851 		sblkcnt <<= 1;
1852 
1853 	while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1854 		if (sblkcnt < 4)
1855 			sblksz >>= 1;
1856 		else
1857 			sblkcnt >>= 1;
1858 	}
1859 
1860 	sblksz -= sblksz % sndbuf_getalign(bs);
1861 
1862 	if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
1863 	    sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
1864 		ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1865 		if (ret != 0) {
1866 			device_printf(c->dev, "%s(): Failed: %d %d\n",
1867 			    __func__, sblkcnt, sblksz);
1868 			return ret;
1869 		}
1870 	}
1871 
1872 	/*
1873 	 * Interrupt timeout
1874 	 */
1875 	c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
1876 	    ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
1877 	if (c->parentchannel != NULL)
1878 		c->timeout = min(c->timeout, c->parentchannel->timeout);
1879 	if (c->timeout < 1)
1880 		c->timeout = 1;
1881 
1882 	/*
1883 	 * OSSv4 docs: "By default OSS will set the low water level equal
1884 	 * to the fragment size which is optimal in most cases."
1885 	 */
1886 	c->lw = sndbuf_getblksz(bs);
1887 	chn_resetbuf(c);
1888 
1889 	if (snd_verbose > 3)
1890 		device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1891 		    "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1892 		    __func__, CHN_DIRSTR(c),
1893 		    (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1894 		    c->timeout,
1895 		    sndbuf_getsize(b), sndbuf_getblksz(b),
1896 		    sndbuf_getblkcnt(b),
1897 		    sndbuf_getsize(bs), sndbuf_getblksz(bs),
1898 		    sndbuf_getblkcnt(bs), limit);
1899 
1900 	return 0;
1901 }
1902 
1903 int
1904 chn_setlatency(struct pcm_channel *c, int latency)
1905 {
1906 	CHN_LOCKASSERT(c);
1907 	/* Destroy blksz/blkcnt, enforce latency profile. */
1908 	return chn_resizebuf(c, latency, -1, 0);
1909 }
1910 
1911 int
1912 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1913 {
1914 	CHN_LOCKASSERT(c);
1915 	/* Destroy latency profile, enforce blksz/blkcnt */
1916 	return chn_resizebuf(c, -1, blkcnt, blksz);
1917 }
1918 
1919 int
1920 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
1921 {
1922 	struct pcmchan_caps *caps;
1923 	uint32_t hwspeed, delta;
1924 	int ret;
1925 
1926 	CHN_LOCKASSERT(c);
1927 
1928 	if (speed < 1 || format == 0 || CHN_STARTED(c))
1929 		return (EINVAL);
1930 
1931 	c->format = format;
1932 	c->speed = speed;
1933 
1934 	caps = chn_getcaps(c);
1935 
1936 	hwspeed = speed;
1937 	RANGE(hwspeed, caps->minspeed, caps->maxspeed);
1938 
1939 	sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
1940 	    hwspeed));
1941 	hwspeed = sndbuf_getspd(c->bufhard);
1942 
1943 	delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
1944 
1945 	if (delta <= feeder_rate_round)
1946 		c->speed = hwspeed;
1947 
1948 	ret = feeder_chain(c);
1949 
1950 	if (ret == 0)
1951 		ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
1952 		    sndbuf_getfmt(c->bufhard));
1953 
1954 	if (ret == 0)
1955 		ret = chn_resizebuf(c, -2, 0, 0);
1956 
1957 	return (ret);
1958 }
1959 
1960 int
1961 chn_setspeed(struct pcm_channel *c, uint32_t speed)
1962 {
1963 	uint32_t oldformat, oldspeed, format;
1964 	int ret;
1965 
1966 #if 0
1967 	/* XXX force 48k */
1968 	if (c->format & AFMT_PASSTHROUGH)
1969 		speed = AFMT_PASSTHROUGH_RATE;
1970 #endif
1971 
1972 	oldformat = c->format;
1973 	oldspeed = c->speed;
1974 	format = oldformat;
1975 
1976 	ret = chn_setparam(c, format, speed);
1977 	if (ret != 0) {
1978 		if (snd_verbose > 3)
1979 			device_printf(c->dev,
1980 			    "%s(): Setting speed %d failed, "
1981 			    "falling back to %d\n",
1982 			    __func__, speed, oldspeed);
1983 		chn_setparam(c, c->format, oldspeed);
1984 	}
1985 
1986 	return (ret);
1987 }
1988 
1989 int
1990 chn_setformat(struct pcm_channel *c, uint32_t format)
1991 {
1992 	uint32_t oldformat, oldspeed, speed;
1993 	int ret;
1994 
1995 	/* XXX force stereo */
1996 	if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
1997 		format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
1998 		    AFMT_PASSTHROUGH_EXTCHANNEL);
1999 	}
2000 
2001 	oldformat = c->format;
2002 	oldspeed = c->speed;
2003 	speed = oldspeed;
2004 
2005 	ret = chn_setparam(c, format, speed);
2006 	if (ret != 0) {
2007 		if (snd_verbose > 3)
2008 			device_printf(c->dev,
2009 			    "%s(): Format change 0x%08x failed, "
2010 			    "falling back to 0x%08x\n",
2011 			    __func__, format, oldformat);
2012 		chn_setparam(c, oldformat, oldspeed);
2013 	}
2014 
2015 	return (ret);
2016 }
2017 
2018 void
2019 chn_syncstate(struct pcm_channel *c)
2020 {
2021 	struct snddev_info *d;
2022 	struct snd_mixer *m;
2023 
2024 	d = (c != NULL) ? c->parentsnddev : NULL;
2025 	m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2026 	    NULL;
2027 
2028 	if (d == NULL || m == NULL)
2029 		return;
2030 
2031 	CHN_LOCKASSERT(c);
2032 
2033 	if (c->feederflags & (1 << FEEDER_VOLUME)) {
2034 		uint32_t parent;
2035 		int vol, pvol, left, right, center;
2036 
2037 		if (c->direction == PCMDIR_PLAY &&
2038 		    (d->flags & SD_F_SOFTPCMVOL)) {
2039 			/* CHN_UNLOCK(c); */
2040 			vol = mix_get(m, SOUND_MIXER_PCM);
2041 			parent = mix_getparent(m, SOUND_MIXER_PCM);
2042 			if (parent != SOUND_MIXER_NONE)
2043 				pvol = mix_get(m, parent);
2044 			else
2045 				pvol = 100 | (100 << 8);
2046 			/* CHN_LOCK(c); */
2047 		} else {
2048 			vol = 100 | (100 << 8);
2049 			pvol = vol;
2050 		}
2051 
2052 		if (vol == -1) {
2053 			device_printf(c->dev,
2054 			    "Soft PCM Volume: Failed to read pcm "
2055 			    "default value\n");
2056 			vol = 100 | (100 << 8);
2057 		}
2058 
2059 		if (pvol == -1) {
2060 			device_printf(c->dev,
2061 			    "Soft PCM Volume: Failed to read parent "
2062 			    "default value\n");
2063 			pvol = 100 | (100 << 8);
2064 		}
2065 
2066 		left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2067 		right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2068 		center = (left + right) >> 1;
2069 
2070 		chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2071 	}
2072 
2073 	if (c->feederflags & (1 << FEEDER_EQ)) {
2074 		struct pcm_feeder *f;
2075 		int treble, bass, state;
2076 
2077 		/* CHN_UNLOCK(c); */
2078 		treble = mix_get(m, SOUND_MIXER_TREBLE);
2079 		bass = mix_get(m, SOUND_MIXER_BASS);
2080 		/* CHN_LOCK(c); */
2081 
2082 		if (treble == -1)
2083 			treble = 50;
2084 		else
2085 			treble = ((treble & 0x7f) +
2086 			    ((treble >> 8) & 0x7f)) >> 1;
2087 
2088 		if (bass == -1)
2089 			bass = 50;
2090 		else
2091 			bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2092 
2093 		f = chn_findfeeder(c, FEEDER_EQ);
2094 		if (f != NULL) {
2095 			if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2096 				device_printf(c->dev,
2097 				    "EQ: Failed to set treble -- %d\n",
2098 				    treble);
2099 			if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2100 				device_printf(c->dev,
2101 				    "EQ: Failed to set bass -- %d\n",
2102 				    bass);
2103 			if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2104 				device_printf(c->dev,
2105 				    "EQ: Failed to set preamp -- %d\n",
2106 				    d->eqpreamp);
2107 			if (d->flags & SD_F_EQ_BYPASSED)
2108 				state = FEEDEQ_BYPASS;
2109 			else if (d->flags & SD_F_EQ_ENABLED)
2110 				state = FEEDEQ_ENABLE;
2111 			else
2112 				state = FEEDEQ_DISABLE;
2113 			if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2114 				device_printf(c->dev,
2115 				    "EQ: Failed to set state -- %d\n", state);
2116 		}
2117 	}
2118 }
2119 
2120 int
2121 chn_trigger(struct pcm_channel *c, int go)
2122 {
2123 #ifdef DEV_ISA
2124     	struct snd_dbuf *b = c->bufhard;
2125 #endif
2126 	struct snddev_info *d = c->parentsnddev;
2127 	int ret;
2128 
2129 	CHN_LOCKASSERT(c);
2130 #ifdef DEV_ISA
2131 	if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
2132 		sndbuf_dmabounce(b);
2133 #endif
2134 	if (!PCMTRIG_COMMON(go))
2135 		return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2136 
2137 	if (go == c->trigger)
2138 		return (0);
2139 
2140 	ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2141 	if (ret != 0)
2142 		return (ret);
2143 
2144 	switch (go) {
2145 	case PCMTRIG_START:
2146 		if (snd_verbose > 3)
2147 			device_printf(c->dev,
2148 			    "%s() %s: calling go=0x%08x , "
2149 			    "prev=0x%08x\n", __func__, c->name, go,
2150 			    c->trigger);
2151 		if (c->trigger != PCMTRIG_START) {
2152 			c->trigger = go;
2153 			CHN_UNLOCK(c);
2154 			PCM_LOCK(d);
2155 			CHN_INSERT_HEAD(d, c, channels.pcm.busy);
2156 			PCM_UNLOCK(d);
2157 			CHN_LOCK(c);
2158 			chn_syncstate(c);
2159 		}
2160 		break;
2161 	case PCMTRIG_STOP:
2162 	case PCMTRIG_ABORT:
2163 		if (snd_verbose > 3)
2164 			device_printf(c->dev,
2165 			    "%s() %s: calling go=0x%08x , "
2166 			    "prev=0x%08x\n", __func__, c->name, go,
2167 			    c->trigger);
2168 		if (c->trigger == PCMTRIG_START) {
2169 			c->trigger = go;
2170 			CHN_UNLOCK(c);
2171 			PCM_LOCK(d);
2172 			CHN_REMOVE(d, c, channels.pcm.busy);
2173 			PCM_UNLOCK(d);
2174 			CHN_LOCK(c);
2175 		}
2176 		break;
2177 	default:
2178 		break;
2179 	}
2180 
2181 	return (0);
2182 }
2183 
2184 /**
2185  * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2186  *
2187  * This function obtains the hardware pointer location, then aligns it to
2188  * the current bytes-per-sample value before returning.  (E.g., a channel
2189  * running in 16 bit stereo mode would require 4 bytes per sample, so a
2190  * hwptr value ranging from 32-35 would be returned as 32.)
2191  *
2192  * @param c	PCM channel context
2193  * @returns 	sample-aligned hardware buffer pointer index
2194  */
2195 int
2196 chn_getptr(struct pcm_channel *c)
2197 {
2198 	int hwptr;
2199 
2200 	CHN_LOCKASSERT(c);
2201 	hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2202 	return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
2203 }
2204 
2205 struct pcmchan_caps *
2206 chn_getcaps(struct pcm_channel *c)
2207 {
2208 	CHN_LOCKASSERT(c);
2209 	return CHANNEL_GETCAPS(c->methods, c->devinfo);
2210 }
2211 
2212 u_int32_t
2213 chn_getformats(struct pcm_channel *c)
2214 {
2215 	u_int32_t *fmtlist, fmts;
2216 	int i;
2217 
2218 	fmtlist = chn_getcaps(c)->fmtlist;
2219 	fmts = 0;
2220 	for (i = 0; fmtlist[i]; i++)
2221 		fmts |= fmtlist[i];
2222 
2223 	/* report software-supported formats */
2224 	if (!CHN_BITPERFECT(c) && report_soft_formats)
2225 		fmts |= AFMT_CONVERTIBLE;
2226 
2227 	return (AFMT_ENCODING(fmts));
2228 }
2229 
2230 int
2231 chn_notify(struct pcm_channel *c, u_int32_t flags)
2232 {
2233 	struct pcm_channel *ch;
2234 	struct pcmchan_caps *caps;
2235 	uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2236 	uint32_t vpflags;
2237 	int dirty, err, run, nrun;
2238 
2239 	CHN_LOCKASSERT(c);
2240 
2241 	if (CHN_EMPTY(c, children))
2242 		return (ENODEV);
2243 
2244 	err = 0;
2245 
2246 	/*
2247 	 * If the hwchan is running, we can't change its rate, format or
2248 	 * blocksize
2249 	 */
2250 	run = (CHN_STARTED(c)) ? 1 : 0;
2251 	if (run)
2252 		flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
2253 
2254 	if (flags & CHN_N_RATE) {
2255 		/*
2256 		 * XXX I'll make good use of this someday.
2257 		 *     However this is currently being superseded by
2258 		 *     the availability of CHN_F_VCHAN_DYNAMIC.
2259 		 */
2260 	}
2261 
2262 	if (flags & CHN_N_FORMAT) {
2263 		/*
2264 		 * XXX I'll make good use of this someday.
2265 		 *     However this is currently being superseded by
2266 		 *     the availability of CHN_F_VCHAN_DYNAMIC.
2267 		 */
2268 	}
2269 
2270 	if (flags & CHN_N_VOLUME) {
2271 		/*
2272 		 * XXX I'll make good use of this someday, though
2273 		 *     soft volume control is currently pretty much
2274 		 *     integrated.
2275 		 */
2276 	}
2277 
2278 	if (flags & CHN_N_BLOCKSIZE) {
2279 		/*
2280 		 * Set to default latency profile
2281 		 */
2282 		chn_setlatency(c, chn_latency);
2283 	}
2284 
2285 	if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2286 		nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
2287 		if (nrun && !run)
2288 			err = chn_start(c, 1);
2289 		if (!nrun && run)
2290 			chn_abort(c);
2291 		flags &= ~CHN_N_TRIGGER;
2292 	}
2293 
2294 	if (flags & CHN_N_TRIGGER) {
2295 		if (c->direction == PCMDIR_PLAY) {
2296 			vchanformat = &c->parentsnddev->pvchanformat;
2297 			vchanrate = &c->parentsnddev->pvchanrate;
2298 		} else {
2299 			vchanformat = &c->parentsnddev->rvchanformat;
2300 			vchanrate = &c->parentsnddev->rvchanrate;
2301 		}
2302 
2303 		/* Dynamic Virtual Channel */
2304 		if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2305 			bestformat = *vchanformat;
2306 			bestspeed = *vchanrate;
2307 		} else {
2308 			bestformat = 0;
2309 			bestspeed = 0;
2310 		}
2311 
2312 		besthwformat = 0;
2313 		nrun = 0;
2314 		caps = chn_getcaps(c);
2315 		dirty = 0;
2316 		vpflags = 0;
2317 
2318 		CHN_FOREACH(ch, c, children.busy) {
2319 			CHN_LOCK(ch);
2320 			if ((ch->format & AFMT_PASSTHROUGH) &&
2321 			    snd_fmtvalid(ch->format, caps->fmtlist)) {
2322 				bestformat = ch->format;
2323 				bestspeed = ch->speed;
2324 				CHN_UNLOCK(ch);
2325 				vpflags = CHN_F_PASSTHROUGH;
2326 				nrun++;
2327 				break;
2328 			}
2329 			if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2330 				if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2331 					bestspeed = ch->speed;
2332 					RANGE(bestspeed, caps->minspeed,
2333 					    caps->maxspeed);
2334 					besthwformat = snd_fmtbest(ch->format,
2335 					    caps->fmtlist);
2336 					if (besthwformat != 0)
2337 						bestformat = besthwformat;
2338 				}
2339 				CHN_UNLOCK(ch);
2340 				vpflags = CHN_F_EXCLUSIVE;
2341 				nrun++;
2342 				continue;
2343 			}
2344 			if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2345 			    vpflags != 0) {
2346 				CHN_UNLOCK(ch);
2347 				nrun++;
2348 				continue;
2349 			}
2350 			if (ch->speed > bestspeed) {
2351 				bestspeed = ch->speed;
2352 				RANGE(bestspeed, caps->minspeed,
2353 				    caps->maxspeed);
2354 			}
2355 			besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2356 			if (!(besthwformat & AFMT_VCHAN)) {
2357 				CHN_UNLOCK(ch);
2358 				nrun++;
2359 				continue;
2360 			}
2361 			if (AFMT_CHANNEL(besthwformat) >
2362 			    AFMT_CHANNEL(bestformat))
2363 				bestformat = besthwformat;
2364 			else if (AFMT_CHANNEL(besthwformat) ==
2365 			    AFMT_CHANNEL(bestformat) &&
2366 			    AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2367 				bestformat = besthwformat;
2368 			CHN_UNLOCK(ch);
2369 			nrun++;
2370 		}
2371 
2372 		if (bestformat == 0)
2373 			bestformat = c->format;
2374 		if (bestspeed == 0)
2375 			bestspeed = c->speed;
2376 
2377 		if (bestformat != c->format || bestspeed != c->speed)
2378 			dirty = 1;
2379 
2380 		c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2381 		c->flags |= vpflags;
2382 
2383 		if (nrun && !run) {
2384 			if (dirty) {
2385 				bestspeed = CHANNEL_SETSPEED(c->methods,
2386 				    c->devinfo, bestspeed);
2387 				err = chn_reset(c, bestformat, bestspeed);
2388 			}
2389 			if (err == 0 && dirty) {
2390 				CHN_FOREACH(ch, c, children.busy) {
2391 					CHN_LOCK(ch);
2392 					if (VCHAN_SYNC_REQUIRED(ch))
2393 						vchan_sync(ch);
2394 					CHN_UNLOCK(ch);
2395 				}
2396 			}
2397 			if (err == 0) {
2398 				if (dirty)
2399 					c->flags |= CHN_F_DIRTY;
2400 				err = chn_start(c, 1);
2401 			}
2402 		}
2403 
2404 		if (nrun && run && dirty) {
2405 			chn_abort(c);
2406 			bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2407 			    bestspeed);
2408 			err = chn_reset(c, bestformat, bestspeed);
2409 			if (err == 0) {
2410 				CHN_FOREACH(ch, c, children.busy) {
2411 					CHN_LOCK(ch);
2412 					if (VCHAN_SYNC_REQUIRED(ch))
2413 						vchan_sync(ch);
2414 					CHN_UNLOCK(ch);
2415 				}
2416 			}
2417 			if (err == 0) {
2418 				c->flags |= CHN_F_DIRTY;
2419 				err = chn_start(c, 1);
2420 			}
2421 		}
2422 
2423 		if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2424 		    (bestformat & AFMT_VCHAN)) {
2425 			*vchanformat = bestformat;
2426 			*vchanrate = bestspeed;
2427 		}
2428 
2429 		if (!nrun && run) {
2430 			c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2431 			bestformat = *vchanformat;
2432 			bestspeed = *vchanrate;
2433 			chn_abort(c);
2434 			if (c->format != bestformat || c->speed != bestspeed)
2435 				chn_reset(c, bestformat, bestspeed);
2436 		}
2437 	}
2438 
2439 	return (err);
2440 }
2441 
2442 /**
2443  * @brief Fetch array of supported discrete sample rates
2444  *
2445  * Wrapper for CHANNEL_GETRATES.  Please see channel_if.m:getrates() for
2446  * detailed information.
2447  *
2448  * @note If the operation isn't supported, this function will just return 0
2449  *       (no rates in the array), and *rates will be set to NULL.  Callers
2450  *       should examine rates @b only if this function returns non-zero.
2451  *
2452  * @param c	pcm channel to examine
2453  * @param rates	pointer to array of integers; rate table will be recorded here
2454  *
2455  * @return number of rates in the array pointed to be @c rates
2456  */
2457 int
2458 chn_getrates(struct pcm_channel *c, int **rates)
2459 {
2460 	KASSERT(rates != NULL, ("rates is null"));
2461 	CHN_LOCKASSERT(c);
2462 	return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2463 }
2464 
2465 /**
2466  * @brief Remove channel from a sync group, if there is one.
2467  *
2468  * This function is initially intended for the following conditions:
2469  *   - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2470  *   - Closing a device.  (A channel can't be destroyed if it's still in use.)
2471  *
2472  * @note Before calling this function, the syncgroup list mutex must be
2473  * held.  (Consider pcm_channel::sm protected by the SG list mutex
2474  * whether @c c is locked or not.)
2475  *
2476  * @param c	channel device to be started or closed
2477  * @returns	If this channel was the only member of a group, the group ID
2478  * 		is returned to the caller so that the caller can release it
2479  * 		via free_unr() after giving up the syncgroup lock.  Else it
2480  * 		returns 0.
2481  */
2482 int
2483 chn_syncdestroy(struct pcm_channel *c)
2484 {
2485 	struct pcmchan_syncmember *sm;
2486 	struct pcmchan_syncgroup *sg;
2487 	int sg_id;
2488 
2489 	sg_id = 0;
2490 
2491 	PCM_SG_LOCKASSERT(MA_OWNED);
2492 
2493 	if (c->sm != NULL) {
2494 		sm = c->sm;
2495 		sg = sm->parent;
2496 		c->sm = NULL;
2497 
2498 		KASSERT(sg != NULL, ("syncmember has null parent"));
2499 
2500 		SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2501 		free(sm, M_DEVBUF);
2502 
2503 		if (SLIST_EMPTY(&sg->members)) {
2504 			SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2505 			sg_id = sg->id;
2506 			free(sg, M_DEVBUF);
2507 		}
2508 	}
2509 
2510 	return sg_id;
2511 }
2512 
2513 #ifdef OSSV4_EXPERIMENT
2514 int
2515 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2516 {
2517 	CHN_LOCKASSERT(c);
2518 	return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);
2519 }
2520 #endif
2521