2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * Portions Copyright by Luigi Rizzo - 1997-99
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/dev/sound/pcm/channel.c,v 1.99.2.5 2007/05/13 20:53:39 ariff Exp $
28 * $DragonFly: src/sys/dev/sound/pcm/channel.c,v 1.14 2007/06/16 20:07:22 dillon Exp $
33 #include <dev/sound/pcm/sound.h>
34 #include <sys/vnode.h> /* IO_NDELAY */
36 #include "feeder_if.h"
38 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/channel.c,v 1.14 2007/06/16 20:07:22 dillon Exp $");
40 #define MIN_CHUNK_SIZE 256 /* for uiomove etc. */
42 #define DMA_ALIGN_THRESHOLD 4
43 #define DMA_ALIGN_MASK (~(DMA_ALIGN_THRESHOLD - 1))
46 #define CANCHANGE(c) (!(c->flags & CHN_F_TRIGGERED))
52 static int chn_targetirqrate = 32;
53 TUNABLE_INT("hw.snd.targetirqrate", &chn_targetirqrate);
56 sysctl_hw_snd_targetirqrate(SYSCTL_HANDLER_ARGS)
60 val = chn_targetirqrate;
61 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
62 if (val < 16 || val > 512)
65 chn_targetirqrate = val;
69 SYSCTL_PROC(_hw_snd, OID_AUTO, targetirqrate, CTLTYPE_INT | CTLFLAG_RW,
70 0, sizeof(int), sysctl_hw_snd_targetirqrate, "I", "");
71 static int report_soft_formats = 1;
72 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
73 &report_soft_formats, 1, "report software-emulated formats");
75 static int chn_buildfeeder(struct pcm_channel *c);
78 chn_lockinit(struct pcm_channel *c, int dir)
82 c->lock = snd_mtxcreate(c->name, "pcm play channel");
85 c->lock = snd_mtxcreate(c->name, "pcm record channel");
88 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
91 c->lock = snd_mtxcreate(c->name, "pcm fake channel");
97 chn_lockdestroy(struct pcm_channel *c)
103 chn_polltrigger(struct pcm_channel *c)
105 struct snd_dbuf *bs = c->bufsoft;
109 if (c->flags & CHN_F_MAPPED) {
110 if (sndbuf_getprevblocks(bs) == 0)
113 return (sndbuf_getblocks(bs) > sndbuf_getprevblocks(bs))? 1 : 0;
115 amt = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
117 lim = (c->flags & CHN_F_HAS_SIZE)? sndbuf_getblksz(bs) : 1;
120 return (amt >= lim)? 1 : 0;
126 chn_pollreset(struct pcm_channel *c)
128 struct snd_dbuf *bs = c->bufsoft;
131 sndbuf_updateprevtotal(bs);
136 chn_wakeup(struct pcm_channel *c)
138 struct snd_dbuf *bs = c->bufsoft;
139 struct pcmchan_children *pce;
142 if (SLIST_EMPTY(&c->children)) {
143 /*if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))*/
144 if (sndbuf_getsel(bs)->si_pid && chn_polltrigger(c))
145 selwakeup(sndbuf_getsel(bs));
147 SLIST_FOREACH(pce, &c->children, link) {
148 CHN_LOCK(pce->channel);
149 chn_wakeup(pce->channel);
150 CHN_UNLOCK(pce->channel);
158 chn_sleep(struct pcm_channel *c, char *str, int timeout)
160 struct snd_dbuf *bs = c->bufsoft;
165 ret = snd_mtxsleep(bs, c->lock, PCATCH, str, timeout);
167 ret = tsleep(bs, PRIBIO | PCATCH, str, timeout);
174 * chn_dmaupdate() tracks the status of a dma transfer,
179 chn_dmaupdate(struct pcm_channel *c)
181 struct snd_dbuf *b = c->bufhard;
182 unsigned int delta, old, hwptr, amt;
184 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
187 old = sndbuf_gethwptr(b);
188 hwptr = chn_getptr(c);
189 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
190 sndbuf_sethwptr(b, hwptr);
193 if (delta >= ((sndbuf_getsize(b) * 15) / 16)) {
194 if (!(c->flags & (CHN_F_CLOSING | CHN_F_ABORTING)))
195 device_printf(c->dev, "hwptr went backwards %d -> %d\n", old, hwptr);
199 if (c->direction == PCMDIR_PLAY) {
200 amt = MIN(delta, sndbuf_getready(b));
202 sndbuf_dispose(b, NULL, amt);
204 amt = MIN(delta, sndbuf_getfree(b));
206 sndbuf_acquire(b, NULL, amt);
213 chn_wrupdate(struct pcm_channel *c)
218 KASSERT(c->direction == PCMDIR_PLAY, ("chn_wrupdate on bad channel"));
220 if ((c->flags & (CHN_F_MAPPED | CHN_F_VIRTUAL)) || !(c->flags & CHN_F_TRIGGERED))
224 /* tell the driver we've updated the primary buffer */
225 chn_trigger(c, PCMTRIG_EMLDMAWR);
227 kprintf("chn_wrupdate: chn_wrfeed returned %d\n", ret);)
232 chn_wrfeed(struct pcm_channel *c)
234 struct snd_dbuf *b = c->bufhard;
235 struct snd_dbuf *bs = c->bufsoft;
236 unsigned int ret, amt;
241 if (c->flags & CHN_F_CLOSING) {
242 sndbuf_dump(b, "b", 0x02);
243 sndbuf_dump(bs, "bs", 0x02);
247 if (c->flags & CHN_F_MAPPED)
248 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
250 amt = sndbuf_getfree(b);
251 KASSERT(amt <= sndbuf_getsize(bs),
252 ("%s(%s): amt %d > source size %d, flags 0x%x", __func__, c->name,
253 amt, sndbuf_getsize(bs), c->flags));
255 ret = (amt > 0) ? sndbuf_feed(bs, b, c, c->feeder, amt) : ENOSPC;
257 * Possible xruns. There should be no empty space left in buffer.
259 if (sndbuf_getfree(b) > 0)
262 if (ret == 0 && sndbuf_getfree(b) < amt)
269 chn_wrintr(struct pcm_channel *c)
274 /* update pointers in primary buffer */
276 /* ...and feed from secondary to primary */
278 /* tell the driver we've updated the primary buffer */
279 chn_trigger(c, PCMTRIG_EMLDMAWR);
281 kprintf("chn_wrintr: chn_wrfeed returned %d\n", ret);)
285 * user write routine - uiomove data into secondary buffer, trigger if necessary
286 * if blocking, sleep, rinse and repeat.
288 * called externally, so must handle locking
292 chn_write(struct pcm_channel *c, struct uio *buf, int ioflags)
294 int ret, timeout, newsize, count, sz;
296 struct snd_dbuf *bs = c->bufsoft;
302 * XXX Certain applications attempt to write larger size
303 * of pcm data than c->blocksize2nd without blocking,
304 * resulting partial write. Expand the block size so that
305 * the write operation avoids blocking.
307 nbio = (c->flags & CHN_F_NBIO) || (ioflags & IO_NDELAY);
308 if (nbio && buf->uio_resid > sndbuf_getblksz(bs)) {
309 DEB(device_printf(c->dev, "broken app, nbio and tried to write %d bytes with fragsz %d\n",
310 buf->uio_resid, sndbuf_getblksz(bs)));
312 while (newsize < min(buf->uio_resid, CHN_2NDBUFMAXSIZE / 2))
314 chn_setblocksize(c, sndbuf_getblkcnt(bs), newsize);
315 DEB(device_printf(c->dev, "frags reset to %d x %d\n", sndbuf_getblkcnt(bs), sndbuf_getblksz(bs)));
320 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
321 sz = sndbuf_getfree(bs);
326 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
330 ret = chn_sleep(c, "pcmwr", timeout);
331 if (ret == EWOULDBLOCK) {
338 sz = MIN(sz, buf->uio_resid);
339 KASSERT(sz > 0, ("confusion in chn_write"));
340 /* kprintf("sz: %d\n", sz); */
343 * The following assumes that the free space in
344 * the buffer can never be less around the
345 * unlock-uiomove-lock sequence.
348 while (ret == 0 && togo> 0) {
349 p = sndbuf_getfreeptr(bs);
350 t = MIN(togo, sndbuf_getsize(bs) - p);
351 off = sndbuf_getbufofs(bs, p);
353 ret = uiomove(off, t, buf);
356 x = sndbuf_acquire(bs, NULL, t);
359 if (ret == 0 && !(c->flags & CHN_F_TRIGGERED))
363 /* kprintf("ret: %d left: %d\n", ret, buf->uio_resid); */
366 c->flags |= CHN_F_DEAD;
367 kprintf("%s: play interrupt timeout, channel dead\n", c->name);
375 chn_rddump(struct pcm_channel *c, unsigned int cnt)
377 struct snd_dbuf *b = c->bufhard;
381 static uint32_t kk = 0;
382 printf("%u: dumping %d bytes\n", ++kk, cnt);
385 sndbuf_setxrun(b, sndbuf_getxrun(b) + cnt);
386 return sndbuf_dispose(b, NULL, cnt);
391 * Feed new data from the read buffer. Can be called in the bottom half.
394 chn_rdfeed(struct pcm_channel *c)
396 struct snd_dbuf *b = c->bufhard;
397 struct snd_dbuf *bs = c->bufsoft;
398 unsigned int ret, amt;
402 if (c->flags & CHN_F_CLOSING) {
403 sndbuf_dump(b, "b", 0x02);
404 sndbuf_dump(bs, "bs", 0x02);
408 amt = sndbuf_getready(b);
409 if (sndbuf_getfree(bs) < amt) {
411 amt = sndbuf_getfree(bs);
414 amt = sndbuf_getfree(bs);
415 ret = (amt > 0)? sndbuf_feed(b, bs, c, c->feeder, amt) : 0;
417 amt = sndbuf_getready(b);
420 sndbuf_dispose(b, NULL, amt);
429 chn_rdupdate(struct pcm_channel *c)
434 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
436 if ((c->flags & CHN_F_MAPPED) || !(c->flags & CHN_F_TRIGGERED))
438 chn_trigger(c, PCMTRIG_EMLDMARD);
442 kprintf("chn_rdfeed: %d\n", ret);)
445 /* read interrupt routine. Must be called with interrupts blocked. */
447 chn_rdintr(struct pcm_channel *c)
452 /* tell the driver to update the primary buffer if non-dma */
453 chn_trigger(c, PCMTRIG_EMLDMARD);
454 /* update pointers in primary buffer */
456 /* ...and feed from primary to secondary */
461 * user read routine - trigger if necessary, uiomove data from secondary buffer
462 * if blocking, sleep, rinse and repeat.
464 * called externally, so must handle locking
468 chn_read(struct pcm_channel *c, struct uio *buf, int ioflags)
470 int ret, timeout, sz, count;
472 struct snd_dbuf *bs = c->bufsoft;
477 nbio = (c->flags & CHN_F_NBIO) || (ioflags & IO_NDELAY);
478 if (!(c->flags & CHN_F_TRIGGERED))
483 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
484 sz = MIN(buf->uio_resid, sndbuf_getready(bs));
488 * The following assumes that the free space in
489 * the buffer can never be less around the
490 * unlock-uiomove-lock sequence.
493 while (ret == 0 && togo> 0) {
494 p = sndbuf_getreadyptr(bs);
495 t = MIN(togo, sndbuf_getsize(bs) - p);
496 off = sndbuf_getbufofs(bs, p);
498 ret = uiomove(off, t, buf);
501 x = sndbuf_dispose(bs, NULL, t);
508 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
511 ret = chn_sleep(c, "pcmrd", timeout);
512 if (ret == EWOULDBLOCK) {
524 c->flags |= CHN_F_DEAD;
525 kprintf("%s: record interrupt timeout, channel dead\n", c->name);
532 chn_intr(struct pcm_channel *c)
536 if (c->direction == PCMDIR_PLAY)
544 chn_start(struct pcm_channel *c, int force)
547 struct snd_dbuf *b = c->bufhard;
548 struct snd_dbuf *bs = c->bufsoft;
551 /* if we're running, or if we're prevented from triggering, bail */
552 if ((c->flags & CHN_F_TRIGGERED) || ((c->flags & CHN_F_NOTRIGGER) && !force))
555 i = (c->direction == PCMDIR_PLAY)? sndbuf_getready(bs) : sndbuf_getfree(bs);
556 j = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(b) : sndbuf_getready(b);
557 if (force || (i >= j)) {
558 c->flags |= CHN_F_TRIGGERED;
560 * if we're starting because a vchan started, don't feed any data
561 * or it becomes impossible to start vchans synchronised with the
562 * first one. the hardbuf should be empty so we top it up with
563 * silence to give it something to chew. the real data will be
564 * fed at the first irq.
566 if (c->direction == PCMDIR_PLAY) {
568 * Reduce pops during playback startup.
570 sndbuf_fillsilence(b);
571 if (SLIST_EMPTY(&c->children))
576 chn_trigger(c, PCMTRIG_START);
584 chn_resetbuf(struct pcm_channel *c)
586 struct snd_dbuf *b = c->bufhard;
587 struct snd_dbuf *bs = c->bufsoft;
595 * chn_sync waits until the space in the given channel goes above
596 * a threshold. The threshold is checked against fl or rl respectively.
597 * Assume that the condition can become true, do not check here...
600 chn_sync(struct pcm_channel *c, int threshold)
604 struct snd_dbuf *bs = c->bufsoft;
608 /* if we haven't yet started and nothing is buffered, else start*/
609 if (!(c->flags & CHN_F_TRIGGERED)) {
610 if (sndbuf_getready(bs) > 0) {
611 ret = chn_start(c, 1);
620 rdy = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
621 if (rdy <= threshold) {
622 ret = chn_sleep(c, "pcmsyn", 1);
623 if (ret == ERESTART || ret == EINTR) {
624 DEB(kprintf("chn_sync: tsleep returns %d\n", ret));
633 /* called externally, handle locking */
635 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
637 struct snd_dbuf *bs = c->bufsoft;
641 if (!(c->flags & CHN_F_MAPPED) && !(c->flags & CHN_F_TRIGGERED))
644 if (chn_polltrigger(c) && chn_pollreset(c))
647 selrecord(td, sndbuf_getsel(bs));
652 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
653 * it returns the number of bytes that have not been transferred.
655 * called from: dsp_close, dsp_ioctl, with channel locked
658 chn_abort(struct pcm_channel *c)
661 struct snd_dbuf *b = c->bufhard;
662 struct snd_dbuf *bs = c->bufsoft;
665 if (!(c->flags & CHN_F_TRIGGERED))
667 c->flags |= CHN_F_ABORTING;
669 c->flags &= ~CHN_F_TRIGGERED;
670 /* kill the channel */
671 chn_trigger(c, PCMTRIG_ABORT);
673 if (!(c->flags & CHN_F_VIRTUAL))
675 missing = sndbuf_getready(bs) + sndbuf_getready(b);
677 c->flags &= ~CHN_F_ABORTING;
682 * this routine tries to flush the dma transfer. It is called
683 * on a close of a playback channel.
684 * first, if there is data in the buffer, but the dma has not yet
685 * begun, we need to start it.
686 * next, we wait for the play buffer to drain
687 * finally, we stop the dma.
689 * called from: dsp_close, not valid for record channels.
693 chn_flush(struct pcm_channel *c)
695 int ret, count, resid, resid_p;
696 struct snd_dbuf *b = c->bufhard;
697 struct snd_dbuf *bs = c->bufsoft;
700 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
701 DEB(kprintf("chn_flush: c->flags 0x%08x\n", c->flags));
703 /* if we haven't yet started and nothing is buffered, else start*/
704 if (!(c->flags & CHN_F_TRIGGERED)) {
705 if (sndbuf_getready(bs) > 0) {
706 ret = chn_start(c, 1);
714 c->flags |= CHN_F_CLOSING;
715 resid = sndbuf_getready(bs) + sndbuf_getready(b);
719 while ((count > 0) && (resid > sndbuf_getsize(b)) && (ret == 0)) {
720 /* still pending output data. */
721 ret = chn_sleep(c, "pcmflu", hz / 10);
722 if (ret == EWOULDBLOCK)
725 resid = sndbuf_getready(bs) + sndbuf_getready(b);
726 if (resid == resid_p)
729 DEB(printf("chn_flush: buffer length increasind %d -> %d\n", resid_p, resid));
734 DEB(kprintf("chn_flush: timeout, hw %d, sw %d\n",
735 sndbuf_getready(b), sndbuf_getready(bs)));
737 c->flags &= ~CHN_F_TRIGGERED;
738 /* kill the channel */
739 chn_trigger(c, PCMTRIG_ABORT);
742 c->flags &= ~CHN_F_CLOSING;
747 fmtvalid(u_int32_t fmt, u_int32_t *fmtlist)
751 for (i = 0; fmtlist[i]; i++)
752 if (fmt == fmtlist[i])
758 chn_reset(struct pcm_channel *c, u_int32_t fmt)
763 c->flags &= CHN_F_RESET;
767 r = CHANNEL_RESET(c->methods, c->devinfo);
770 hwspd = DSP_DEFAULT_SPEED;
771 /* only do this on a record channel until feederbuilder works */
772 if (c->direction == PCMDIR_REC)
773 RANGE(hwspd, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
776 hwspd = chn_getcaps(c)->minspeed;
780 r = chn_setformat(c, fmt);
782 r = chn_setspeed(c, hwspd);
785 r = chn_setvolume(c, 100, 100);
789 r = chn_setblocksize(c, 0, 0);
792 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
798 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
800 struct feeder_class *fc;
801 struct snd_dbuf *b, *bs;
804 chn_lockinit(c, dir);
812 b = sndbuf_create(c->dev, c->name, "primary", c);
815 bs = sndbuf_create(c->dev, c->name, "secondary", c);
822 fc = feeder_getclass(NULL);
825 if (chn_addfeeder(c, fc, NULL))
829 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
830 * with the channel unlocked because they are also called
831 * from driver methods that don't know about locking
834 sndbuf_setup(bs, NULL, 0);
842 CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() kmalloc() call */
843 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
845 if (c->devinfo == NULL)
849 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
852 ret = chn_setdir(c, direction);
856 ret = sndbuf_setfmt(b, AFMT_U8);
860 ret = sndbuf_setfmt(bs, AFMT_U8);
864 ret = chn_setvolume(c, 100, 100);
873 if (CHANNEL_FREE(c->methods, c->devinfo))
880 c->flags |= CHN_F_DEAD;
890 chn_kill(struct pcm_channel *c)
892 struct snd_dbuf *b = c->bufhard;
893 struct snd_dbuf *bs = c->bufsoft;
895 if (c->flags & CHN_F_TRIGGERED)
896 chn_trigger(c, PCMTRIG_ABORT);
897 while (chn_removefeeder(c) == 0);
898 if (CHANNEL_FREE(c->methods, c->devinfo))
900 c->flags |= CHN_F_DEAD;
908 chn_setdir(struct pcm_channel *c, int dir)
911 struct snd_dbuf *b = c->bufhard;
917 r = CHANNEL_SETDIR(c->methods, c->devinfo, c->direction);
919 if (!r && SND_DMA(b))
920 sndbuf_dmasetdir(b, c->direction);
926 chn_setvolume(struct pcm_channel *c, int left, int right)
929 /* should add a feeder for volume changing if channel returns -1 */
938 c->volume = left | (right << 8);
943 chn_tryspeed(struct pcm_channel *c, int speed)
945 struct pcm_feeder *f;
946 struct snd_dbuf *b = c->bufhard;
947 struct snd_dbuf *bs = c->bufsoft;
952 DEB(kprintf("setspeed, channel %s\n", c->name));
953 DEB(kprintf("want speed %d, ", speed));
959 sndbuf_setspd(bs, speed);
960 RANGE(speed, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
961 DEB(kprintf("try speed %d, ", speed));
962 sndbuf_setspd(b, CHANNEL_SETSPEED(c->methods, c->devinfo, speed));
963 DEB(kprintf("got speed %d\n", sndbuf_getspd(b)));
965 delta = sndbuf_getspd(b) - sndbuf_getspd(bs);
969 c->feederflags &= ~(1 << FEEDER_RATE);
971 * Used to be 500. It was too big!
974 c->feederflags |= 1 << FEEDER_RATE;
976 sndbuf_setspd(bs, sndbuf_getspd(b));
978 r = chn_buildfeeder(c);
979 DEB(kprintf("r = %d\n", r));
983 r = chn_setblocksize(c, 0, 0);
987 if (!(c->feederflags & (1 << FEEDER_RATE)))
991 f = chn_findfeeder(c, FEEDER_RATE);
992 DEB(kprintf("feedrate = %p\n", f));
996 x = (c->direction == PCMDIR_REC)? b : bs;
997 r = FEEDER_SET(f, FEEDRATE_SRC, sndbuf_getspd(x));
998 DEB(kprintf("feeder_set(FEEDRATE_SRC, %d) = %d\n", sndbuf_getspd(x), r));
1002 x = (c->direction == PCMDIR_REC)? bs : b;
1003 r = FEEDER_SET(f, FEEDRATE_DST, sndbuf_getspd(x));
1004 DEB(kprintf("feeder_set(FEEDRATE_DST, %d) = %d\n", sndbuf_getspd(x), r));
1007 r = CHANNEL_SETFORMAT(c->methods, c->devinfo,
1010 sndbuf_setfmt(bs, c->format);
1011 DEB(kprintf("setspeed done, r = %d\n", r));
1018 chn_setspeed(struct pcm_channel *c, int speed)
1020 int r, oldspeed = c->speed;
1022 r = chn_tryspeed(c, speed);
1024 DEB(kprintf("Failed to set speed %d falling back to %d\n", speed, oldspeed));
1025 r = chn_tryspeed(c, oldspeed);
1031 chn_tryformat(struct pcm_channel *c, u_int32_t fmt)
1033 struct snd_dbuf *b = c->bufhard;
1034 struct snd_dbuf *bs = c->bufsoft;
1039 DEB(kprintf("want format %d\n", fmt));
1041 r = chn_buildfeeder(c);
1043 sndbuf_setfmt(bs, c->format);
1045 r = CHANNEL_SETFORMAT(c->methods, c->devinfo, sndbuf_getfmt(b));
1047 r = chn_tryspeed(c, c->speed);
1055 chn_setformat(struct pcm_channel *c, u_int32_t fmt)
1057 u_int32_t oldfmt = c->format;
1060 r = chn_tryformat(c, fmt);
1062 DEB(kprintf("Format change %d failed, reverting to %d\n", fmt, oldfmt));
1063 chn_tryformat(c, oldfmt);
1069 * given a bufsz value, round it to a power of 2 in the min-max range
1070 * XXX only works if min and max are powers of 2
1073 round_bufsz(int bufsz, int min, int max)
1077 KASSERT((min & (min-1)) == 0, ("min %d must be power of 2\n", min));
1078 KASSERT((max & (max-1)) == 0, ("max %d must be power of 2\n", max));
1079 while (tmp <= bufsz)
1088 * set the channel's blocksize both for soft and hard buffers.
1090 * blksz should be a power of 2 between 2**4 and 2**16 -- it is useful
1091 * that it has the same value for both bufsoft and bufhard.
1092 * blksz == -1 computes values according to a target irq rate.
1093 * blksz == 0 reuses previous values if available, otherwise
1096 * blkcnt is set by the user, between 2 and (2**17)/blksz for bufsoft,
1097 * but should be a power of 2 for bufhard to simplify life to low
1099 * Note, for the rec channel a large blkcnt is ok,
1100 * but for the play channel we want blksz as small as possible to keep
1101 * the delay small, because routines in the write path always try to
1102 * keep bufhard full.
1104 * Unless we have good reason to, use the values suggested by the caller.
1107 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1109 struct snd_dbuf *b = c->bufhard;
1110 struct snd_dbuf *bs = c->bufsoft;
1111 int irqhz, ret, maxsz, maxsize, reqblksz;
1114 if (!CANCHANGE(c) || (c->flags & CHN_F_MAPPED)) {
1115 KASSERT(sndbuf_getsize(bs) == 0 ||
1116 sndbuf_getsize(bs) >= sndbuf_getsize(b),
1117 ("%s(%s): bufsoft size %d < bufhard size %d", __func__,
1118 c->name, sndbuf_getsize(bs), sndbuf_getsize(b)));
1121 c->flags |= CHN_F_SETBLOCKSIZE;
1124 DEB(kprintf("%s(%d, %d)\n", __func__, blkcnt, blksz));
1125 if (blksz == 0 || blksz == -1) { /* let the driver choose values */
1126 if (blksz == -1) /* delete previous values */
1127 c->flags &= ~CHN_F_HAS_SIZE;
1128 if (!(c->flags & CHN_F_HAS_SIZE)) { /* no previous value */
1130 * compute a base blksz according to the target irq
1131 * rate, then round to a suitable power of 2
1132 * in the range 16.. 2^17/2.
1133 * Finally compute a suitable blkcnt.
1135 blksz = round_bufsz( (sndbuf_getbps(bs) *
1136 sndbuf_getspd(bs)) / chn_targetirqrate,
1137 16, CHN_2NDBUFMAXSIZE / 2);
1138 blkcnt = CHN_2NDBUFMAXSIZE / blksz;
1139 } else { /* use previously defined value */
1140 blkcnt = sndbuf_getblkcnt(bs);
1141 blksz = sndbuf_getblksz(bs);
1145 * use supplied values if reasonable. Note that here we
1146 * might have blksz which is not a power of 2 if the
1147 * ioctl() to compute it allows such values.
1150 if ((blksz < 16) || (blkcnt < 2) || (blkcnt * blksz > CHN_2NDBUFMAXSIZE))
1153 c->flags |= CHN_F_HAS_SIZE;
1157 if (reqblksz < sndbuf_getbps(bs))
1158 reqblksz = sndbuf_getbps(bs);
1159 if (reqblksz % sndbuf_getbps(bs))
1160 reqblksz -= reqblksz % sndbuf_getbps(bs);
1162 /* adjust for different hw format/speed */
1164 * Now compute the approx irq rate for the given (soft) blksz,
1165 * reduce to the acceptable range and compute a corresponding blksz
1166 * for the hard buffer. Then set the channel's blocksize and
1167 * corresponding hardbuf value. The number of blocks used should
1168 * be set by the device-specific routine. In fact, even the
1169 * call to sndbuf_setblksz() should not be here! XXX
1172 irqhz = (sndbuf_getbps(bs) * sndbuf_getspd(bs)) / blksz;
1173 RANGE(irqhz, 16, 512);
1175 maxsz = sndbuf_getmaxsize(b);
1176 if (maxsz == 0) /* virtual channels don't appear to allocate bufhard */
1177 maxsz = CHN_2NDBUFMAXSIZE;
1178 blksz = round_bufsz( (sndbuf_getbps(b) * sndbuf_getspd(b)) / irqhz,
1181 /* Increase the size of bufsoft if before increasing bufhard. */
1182 maxsize = sndbuf_getsize(b);
1183 if (sndbuf_getsize(bs) > maxsize)
1184 maxsize = sndbuf_getsize(bs);
1185 if (reqblksz * blkcnt > maxsize)
1186 maxsize = reqblksz * blkcnt;
1187 if (sndbuf_getsize(bs) != maxsize || sndbuf_getblksz(bs) != reqblksz) {
1188 ret = sndbuf_remalloc(bs, maxsize/reqblksz, reqblksz);
1194 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods, c->devinfo, blksz));
1197 /* Decrease the size of bufsoft after decreasing bufhard. */
1198 maxsize = sndbuf_getsize(b);
1199 if (reqblksz * blkcnt > maxsize)
1200 maxsize = reqblksz * blkcnt;
1201 if (maxsize > sndbuf_getsize(bs))
1202 kprintf("Danger! %s bufsoft size increasing from %d to %d after CHANNEL_SETBLOCKSIZE()\n",
1203 c->name, sndbuf_getsize(bs), maxsize);
1204 if (sndbuf_getsize(bs) != maxsize || sndbuf_getblksz(bs) != reqblksz) {
1205 ret = sndbuf_remalloc(bs, maxsize/reqblksz, reqblksz);
1212 KASSERT(sndbuf_getsize(bs) == 0 ||
1213 sndbuf_getsize(bs) >= sndbuf_getsize(b),
1214 ("%s(%s): bufsoft size %d < bufhard size %d, reqblksz=%d blksz=%d maxsize=%d blkcnt=%d",
1215 __func__, c->name, sndbuf_getsize(bs), sndbuf_getsize(b), reqblksz,
1216 blksz, maxsize, blkcnt));
1218 c->flags &= ~CHN_F_SETBLOCKSIZE;
1221 static uint32_t kk = 0;
1222 printf("%u: b %d/%d/%d : (%d)%d/0x%0x | bs %d/%d/%d : (%d)%d/0x%0x\n", ++kk,
1223 sndbuf_getsize(b), sndbuf_getblksz(b), sndbuf_getblkcnt(b),
1225 sndbuf_getspd(b), sndbuf_getfmt(b),
1226 sndbuf_getsize(bs), sndbuf_getblksz(bs), sndbuf_getblkcnt(bs),
1228 sndbuf_getspd(bs), sndbuf_getfmt(bs));
1229 if (sndbuf_getsize(b) % sndbuf_getbps(b) ||
1230 sndbuf_getblksz(b) % sndbuf_getbps(b) ||
1231 sndbuf_getsize(bs) % sndbuf_getbps(bs) ||
1232 sndbuf_getblksz(b) % sndbuf_getbps(b)) {
1233 printf("%u: bps/blksz alignment screwed!\n", kk);
1241 chn_trigger(struct pcm_channel *c, int go)
1244 struct snd_dbuf *b = c->bufhard;
1250 if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
1251 sndbuf_dmabounce(b);
1253 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
1259 chn_getptr(struct pcm_channel *c)
1263 int a = (1 << c->align) - 1;
1266 hwptr = (c->flags & CHN_F_TRIGGERED)? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
1267 /* don't allow unaligned values in the hwa ptr */
1269 hwptr &= ~a ; /* Apply channel align mask */
1271 hwptr &= DMA_ALIGN_MASK; /* Apply DMA align mask */
1277 hwptr = (c->flags & CHN_F_TRIGGERED)? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
1278 return (hwptr - (hwptr % sndbuf_getbps(c->bufhard)));
1281 struct pcmchan_caps *
1282 chn_getcaps(struct pcm_channel *c)
1285 return CHANNEL_GETCAPS(c->methods, c->devinfo);
1289 chn_getformats(struct pcm_channel *c)
1291 u_int32_t *fmtlist, fmts;
1294 fmtlist = chn_getcaps(c)->fmtlist;
1296 for (i = 0; fmtlist[i]; i++)
1299 /* report software-supported formats */
1300 if (report_soft_formats)
1301 fmts |= AFMT_MU_LAW|AFMT_A_LAW|AFMT_U32_LE|AFMT_U32_BE|
1302 AFMT_S32_LE|AFMT_S32_BE|AFMT_U24_LE|AFMT_U24_BE|
1303 AFMT_S24_LE|AFMT_S24_BE|AFMT_U16_LE|AFMT_U16_BE|
1304 AFMT_S16_LE|AFMT_S16_BE|AFMT_U8|AFMT_S8;
1310 chn_buildfeeder(struct pcm_channel *c)
1312 struct feeder_class *fc;
1313 struct pcm_feederdesc desc;
1314 u_int32_t tmp[2], type, flags, hwfmt, *fmtlist;
1318 while (chn_removefeeder(c) == 0);
1319 KASSERT((c->feeder == NULL), ("feeder chain not empty"));
1321 c->align = sndbuf_getalign(c->bufsoft);
1323 if (SLIST_EMPTY(&c->children)) {
1324 fc = feeder_getclass(NULL);
1325 KASSERT(fc != NULL, ("can't find root feeder"));
1327 err = chn_addfeeder(c, fc, NULL);
1329 DEB(kprintf("can't add root feeder, err %d\n", err));
1333 c->feeder->desc->out = c->format;
1335 if (c->flags & CHN_F_HAS_VCHAN) {
1336 desc.type = FEEDER_MIXER;
1339 DEB(printf("can't decide which feeder type to use!\n"));
1342 desc.out = c->format;
1344 fc = feeder_getclass(&desc);
1346 DEB(kprintf("can't find vchan feeder\n"));
1351 err = chn_addfeeder(c, fc, &desc);
1353 DEB(kprintf("can't add vchan feeder, err %d\n", err));
1358 c->feederflags &= ~(1 << FEEDER_VOLUME);
1359 if (c->direction == PCMDIR_PLAY &&
1360 !(c->flags & CHN_F_VIRTUAL) &&
1361 c->parentsnddev && (c->parentsnddev->flags & SD_F_SOFTPCMVOL) &&
1362 c->parentsnddev->mixer_dev)
1363 c->feederflags |= 1 << FEEDER_VOLUME;
1364 flags = c->feederflags;
1365 fmtlist = chn_getcaps(c)->fmtlist;
1367 DEB(kprintf("feederflags %x\n", flags));
1369 for (type = FEEDER_RATE; type <= FEEDER_LAST; type++) {
1370 if (flags & (1 << type)) {
1375 DEB(kprintf("find feeder type %d, ", type));
1376 fc = feeder_getclass(&desc);
1377 DEB(kprintf("got %p\n", fc));
1379 DEB(kprintf("can't find required feeder type %d\n", type));
1384 DEB(kprintf("build fmtchain from 0x%08x to 0x%08x: ", c->feeder->desc->out, fc->desc->in));
1385 tmp[0] = fc->desc->in;
1387 if (chn_fmtchain(c, tmp) == 0) {
1388 DEB(printf("failed\n"));
1392 DEB(printf("ok\n"));
1394 err = chn_addfeeder(c, fc, fc->desc);
1396 DEB(kprintf("can't add feeder %p, output 0x%x, err %d\n", fc, fc->desc->out, err));
1400 DEB(kprintf("added feeder %p, output 0x%x\n", fc, c->feeder->desc->out));
1404 if (c->direction == PCMDIR_REC) {
1407 hwfmt = chn_fmtchain(c, tmp);
1409 hwfmt = chn_fmtchain(c, fmtlist);
1411 if (hwfmt == 0 || !fmtvalid(hwfmt, fmtlist)) {
1412 DEB(printf("Invalid hardware format: 0x%08x\n", hwfmt));
1416 sndbuf_setfmt(c->bufhard, hwfmt);
1418 if ((flags & (1 << FEEDER_VOLUME))) {
1419 struct dev_ioctl_args map;
1420 u_int32_t parent = SOUND_MIXER_NONE;
1421 int vol, left, right;
1423 vol = 100 | (100 << 8);
1427 * XXX This is ugly! The way mixer subs being so secretive
1428 * about its own internals force us to use this silly
1431 map.a_head.a_dev = c->parentsnddev->mixer_dev;
1432 map.a_cmd = MIXER_READ(SOUND_MIXER_PCM);
1433 map.a_data = (caddr_t)&vol;
1436 if (mixer_ioctl(&map) != 0)
1437 device_printf(c->dev, "Soft PCM Volume: Failed to read default value\n");
1439 right = (vol >> 8) & 0x7f;
1440 if (c->parentsnddev != NULL &&
1441 c->parentsnddev->mixer_dev != NULL &&
1442 c->parentsnddev->mixer_dev->si_drv1 != NULL)
1443 parent = mix_getparent(
1444 c->parentsnddev->mixer_dev->si_drv1,
1446 if (parent != SOUND_MIXER_NONE) {
1447 vol = 100 | (100 << 8);
1448 map.a_head.a_dev = c->parentsnddev->mixer_dev;
1449 map.a_cmd = MIXER_READ(parent);
1450 map.a_data = (caddr_t)&vol;
1453 if (mixer_ioctl(&map) != 0)
1454 device_printf(c->dev, "Soft Volume: Failed to read parent default value\n");
1455 left = (left * (vol & 0x7f)) / 100;
1456 right = (right * ((vol >> 8) & 0x7f)) / 100;
1460 chn_setvolume(c, left, right);
1467 chn_notify(struct pcm_channel *c, u_int32_t flags)
1469 struct pcmchan_children *pce;
1470 struct pcm_channel *child;
1475 if (SLIST_EMPTY(&c->children)) {
1480 run = (c->flags & CHN_F_TRIGGERED)? 1 : 0;
1482 * if the hwchan is running, we can't change its rate, format or
1486 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
1488 if (flags & CHN_N_RATE) {
1490 * we could do something here, like scan children and decide on
1491 * the most appropriate rate to mix at, but we don't for now
1494 if (flags & CHN_N_FORMAT) {
1496 * we could do something here, like scan children and decide on
1497 * the most appropriate mixer feeder to use, but we don't for now
1500 if (flags & CHN_N_VOLUME) {
1502 * we could do something here but we don't for now
1505 if (flags & CHN_N_BLOCKSIZE) {
1508 * scan the children, find the lowest blocksize and use that
1509 * for the hard blocksize
1511 blksz = sndbuf_getmaxsize(c->bufhard) / 2;
1512 SLIST_FOREACH(pce, &c->children, link) {
1513 child = pce->channel;
1515 if (sndbuf_getblksz(child->bufhard) < blksz)
1516 blksz = sndbuf_getblksz(child->bufhard);
1519 chn_setblocksize(c, 2, blksz);
1521 if (flags & CHN_N_TRIGGER) {
1524 * scan the children, and figure out if any are running
1525 * if so, we need to be running, otherwise we need to be stopped
1526 * if we aren't in our target sstate, move to it
1529 SLIST_FOREACH(pce, &c->children, link) {
1530 child = pce->channel;
1532 if (child->flags & CHN_F_TRIGGERED)
1546 chn_lock(struct pcm_channel *c)
1552 chn_unlock(struct pcm_channel *c)