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