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 err;
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 if ((err = CHANNEL_RESET(c->methods, c->devinfo)))
1075 return (err);
1076
1077 if (fmt != 0 && spd != 0) {
1078 if ((err = chn_setparam(c, fmt, spd)))
1079 return (err);
1080 fmt = 0;
1081 spd = 0;
1082 }
1083 if (fmt != 0 && (err = chn_setformat(c, fmt)))
1084 return (err);
1085 if (spd != 0 && (err = chn_setspeed(c, spd)))
1086 return (err);
1087
1088 if ((err = chn_setlatency(c, chn_latency)))
1089 return (err);
1090
1091 chn_resetbuf(c);
1092
1093 return (CHANNEL_RESETDONE(c->methods, c->devinfo));
1094 }
1095
1096 static struct unrhdr *
chn_getunr(struct snddev_info * d,int type)1097 chn_getunr(struct snddev_info *d, int type)
1098 {
1099 switch (type) {
1100 case PCMDIR_PLAY:
1101 return (d->p_unr);
1102 case PCMDIR_PLAY_VIRTUAL:
1103 return (d->vp_unr);
1104 case PCMDIR_REC:
1105 return (d->r_unr);
1106 case PCMDIR_REC_VIRTUAL:
1107 return (d->vr_unr);
1108 default:
1109 __assert_unreachable();
1110 }
1111
1112 }
1113
1114 char *
chn_mkname(char * buf,size_t len,struct pcm_channel * c)1115 chn_mkname(char *buf, size_t len, struct pcm_channel *c)
1116 {
1117 const char *str;
1118
1119 KASSERT(buf != NULL && len != 0,
1120 ("%s(): bogus buf=%p len=%zu", __func__, buf, len));
1121
1122 switch (c->type) {
1123 case PCMDIR_PLAY:
1124 str = "play";
1125 break;
1126 case PCMDIR_PLAY_VIRTUAL:
1127 str = "virtual_play";
1128 break;
1129 case PCMDIR_REC:
1130 str = "record";
1131 break;
1132 case PCMDIR_REC_VIRTUAL:
1133 str = "virtual_record";
1134 break;
1135 default:
1136 __assert_unreachable();
1137 }
1138
1139 snprintf(buf, len, "dsp%d.%s.%d",
1140 device_get_unit(c->dev), str, c->unit);
1141
1142 return (buf);
1143 }
1144
1145 struct pcm_channel *
chn_init(struct snddev_info * d,struct pcm_channel * parent,kobj_class_t cls,int dir,void * devinfo)1146 chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls,
1147 int dir, void *devinfo)
1148 {
1149 struct pcm_channel *c;
1150 struct snd_dbuf *b, *bs;
1151 char buf[CHN_NAMELEN];
1152 int err, i, direction, *vchanrate, *vchanformat;
1153
1154 PCM_BUSYASSERT(d);
1155 PCM_LOCKASSERT(d);
1156
1157 switch (dir) {
1158 case PCMDIR_PLAY:
1159 d->playcount++;
1160 /* FALLTHROUGH */
1161 case PCMDIR_PLAY_VIRTUAL:
1162 if (dir == PCMDIR_PLAY_VIRTUAL)
1163 d->pvchancount++;
1164 direction = PCMDIR_PLAY;
1165 vchanrate = &d->pvchanrate;
1166 vchanformat = &d->pvchanformat;
1167 break;
1168 case PCMDIR_REC:
1169 d->reccount++;
1170 /* FALLTHROUGH */
1171 case PCMDIR_REC_VIRTUAL:
1172 if (dir == PCMDIR_REC_VIRTUAL)
1173 d->rvchancount++;
1174 direction = PCMDIR_REC;
1175 vchanrate = &d->rvchanrate;
1176 vchanformat = &d->rvchanformat;
1177 break;
1178 default:
1179 device_printf(d->dev,
1180 "%s(): invalid channel direction: %d\n",
1181 __func__, dir);
1182 return (NULL);
1183 }
1184
1185 PCM_UNLOCK(d);
1186 b = NULL;
1187 bs = NULL;
1188
1189 c = malloc(sizeof(*c), M_DEVBUF, M_WAITOK | M_ZERO);
1190 c->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
1191 chn_lockinit(c, dir);
1192 CHN_INIT(c, children);
1193 CHN_INIT(c, children.busy);
1194 c->direction = direction;
1195 c->type = dir;
1196 c->unit = alloc_unr(chn_getunr(d, c->type));
1197 c->format = SND_FORMAT(AFMT_S16_LE, 2, 0);
1198 c->speed = 48000;
1199 c->pid = -1;
1200 c->latency = -1;
1201 c->timeout = 1;
1202 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
1203 c->parentsnddev = d;
1204 c->parentchannel = parent;
1205 c->dev = d->dev;
1206 c->trigger = PCMTRIG_STOP;
1207 strlcpy(c->name, chn_mkname(buf, sizeof(buf), c), sizeof(c->name));
1208
1209 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1210 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1211
1212 for (i = 0; i < SND_CHN_T_MAX; i++)
1213 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1214
1215 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1216 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1217
1218 CHN_LOCK(c);
1219 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1220 CHN_UNLOCK(c);
1221
1222 b = sndbuf_create(c, "primary");
1223 bs = sndbuf_create(c, "secondary");
1224 if (b == NULL || bs == NULL) {
1225 device_printf(d->dev, "%s(): failed to create %s buffer\n",
1226 __func__, b == NULL ? "hardware" : "software");
1227 goto fail;
1228 }
1229 c->bufhard = b;
1230 c->bufsoft = bs;
1231 knlist_init_mtx(&bs->sel.si_note, &c->lock);
1232
1233 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1234 if (c->devinfo == NULL) {
1235 device_printf(d->dev, "%s(): CHANNEL_INIT() failed\n", __func__);
1236 goto fail;
1237 }
1238
1239 if (b->bufsize == 0 && ((c->flags & CHN_F_VIRTUAL) == 0)) {
1240 device_printf(d->dev, "%s(): hardware buffer's size is 0\n",
1241 __func__);
1242 goto fail;
1243 }
1244
1245 sndbuf_setfmt(b, c->format);
1246 sndbuf_setspd(b, c->speed);
1247 sndbuf_setfmt(bs, c->format);
1248 sndbuf_setspd(bs, c->speed);
1249 sndbuf_setup(bs, NULL, 0);
1250
1251 /**
1252 * @todo Should this be moved somewhere else? The primary buffer
1253 * is allocated by the driver or via DMA map setup, and tmpbuf
1254 * seems to only come into existence in sndbuf_resize().
1255 */
1256 if (c->direction == PCMDIR_PLAY) {
1257 bs->sl = bs->maxsize;
1258 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_WAITOK);
1259 }
1260
1261 if ((c->flags & CHN_F_VIRTUAL) == 0) {
1262 CHN_LOCK(c);
1263 err = chn_reset(c, c->format, c->speed);
1264 CHN_UNLOCK(c);
1265 if (err != 0)
1266 goto fail;
1267 }
1268
1269 PCM_LOCK(d);
1270 CHN_INSERT_SORT_ASCEND(d, c, channels.pcm);
1271 if ((c->flags & CHN_F_VIRTUAL) == 0) {
1272 CHN_INSERT_SORT_ASCEND(d, c, channels.pcm.primary);
1273 /* Initialize the *vchanrate/vchanformat parameters. */
1274 *vchanrate = c->bufsoft->spd;
1275 *vchanformat = c->bufsoft->fmt;
1276 }
1277
1278 return (c);
1279
1280 fail:
1281 chn_kill(c);
1282 PCM_LOCK(d);
1283
1284 return (NULL);
1285 }
1286
1287 void
chn_kill(struct pcm_channel * c)1288 chn_kill(struct pcm_channel *c)
1289 {
1290 struct snddev_info *d = c->parentsnddev;
1291 struct snd_dbuf *b = c->bufhard;
1292 struct snd_dbuf *bs = c->bufsoft;
1293
1294 PCM_BUSYASSERT(c->parentsnddev);
1295
1296 PCM_LOCK(d);
1297 CHN_REMOVE(d, c, channels.pcm);
1298 if ((c->flags & CHN_F_VIRTUAL) == 0)
1299 CHN_REMOVE(d, c, channels.pcm.primary);
1300
1301 switch (c->type) {
1302 case PCMDIR_PLAY:
1303 d->playcount--;
1304 break;
1305 case PCMDIR_PLAY_VIRTUAL:
1306 d->pvchancount--;
1307 break;
1308 case PCMDIR_REC:
1309 d->reccount--;
1310 break;
1311 case PCMDIR_REC_VIRTUAL:
1312 d->rvchancount--;
1313 break;
1314 default:
1315 __assert_unreachable();
1316 }
1317 PCM_UNLOCK(d);
1318
1319 if (CHN_STARTED(c)) {
1320 CHN_LOCK(c);
1321 chn_trigger(c, PCMTRIG_ABORT);
1322 CHN_UNLOCK(c);
1323 }
1324 free_unr(chn_getunr(d, c->type), c->unit);
1325 feeder_remove(c);
1326 if (c->devinfo)
1327 CHANNEL_FREE(c->methods, c->devinfo);
1328 if (bs) {
1329 knlist_clear(&bs->sel.si_note, 0);
1330 knlist_destroy(&bs->sel.si_note);
1331 sndbuf_destroy(bs);
1332 }
1333 if (b)
1334 sndbuf_destroy(b);
1335 CHN_LOCK(c);
1336 c->flags |= CHN_F_DEAD;
1337 chn_lockdestroy(c);
1338 kobj_delete(c->methods, M_DEVBUF);
1339 free(c, M_DEVBUF);
1340 }
1341
1342 void
chn_shutdown(struct pcm_channel * c)1343 chn_shutdown(struct pcm_channel *c)
1344 {
1345 CHN_LOCKASSERT(c);
1346
1347 chn_wakeup(c);
1348 c->flags |= CHN_F_DEAD;
1349 }
1350
1351 /* release a locked channel and unlock it */
1352 int
chn_release(struct pcm_channel * c)1353 chn_release(struct pcm_channel *c)
1354 {
1355 PCM_BUSYASSERT(c->parentsnddev);
1356 CHN_LOCKASSERT(c);
1357
1358 c->flags &= ~CHN_F_BUSY;
1359 c->pid = -1;
1360 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
1361 CHN_UNLOCK(c);
1362
1363 return (0);
1364 }
1365
1366 int
chn_setvolume_multi(struct pcm_channel * c,int vc,int left,int right,int center)1367 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1368 int center)
1369 {
1370 int i, ret;
1371
1372 ret = 0;
1373
1374 for (i = 0; i < SND_CHN_T_MAX; i++) {
1375 if ((1 << i) & SND_CHN_LEFT_MASK)
1376 ret |= chn_setvolume_matrix(c, vc, i, left);
1377 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1378 ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1379 else
1380 ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1381 }
1382
1383 return (ret);
1384 }
1385
1386 int
chn_setvolume_matrix(struct pcm_channel * c,int vc,int vt,int val)1387 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1388 {
1389 int i;
1390
1391 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1392 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1393 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1394 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1395 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1396 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1397 __func__, c, vc, vt, val));
1398 CHN_LOCKASSERT(c);
1399
1400 if (val < 0)
1401 val = 0;
1402 if (val > 100)
1403 val = 100;
1404
1405 c->volume[vc][vt] = val;
1406
1407 /*
1408 * Do relative calculation here and store it into class + 1
1409 * to ease the job of feeder_volume.
1410 */
1411 if (vc == SND_VOL_C_MASTER) {
1412 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1413 vc += SND_VOL_C_STEP)
1414 c->volume[SND_VOL_C_VAL(vc)][vt] =
1415 SND_VOL_CALC_VAL(c->volume, vc, vt);
1416 } else if (vc & 1) {
1417 if (vt == SND_CHN_T_VOL_0DB)
1418 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1419 i += SND_CHN_T_STEP) {
1420 c->volume[SND_VOL_C_VAL(vc)][i] =
1421 SND_VOL_CALC_VAL(c->volume, vc, i);
1422 }
1423 else
1424 c->volume[SND_VOL_C_VAL(vc)][vt] =
1425 SND_VOL_CALC_VAL(c->volume, vc, vt);
1426 }
1427
1428 return (val);
1429 }
1430
1431 int
chn_getvolume_matrix(struct pcm_channel * c,int vc,int vt)1432 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1433 {
1434 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1435 (vt == SND_CHN_T_VOL_0DB ||
1436 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1437 ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1438 __func__, c, vc, vt));
1439 CHN_LOCKASSERT(c);
1440
1441 return (c->volume[vc][vt]);
1442 }
1443
1444 int
chn_setmute_multi(struct pcm_channel * c,int vc,int mute)1445 chn_setmute_multi(struct pcm_channel *c, int vc, int mute)
1446 {
1447 int i, ret;
1448
1449 ret = 0;
1450
1451 for (i = 0; i < SND_CHN_T_MAX; i++) {
1452 if ((1 << i) & SND_CHN_LEFT_MASK)
1453 ret |= chn_setmute_matrix(c, vc, i, mute);
1454 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1455 ret |= chn_setmute_matrix(c, vc, i, mute) << 8;
1456 else
1457 ret |= chn_setmute_matrix(c, vc, i, mute) << 16;
1458 }
1459 return (ret);
1460 }
1461
1462 int
chn_setmute_matrix(struct pcm_channel * c,int vc,int vt,int mute)1463 chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute)
1464 {
1465 int i;
1466
1467 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1468 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1469 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1470 ("%s(): invalid mute matrix c=%p vc=%d vt=%d mute=%d",
1471 __func__, c, vc, vt, mute));
1472
1473 CHN_LOCKASSERT(c);
1474
1475 mute = (mute != 0);
1476
1477 c->muted[vc][vt] = mute;
1478
1479 /*
1480 * Do relative calculation here and store it into class + 1
1481 * to ease the job of feeder_volume.
1482 */
1483 if (vc == SND_VOL_C_MASTER) {
1484 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1485 vc += SND_VOL_C_STEP)
1486 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
1487 } else if (vc & 1) {
1488 if (vt == SND_CHN_T_VOL_0DB) {
1489 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1490 i += SND_CHN_T_STEP) {
1491 c->muted[SND_VOL_C_VAL(vc)][i] = mute;
1492 }
1493 } else {
1494 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
1495 }
1496 }
1497 return (mute);
1498 }
1499
1500 int
chn_getmute_matrix(struct pcm_channel * c,int vc,int vt)1501 chn_getmute_matrix(struct pcm_channel *c, int vc, int vt)
1502 {
1503 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1504 (vt == SND_CHN_T_VOL_0DB ||
1505 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1506 ("%s(): invalid mute matrix c=%p vc=%d vt=%d",
1507 __func__, c, vc, vt));
1508 CHN_LOCKASSERT(c);
1509
1510 return (c->muted[vc][vt]);
1511 }
1512
1513 struct pcmchan_matrix *
chn_getmatrix(struct pcm_channel * c)1514 chn_getmatrix(struct pcm_channel *c)
1515 {
1516
1517 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1518 CHN_LOCKASSERT(c);
1519
1520 if (!(c->format & AFMT_CONVERTIBLE))
1521 return (NULL);
1522
1523 return (&c->matrix);
1524 }
1525
1526 int
chn_setmatrix(struct pcm_channel * c,struct pcmchan_matrix * m)1527 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1528 {
1529
1530 KASSERT(c != NULL && m != NULL,
1531 ("%s(): NULL channel or matrix", __func__));
1532 CHN_LOCKASSERT(c);
1533
1534 if (!(c->format & AFMT_CONVERTIBLE))
1535 return (EINVAL);
1536
1537 c->matrix = *m;
1538 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1539
1540 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1541 }
1542
1543 /*
1544 * XXX chn_oss_* exists for the sake of compatibility.
1545 */
1546 int
chn_oss_getorder(struct pcm_channel * c,unsigned long long * map)1547 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1548 {
1549
1550 KASSERT(c != NULL && map != NULL,
1551 ("%s(): NULL channel or map", __func__));
1552 CHN_LOCKASSERT(c);
1553
1554 if (!(c->format & AFMT_CONVERTIBLE))
1555 return (EINVAL);
1556
1557 return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1558 }
1559
1560 int
chn_oss_setorder(struct pcm_channel * c,unsigned long long * map)1561 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1562 {
1563 struct pcmchan_matrix m;
1564 int ret;
1565
1566 KASSERT(c != NULL && map != NULL,
1567 ("%s(): NULL channel or map", __func__));
1568 CHN_LOCKASSERT(c);
1569
1570 if (!(c->format & AFMT_CONVERTIBLE))
1571 return (EINVAL);
1572
1573 m = c->matrix;
1574 ret = feeder_matrix_oss_set_channel_order(&m, map);
1575 if (ret != 0)
1576 return (ret);
1577
1578 return (chn_setmatrix(c, &m));
1579 }
1580
1581 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1582 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1583 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1584 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1585
1586 int
chn_oss_getmask(struct pcm_channel * c,uint32_t * retmask)1587 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1588 {
1589 struct pcmchan_matrix *m;
1590 struct pcmchan_caps *caps;
1591 uint32_t i, format;
1592
1593 KASSERT(c != NULL && retmask != NULL,
1594 ("%s(): NULL channel or retmask", __func__));
1595 CHN_LOCKASSERT(c);
1596
1597 caps = chn_getcaps(c);
1598 if (caps == NULL || caps->fmtlist == NULL)
1599 return (ENODEV);
1600
1601 for (i = 0; caps->fmtlist[i] != 0; i++) {
1602 format = caps->fmtlist[i];
1603 if (!(format & AFMT_CONVERTIBLE)) {
1604 *retmask |= DSP_BIND_SPDIF;
1605 continue;
1606 }
1607 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1608 if (m == NULL)
1609 continue;
1610 if (m->mask & SND_CHN_OSS_FRONT)
1611 *retmask |= DSP_BIND_FRONT;
1612 if (m->mask & SND_CHN_OSS_SURR)
1613 *retmask |= DSP_BIND_SURR;
1614 if (m->mask & SND_CHN_OSS_CENTER_LFE)
1615 *retmask |= DSP_BIND_CENTER_LFE;
1616 if (m->mask & SND_CHN_OSS_REAR)
1617 *retmask |= DSP_BIND_REAR;
1618 }
1619
1620 /* report software-supported binding mask */
1621 if (!CHN_BITPERFECT(c) && report_soft_matrix)
1622 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1623 DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1624
1625 return (0);
1626 }
1627
1628 void
chn_vpc_reset(struct pcm_channel * c,int vc,int force)1629 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1630 {
1631 int i;
1632
1633 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1634 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1635 CHN_LOCKASSERT(c);
1636
1637 if (force == 0 && chn_vpc_autoreset == 0)
1638 return;
1639
1640 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1641 chn_setvolume_matrix(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1642 }
1643
1644 static u_int32_t
round_pow2(u_int32_t v)1645 round_pow2(u_int32_t v)
1646 {
1647 u_int32_t ret;
1648
1649 if (v < 2)
1650 v = 2;
1651 ret = 0;
1652 while (v >> ret)
1653 ret++;
1654 ret = 1 << (ret - 1);
1655 while (ret < v)
1656 ret <<= 1;
1657 return ret;
1658 }
1659
1660 static u_int32_t
round_blksz(u_int32_t v,int round)1661 round_blksz(u_int32_t v, int round)
1662 {
1663 u_int32_t ret, tmp;
1664
1665 if (round < 1)
1666 round = 1;
1667
1668 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1669
1670 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1671 ret >>= 1;
1672
1673 tmp = ret - (ret % round);
1674 while (tmp < 16 || tmp < round) {
1675 ret <<= 1;
1676 tmp = ret - (ret % round);
1677 }
1678
1679 return ret;
1680 }
1681
1682 /*
1683 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1684 * is to keep 2nd buffer short so that it doesn't cause long queue during
1685 * buffer transfer.
1686 *
1687 * Latency reference table for 48khz stereo 16bit: (PLAY)
1688 *
1689 * +---------+------------+-----------+------------+
1690 * | Latency | Blockcount | Blocksize | Buffersize |
1691 * +---------+------------+-----------+------------+
1692 * | 0 | 2 | 64 | 128 |
1693 * +---------+------------+-----------+------------+
1694 * | 1 | 4 | 128 | 512 |
1695 * +---------+------------+-----------+------------+
1696 * | 2 | 8 | 512 | 4096 |
1697 * +---------+------------+-----------+------------+
1698 * | 3 | 16 | 512 | 8192 |
1699 * +---------+------------+-----------+------------+
1700 * | 4 | 32 | 512 | 16384 |
1701 * +---------+------------+-----------+------------+
1702 * | 5 | 32 | 1024 | 32768 |
1703 * +---------+------------+-----------+------------+
1704 * | 6 | 16 | 2048 | 32768 |
1705 * +---------+------------+-----------+------------+
1706 * | 7 | 8 | 4096 | 32768 |
1707 * +---------+------------+-----------+------------+
1708 * | 8 | 4 | 8192 | 32768 |
1709 * +---------+------------+-----------+------------+
1710 * | 9 | 2 | 16384 | 32768 |
1711 * +---------+------------+-----------+------------+
1712 * | 10 | 2 | 32768 | 65536 |
1713 * +---------+------------+-----------+------------+
1714 *
1715 * Recording need a different reference table. All we care is
1716 * gobbling up everything within reasonable buffering threshold.
1717 *
1718 * Latency reference table for 48khz stereo 16bit: (REC)
1719 *
1720 * +---------+------------+-----------+------------+
1721 * | Latency | Blockcount | Blocksize | Buffersize |
1722 * +---------+------------+-----------+------------+
1723 * | 0 | 512 | 32 | 16384 |
1724 * +---------+------------+-----------+------------+
1725 * | 1 | 256 | 64 | 16384 |
1726 * +---------+------------+-----------+------------+
1727 * | 2 | 128 | 128 | 16384 |
1728 * +---------+------------+-----------+------------+
1729 * | 3 | 64 | 256 | 16384 |
1730 * +---------+------------+-----------+------------+
1731 * | 4 | 32 | 512 | 16384 |
1732 * +---------+------------+-----------+------------+
1733 * | 5 | 32 | 1024 | 32768 |
1734 * +---------+------------+-----------+------------+
1735 * | 6 | 16 | 2048 | 32768 |
1736 * +---------+------------+-----------+------------+
1737 * | 7 | 8 | 4096 | 32768 |
1738 * +---------+------------+-----------+------------+
1739 * | 8 | 4 | 8192 | 32768 |
1740 * +---------+------------+-----------+------------+
1741 * | 9 | 2 | 16384 | 32768 |
1742 * +---------+------------+-----------+------------+
1743 * | 10 | 2 | 32768 | 65536 |
1744 * +---------+------------+-----------+------------+
1745 *
1746 * Calculations for other data rate are entirely based on these reference
1747 * tables. For normal operation, Latency 5 seems give the best, well
1748 * balanced performance for typical workload. Anything below 5 will
1749 * eat up CPU to keep up with increasing context switches because of
1750 * shorter buffer space and usually require the application to handle it
1751 * aggressively through possibly real time programming technique.
1752 *
1753 */
1754
1755 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1756
1757 static int
chn_calclatency(int dir,int latency,int bps,u_int32_t datarate,u_int32_t max,int * rblksz,int * rblkcnt)1758 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1759 u_int32_t max, int *rblksz, int *rblkcnt)
1760 {
1761 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = {
1762 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1},
1763 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}
1764 };
1765 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = {
1766 {7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16},
1767 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}
1768 };
1769 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = {
1770 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1},
1771 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}
1772 };
1773 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = {
1774 {14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},
1775 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}
1776 };
1777 u_int32_t bufsz;
1778 int lprofile, blksz, blkcnt;
1779
1780 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1781 bps < 1 || datarate < 1 ||
1782 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1783 if (rblksz != NULL)
1784 *rblksz = CHN_2NDBUFMAXSIZE >> 1;
1785 if (rblkcnt != NULL)
1786 *rblkcnt = 2;
1787 printf("%s(): FAILED dir=%d latency=%d bps=%d "
1788 "datarate=%u max=%u\n",
1789 __func__, dir, latency, bps, datarate, max);
1790 return CHN_2NDBUFMAXSIZE;
1791 }
1792
1793 lprofile = chn_latency_profile;
1794
1795 if (dir == PCMDIR_PLAY) {
1796 blkcnt = pblkcnts[lprofile][latency];
1797 bufsz = pbufszs[lprofile][latency];
1798 } else {
1799 blkcnt = rblkcnts[lprofile][latency];
1800 bufsz = rbufszs[lprofile][latency];
1801 }
1802
1803 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1804 datarate));
1805 if (bufsz > max)
1806 bufsz = max;
1807 blksz = round_blksz(bufsz >> blkcnt, bps);
1808
1809 if (rblksz != NULL)
1810 *rblksz = blksz;
1811 if (rblkcnt != NULL)
1812 *rblkcnt = 1 << blkcnt;
1813
1814 return blksz << blkcnt;
1815 }
1816
1817 static int
chn_resizebuf(struct pcm_channel * c,int latency,int blkcnt,int blksz)1818 chn_resizebuf(struct pcm_channel *c, int latency,
1819 int blkcnt, int blksz)
1820 {
1821 struct snd_dbuf *b, *bs, *pb;
1822 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1823 int ret;
1824
1825 CHN_LOCKASSERT(c);
1826
1827 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1828 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1829 return EINVAL;
1830
1831 if (latency == -1) {
1832 c->latency = -1;
1833 latency = chn_latency;
1834 } else if (latency == -2) {
1835 latency = c->latency;
1836 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1837 latency = chn_latency;
1838 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1839 return EINVAL;
1840 else {
1841 c->latency = latency;
1842 }
1843
1844 bs = c->bufsoft;
1845 b = c->bufhard;
1846
1847 if (!(blksz == 0 || blkcnt == -1) &&
1848 (blksz < 16 || blksz < bs->align || blkcnt < 2 ||
1849 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1850 return EINVAL;
1851
1852 chn_calclatency(c->direction, latency, bs->align,
1853 bs->align * bs->spd, CHN_2NDBUFMAXSIZE,
1854 &sblksz, &sblkcnt);
1855
1856 if (blksz == 0 || blkcnt == -1) {
1857 if (blkcnt == -1)
1858 c->flags &= ~CHN_F_HAS_SIZE;
1859 if (c->flags & CHN_F_HAS_SIZE) {
1860 blksz = bs->blksz;
1861 blkcnt = bs->blkcnt;
1862 }
1863 } else
1864 c->flags |= CHN_F_HAS_SIZE;
1865
1866 if (c->flags & CHN_F_HAS_SIZE) {
1867 /*
1868 * The application has requested their own blksz/blkcnt.
1869 * Just obey with it, and let them toast alone. We can
1870 * clamp it to the nearest latency profile, but that would
1871 * defeat the purpose of having custom control. The least
1872 * we can do is round it to the nearest ^2 and align it.
1873 */
1874 sblksz = round_blksz(blksz, bs->align);
1875 sblkcnt = round_pow2(blkcnt);
1876 }
1877
1878 if (c->parentchannel != NULL) {
1879 pb = c->parentchannel->bufsoft;
1880 CHN_UNLOCK(c);
1881 CHN_LOCK(c->parentchannel);
1882 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1883 CHN_UNLOCK(c->parentchannel);
1884 CHN_LOCK(c);
1885 if (c->direction == PCMDIR_PLAY) {
1886 limit = (pb != NULL) ?
1887 sndbuf_xbytes(pb->bufsize, pb, bs) : 0;
1888 } else {
1889 limit = (pb != NULL) ?
1890 sndbuf_xbytes(pb->blksz, pb, bs) * 2 : 0;
1891 }
1892 } else {
1893 hblkcnt = 2;
1894 if (c->flags & CHN_F_HAS_SIZE) {
1895 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1896 b->align);
1897 hblkcnt = round_pow2(bs->blkcnt);
1898 } else
1899 chn_calclatency(c->direction, latency,
1900 b->align, b->align * b->spd,
1901 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1902
1903 if ((hblksz << 1) > b->maxsize)
1904 hblksz = round_blksz(b->maxsize >> 1, b->align);
1905
1906 while ((hblksz * hblkcnt) > b->maxsize) {
1907 if (hblkcnt < 4)
1908 hblksz >>= 1;
1909 else
1910 hblkcnt >>= 1;
1911 }
1912
1913 hblksz -= hblksz % b->align;
1914
1915 CHN_UNLOCK(c);
1916 if (chn_usefrags == 0 ||
1917 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1918 hblksz, hblkcnt) != 0)
1919 b->blksz = CHANNEL_SETBLOCKSIZE(c->methods,
1920 c->devinfo, hblksz);
1921 CHN_LOCK(c);
1922
1923 if (!CHN_EMPTY(c, children)) {
1924 nsblksz = round_blksz(
1925 sndbuf_xbytes(b->blksz, b, bs), bs->align);
1926 nsblkcnt = b->blkcnt;
1927 if (c->direction == PCMDIR_PLAY) {
1928 do {
1929 nsblkcnt--;
1930 } while (nsblkcnt >= 2 &&
1931 nsblksz * nsblkcnt >= sblksz * sblkcnt);
1932 nsblkcnt++;
1933 }
1934 sblksz = nsblksz;
1935 sblkcnt = nsblkcnt;
1936 limit = 0;
1937 } else
1938 limit = sndbuf_xbytes(b->blksz, b, bs) * 2;
1939 }
1940
1941 if (limit > CHN_2NDBUFMAXSIZE)
1942 limit = CHN_2NDBUFMAXSIZE;
1943
1944 while ((sblksz * sblkcnt) < limit)
1945 sblkcnt <<= 1;
1946
1947 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1948 if (sblkcnt < 4)
1949 sblksz >>= 1;
1950 else
1951 sblkcnt >>= 1;
1952 }
1953
1954 sblksz -= sblksz % bs->align;
1955
1956 if (bs->blkcnt != sblkcnt || bs->blksz != sblksz ||
1957 bs->bufsize != (sblkcnt * sblksz)) {
1958 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1959 if (ret != 0) {
1960 device_printf(c->dev, "%s(): Failed: %d %d\n",
1961 __func__, sblkcnt, sblksz);
1962 return ret;
1963 }
1964 }
1965
1966 /*
1967 * Interrupt timeout
1968 */
1969 c->timeout = ((u_int64_t)hz * bs->bufsize) /
1970 ((u_int64_t)bs->spd * bs->align);
1971 if (c->parentchannel != NULL)
1972 c->timeout = min(c->timeout, c->parentchannel->timeout);
1973 if (c->timeout < 1)
1974 c->timeout = 1;
1975
1976 /*
1977 * OSSv4 docs: "By default OSS will set the low water level equal
1978 * to the fragment size which is optimal in most cases."
1979 */
1980 c->lw = bs->blksz;
1981 chn_resetbuf(c);
1982
1983 if (snd_verbose > 3)
1984 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1985 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1986 __func__, CHN_DIRSTR(c),
1987 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1988 c->timeout,
1989 b->bufsize, b->blksz,
1990 b->blkcnt,
1991 bs->bufsize, bs->blksz,
1992 bs->blkcnt, limit);
1993
1994 return 0;
1995 }
1996
1997 int
chn_setlatency(struct pcm_channel * c,int latency)1998 chn_setlatency(struct pcm_channel *c, int latency)
1999 {
2000 CHN_LOCKASSERT(c);
2001 /* Destroy blksz/blkcnt, enforce latency profile. */
2002 return chn_resizebuf(c, latency, -1, 0);
2003 }
2004
2005 int
chn_setblocksize(struct pcm_channel * c,int blkcnt,int blksz)2006 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
2007 {
2008 CHN_LOCKASSERT(c);
2009 /* Destroy latency profile, enforce blksz/blkcnt */
2010 return chn_resizebuf(c, -1, blkcnt, blksz);
2011 }
2012
2013 int
chn_setparam(struct pcm_channel * c,uint32_t format,uint32_t speed)2014 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
2015 {
2016 struct pcmchan_caps *caps;
2017 uint32_t hwspeed, delta;
2018 int ret;
2019
2020 CHN_LOCKASSERT(c);
2021
2022 if (speed < 1 || format == 0 || CHN_STARTED(c))
2023 return (EINVAL);
2024
2025 c->format = format;
2026 c->speed = speed;
2027
2028 caps = chn_getcaps(c);
2029
2030 hwspeed = speed;
2031 RANGE(hwspeed, caps->minspeed, caps->maxspeed);
2032
2033 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
2034 hwspeed));
2035 hwspeed = c->bufhard->spd;
2036
2037 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
2038
2039 if (delta <= feeder_rate_round)
2040 c->speed = hwspeed;
2041
2042 ret = feeder_chain(c);
2043
2044 if (ret == 0)
2045 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo, c->bufhard->fmt);
2046
2047 if (ret == 0)
2048 ret = chn_resizebuf(c, -2, 0, 0);
2049
2050 return (ret);
2051 }
2052
2053 int
chn_setspeed(struct pcm_channel * c,uint32_t speed)2054 chn_setspeed(struct pcm_channel *c, uint32_t speed)
2055 {
2056 uint32_t oldformat, oldspeed;
2057 int ret;
2058
2059 oldformat = c->format;
2060 oldspeed = c->speed;
2061
2062 if (c->speed == speed)
2063 return (0);
2064
2065 ret = chn_setparam(c, c->format, speed);
2066 if (ret != 0) {
2067 if (snd_verbose > 3)
2068 device_printf(c->dev,
2069 "%s(): Setting speed %d failed, "
2070 "falling back to %d\n",
2071 __func__, speed, oldspeed);
2072 chn_setparam(c, oldformat, oldspeed);
2073 }
2074
2075 return (ret);
2076 }
2077
2078 int
chn_setformat(struct pcm_channel * c,uint32_t format)2079 chn_setformat(struct pcm_channel *c, uint32_t format)
2080 {
2081 uint32_t oldformat, oldspeed, x;
2082 int ret;
2083
2084 /*
2085 * Detect unsupported formats. This checks if the format is supported
2086 * in the first place, and that no more than 1 format is specified at
2087 * the same time.
2088 */
2089 x = format & AFMT_CONVERTIBLE;
2090 if ((x & (x - 1)) != 0) {
2091 if (snd_verbose > 3) {
2092 device_printf(c->dev, "%s(): Unsupported format: "
2093 "0x%08x\n", __func__, format);
2094 }
2095 return (EINVAL);
2096 }
2097
2098 /* XXX force stereo */
2099 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
2100 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
2101 AFMT_PASSTHROUGH_EXTCHANNEL);
2102 }
2103
2104 oldformat = c->format;
2105 oldspeed = c->speed;
2106
2107 if (c->format == format)
2108 return (0);
2109
2110 ret = chn_setparam(c, format, c->speed);
2111 if (ret != 0) {
2112 if (snd_verbose > 3)
2113 device_printf(c->dev,
2114 "%s(): Format change 0x%08x failed, "
2115 "falling back to 0x%08x\n",
2116 __func__, format, oldformat);
2117 chn_setparam(c, oldformat, oldspeed);
2118 }
2119
2120 return (ret);
2121 }
2122
2123 void
chn_syncstate(struct pcm_channel * c)2124 chn_syncstate(struct pcm_channel *c)
2125 {
2126 struct snddev_info *d;
2127 struct snd_mixer *m;
2128
2129 d = (c != NULL) ? c->parentsnddev : NULL;
2130 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2131 NULL;
2132
2133 if (d == NULL || m == NULL)
2134 return;
2135
2136 CHN_LOCKASSERT(c);
2137
2138 if (c->feederflags & (1 << FEEDER_VOLUME)) {
2139 uint32_t parent;
2140 int vol, pvol, left, right, center;
2141
2142 if (c->direction == PCMDIR_PLAY &&
2143 (d->flags & SD_F_SOFTPCMVOL)) {
2144 /* CHN_UNLOCK(c); */
2145 vol = mix_get(m, SOUND_MIXER_PCM);
2146 parent = mix_getparent(m, SOUND_MIXER_PCM);
2147 if (parent != SOUND_MIXER_NONE)
2148 pvol = mix_get(m, parent);
2149 else
2150 pvol = 100 | (100 << 8);
2151 /* CHN_LOCK(c); */
2152 } else {
2153 vol = 100 | (100 << 8);
2154 pvol = vol;
2155 }
2156
2157 if (vol == -1) {
2158 device_printf(c->dev,
2159 "Soft PCM Volume: Failed to read pcm "
2160 "default value\n");
2161 vol = 100 | (100 << 8);
2162 }
2163
2164 if (pvol == -1) {
2165 device_printf(c->dev,
2166 "Soft PCM Volume: Failed to read parent "
2167 "default value\n");
2168 pvol = 100 | (100 << 8);
2169 }
2170
2171 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2172 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2173 center = (left + right) >> 1;
2174
2175 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2176 }
2177
2178 if (c->feederflags & (1 << FEEDER_EQ)) {
2179 struct pcm_feeder *f;
2180 int treble, bass, state;
2181
2182 /* CHN_UNLOCK(c); */
2183 treble = mix_get(m, SOUND_MIXER_TREBLE);
2184 bass = mix_get(m, SOUND_MIXER_BASS);
2185 /* CHN_LOCK(c); */
2186
2187 if (treble == -1)
2188 treble = 50;
2189 else
2190 treble = ((treble & 0x7f) +
2191 ((treble >> 8) & 0x7f)) >> 1;
2192
2193 if (bass == -1)
2194 bass = 50;
2195 else
2196 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2197
2198 f = feeder_find(c, FEEDER_EQ);
2199 if (f != NULL) {
2200 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2201 device_printf(c->dev,
2202 "EQ: Failed to set treble -- %d\n",
2203 treble);
2204 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2205 device_printf(c->dev,
2206 "EQ: Failed to set bass -- %d\n",
2207 bass);
2208 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2209 device_printf(c->dev,
2210 "EQ: Failed to set preamp -- %d\n",
2211 d->eqpreamp);
2212 if (d->flags & SD_F_EQ_BYPASSED)
2213 state = FEEDEQ_BYPASS;
2214 else if (d->flags & SD_F_EQ_ENABLED)
2215 state = FEEDEQ_ENABLE;
2216 else
2217 state = FEEDEQ_DISABLE;
2218 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2219 device_printf(c->dev,
2220 "EQ: Failed to set state -- %d\n", state);
2221 }
2222 }
2223 }
2224
2225 int
chn_trigger(struct pcm_channel * c,int go)2226 chn_trigger(struct pcm_channel *c, int go)
2227 {
2228 struct snddev_info *d = c->parentsnddev;
2229 int ret;
2230
2231 CHN_LOCKASSERT(c);
2232 if (!PCMTRIG_COMMON(go))
2233 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2234
2235 if (go == c->trigger)
2236 return (0);
2237
2238 if (snd_verbose > 3) {
2239 device_printf(c->dev, "%s() %s: calling go=0x%08x , "
2240 "prev=0x%08x\n", __func__, c->name, go, c->trigger);
2241 }
2242
2243 c->trigger = go;
2244 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2245 if (ret != 0)
2246 return (ret);
2247
2248 CHN_UNLOCK(c);
2249 PCM_LOCK(d);
2250 CHN_LOCK(c);
2251
2252 /*
2253 * Do nothing if another thread set a different trigger while we had
2254 * dropped the mutex.
2255 */
2256 if (go != c->trigger) {
2257 PCM_UNLOCK(d);
2258 return (0);
2259 }
2260
2261 /*
2262 * Use the SAFE variants to prevent inserting/removing an already
2263 * existing/missing element.
2264 */
2265 switch (go) {
2266 case PCMTRIG_START:
2267 CHN_INSERT_HEAD_SAFE(d, c, channels.pcm.busy);
2268 PCM_UNLOCK(d);
2269 chn_syncstate(c);
2270 break;
2271 case PCMTRIG_STOP:
2272 case PCMTRIG_ABORT:
2273 CHN_REMOVE(d, c, channels.pcm.busy);
2274 PCM_UNLOCK(d);
2275 break;
2276 default:
2277 PCM_UNLOCK(d);
2278 break;
2279 }
2280
2281 return (0);
2282 }
2283
2284 /**
2285 * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2286 *
2287 * This function obtains the hardware pointer location, then aligns it to
2288 * the current bytes-per-sample value before returning. (E.g., a channel
2289 * running in 16 bit stereo mode would require 4 bytes per sample, so a
2290 * hwptr value ranging from 32-35 would be returned as 32.)
2291 *
2292 * @param c PCM channel context
2293 * @returns sample-aligned hardware buffer pointer index
2294 */
2295 int
chn_getptr(struct pcm_channel * c)2296 chn_getptr(struct pcm_channel *c)
2297 {
2298 int hwptr;
2299
2300 CHN_LOCKASSERT(c);
2301 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2302 return (hwptr - (hwptr % c->bufhard->align));
2303 }
2304
2305 struct pcmchan_caps *
chn_getcaps(struct pcm_channel * c)2306 chn_getcaps(struct pcm_channel *c)
2307 {
2308 CHN_LOCKASSERT(c);
2309 return CHANNEL_GETCAPS(c->methods, c->devinfo);
2310 }
2311
2312 u_int32_t
chn_getformats(struct pcm_channel * c)2313 chn_getformats(struct pcm_channel *c)
2314 {
2315 u_int32_t *fmtlist, fmts;
2316 int i;
2317
2318 fmtlist = chn_getcaps(c)->fmtlist;
2319 fmts = 0;
2320 for (i = 0; fmtlist[i]; i++)
2321 fmts |= fmtlist[i];
2322
2323 /* report software-supported formats */
2324 if (!CHN_BITPERFECT(c) && report_soft_formats)
2325 fmts |= AFMT_CONVERTIBLE;
2326
2327 return (AFMT_ENCODING(fmts));
2328 }
2329
2330 int
chn_notify(struct pcm_channel * c,u_int32_t flags)2331 chn_notify(struct pcm_channel *c, u_int32_t flags)
2332 {
2333 struct pcm_channel *ch;
2334 struct pcmchan_caps *caps;
2335 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2336 uint32_t vpflags;
2337 int dirty, err, run, nrun;
2338
2339 CHN_LOCKASSERT(c);
2340
2341 if (CHN_EMPTY(c, children))
2342 return (0);
2343
2344 err = 0;
2345
2346 /*
2347 * If the hwchan is running, we can't change its rate, format or
2348 * blocksize
2349 */
2350 run = CHN_STARTED(c);
2351 if (run)
2352 flags &= CHN_N_TRIGGER;
2353
2354 if (flags & CHN_N_BLOCKSIZE) {
2355 /*
2356 * Set to default latency profile
2357 */
2358 chn_setlatency(c, chn_latency);
2359 }
2360
2361 if (flags & CHN_N_TRIGGER) {
2362 if (!(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2363 nrun = !CHN_EMPTY(c, children.busy);
2364 if (nrun && !run)
2365 err = chn_start(c, 1);
2366 if (!nrun && run)
2367 chn_abort(c);
2368
2369 return (err);
2370 }
2371
2372 if (c->direction == PCMDIR_PLAY) {
2373 vchanformat = &c->parentsnddev->pvchanformat;
2374 vchanrate = &c->parentsnddev->pvchanrate;
2375 } else {
2376 vchanformat = &c->parentsnddev->rvchanformat;
2377 vchanrate = &c->parentsnddev->rvchanrate;
2378 }
2379
2380 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2381 bestformat = *vchanformat;
2382 bestspeed = *vchanrate;
2383 } else {
2384 bestformat = 0;
2385 bestspeed = 0;
2386 }
2387
2388 besthwformat = 0;
2389 nrun = 0;
2390 caps = chn_getcaps(c);
2391 dirty = 0;
2392 vpflags = 0;
2393
2394 CHN_FOREACH(ch, c, children.busy) {
2395 nrun++;
2396 CHN_LOCK(ch);
2397 if ((ch->format & AFMT_PASSTHROUGH) &&
2398 snd_fmtvalid(ch->format, caps->fmtlist)) {
2399 bestformat = ch->format;
2400 bestspeed = ch->speed;
2401 CHN_UNLOCK(ch);
2402 vpflags = CHN_F_PASSTHROUGH;
2403 break;
2404 }
2405 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2406 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2407 bestspeed = ch->speed;
2408 RANGE(bestspeed, caps->minspeed,
2409 caps->maxspeed);
2410 besthwformat = snd_fmtbest(ch->format,
2411 caps->fmtlist);
2412 if (besthwformat != 0)
2413 bestformat = besthwformat;
2414 }
2415 CHN_UNLOCK(ch);
2416 vpflags = CHN_F_EXCLUSIVE;
2417 continue;
2418 }
2419 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2420 vpflags != 0) {
2421 CHN_UNLOCK(ch);
2422 continue;
2423 }
2424 if (ch->speed > bestspeed) {
2425 bestspeed = ch->speed;
2426 RANGE(bestspeed, caps->minspeed,
2427 caps->maxspeed);
2428 }
2429 besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2430 if (!(besthwformat & AFMT_VCHAN)) {
2431 CHN_UNLOCK(ch);
2432 continue;
2433 }
2434 if (AFMT_CHANNEL(besthwformat) >
2435 AFMT_CHANNEL(bestformat))
2436 bestformat = besthwformat;
2437 else if (AFMT_CHANNEL(besthwformat) ==
2438 AFMT_CHANNEL(bestformat) &&
2439 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2440 bestformat = besthwformat;
2441 CHN_UNLOCK(ch);
2442 }
2443
2444 if (bestformat == 0)
2445 bestformat = c->format;
2446 if (bestspeed == 0)
2447 bestspeed = c->speed;
2448
2449 if (bestformat != c->format || bestspeed != c->speed)
2450 dirty = 1;
2451
2452 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2453 c->flags |= vpflags;
2454
2455 if (nrun && !run) {
2456 if (dirty) {
2457 bestspeed = CHANNEL_SETSPEED(c->methods,
2458 c->devinfo, bestspeed);
2459 err = chn_reset(c, bestformat, bestspeed);
2460 }
2461 if (err == 0) {
2462 if (dirty) {
2463 CHN_FOREACH(ch, c, children.busy) {
2464 CHN_LOCK(ch);
2465 if (VCHAN_SYNC_REQUIRED(ch))
2466 vchan_sync(ch);
2467 CHN_UNLOCK(ch);
2468 }
2469 c->flags |= CHN_F_DIRTY;
2470 }
2471 err = chn_start(c, 1);
2472 }
2473 }
2474
2475 if (nrun && run && dirty) {
2476 chn_abort(c);
2477 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2478 bestspeed);
2479 err = chn_reset(c, bestformat, bestspeed);
2480 if (err == 0) {
2481 CHN_FOREACH(ch, c, children.busy) {
2482 CHN_LOCK(ch);
2483 if (VCHAN_SYNC_REQUIRED(ch))
2484 vchan_sync(ch);
2485 CHN_UNLOCK(ch);
2486 }
2487 c->flags |= CHN_F_DIRTY;
2488 err = chn_start(c, 1);
2489 }
2490 }
2491
2492 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2493 (bestformat & AFMT_VCHAN)) {
2494 *vchanformat = bestformat;
2495 *vchanrate = bestspeed;
2496 }
2497
2498 if (!nrun && run) {
2499 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2500 bestformat = *vchanformat;
2501 bestspeed = *vchanrate;
2502 chn_abort(c);
2503 if (c->format != bestformat || c->speed != bestspeed)
2504 chn_reset(c, bestformat, bestspeed);
2505 }
2506 }
2507
2508 return (err);
2509 }
2510
2511 /**
2512 * @brief Fetch array of supported discrete sample rates
2513 *
2514 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for
2515 * detailed information.
2516 *
2517 * @note If the operation isn't supported, this function will just return 0
2518 * (no rates in the array), and *rates will be set to NULL. Callers
2519 * should examine rates @b only if this function returns non-zero.
2520 *
2521 * @param c pcm channel to examine
2522 * @param rates pointer to array of integers; rate table will be recorded here
2523 *
2524 * @return number of rates in the array pointed to be @c rates
2525 */
2526 int
chn_getrates(struct pcm_channel * c,int ** rates)2527 chn_getrates(struct pcm_channel *c, int **rates)
2528 {
2529 KASSERT(rates != NULL, ("rates is null"));
2530 CHN_LOCKASSERT(c);
2531 return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2532 }
2533
2534 /**
2535 * @brief Remove channel from a sync group, if there is one.
2536 *
2537 * This function is initially intended for the following conditions:
2538 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2539 * - Closing a device. (A channel can't be destroyed if it's still in use.)
2540 *
2541 * @note Before calling this function, the syncgroup list mutex must be
2542 * held. (Consider pcm_channel::sm protected by the SG list mutex
2543 * whether @c c is locked or not.)
2544 *
2545 * @param c channel device to be started or closed
2546 * @returns If this channel was the only member of a group, the group ID
2547 * is returned to the caller so that the caller can release it
2548 * via free_unr() after giving up the syncgroup lock. Else it
2549 * returns 0.
2550 */
2551 int
chn_syncdestroy(struct pcm_channel * c)2552 chn_syncdestroy(struct pcm_channel *c)
2553 {
2554 struct pcmchan_syncmember *sm;
2555 struct pcmchan_syncgroup *sg;
2556 int sg_id;
2557
2558 sg_id = 0;
2559
2560 PCM_SG_LOCKASSERT(MA_OWNED);
2561
2562 if (c->sm != NULL) {
2563 sm = c->sm;
2564 sg = sm->parent;
2565 c->sm = NULL;
2566
2567 KASSERT(sg != NULL, ("syncmember has null parent"));
2568
2569 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2570 free(sm, M_DEVBUF);
2571
2572 if (SLIST_EMPTY(&sg->members)) {
2573 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2574 sg_id = sg->id;
2575 free(sg, M_DEVBUF);
2576 }
2577 }
2578
2579 return sg_id;
2580 }
2581
2582 #ifdef OSSV4_EXPERIMENT
2583 int
chn_getpeaks(struct pcm_channel * c,int * lpeak,int * rpeak)2584 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2585 {
2586 CHN_LOCKASSERT(c);
2587 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);
2588 }
2589 #endif
2590