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