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