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