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