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