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