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