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