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