2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
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
28 #include <dev/sound/pcm/sound.h>
30 #include "feeder_if.h"
32 SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/pcm/channel.c,v 1.19.2.19 2003/03/11 15:15:41 orion Exp $");
34 #define MIN_CHUNK_SIZE 256 /* for uiomove etc. */
35 #define DMA_ALIGN_THRESHOLD 4
36 #define DMA_ALIGN_MASK (~(DMA_ALIGN_THRESHOLD - 1))
38 #define MIN(x, y) (((x) < (y))? (x) : (y))
39 #define CANCHANGE(c) (!(c->flags & CHN_F_TRIGGERED))
45 static int chn_targetirqrate = 32;
46 TUNABLE_INT("hw.snd.targetirqrate", &chn_targetirqrate);
49 sysctl_hw_snd_targetirqrate(SYSCTL_HANDLER_ARGS)
53 val = chn_targetirqrate;
54 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
55 if (val < 16 || val > 512)
58 chn_targetirqrate = val;
62 SYSCTL_PROC(_hw_snd, OID_AUTO, targetirqrate, CTLTYPE_INT | CTLFLAG_RW,
63 0, sizeof(int), sysctl_hw_snd_targetirqrate, "I", "");
64 static int report_soft_formats = 1;
65 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
66 &report_soft_formats, 1, "report software-emulated formats");
68 static int chn_buildfeeder(struct pcm_channel *c);
71 chn_lockinit(struct pcm_channel *c)
73 c->lock = snd_mtxcreate(c->name, "pcm channel");
77 chn_lockdestroy(struct pcm_channel *c)
83 chn_polltrigger(struct pcm_channel *c)
85 struct snd_dbuf *bs = c->bufsoft;
89 if (c->flags & CHN_F_MAPPED) {
90 if (sndbuf_getprevblocks(bs) == 0)
93 return (sndbuf_getblocks(bs) > sndbuf_getprevblocks(bs))? 1 : 0;
95 amt = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
96 lim = (c->flags & CHN_F_HAS_SIZE)? sndbuf_getblksz(bs) : 1;
98 return (amt >= lim)? 1 : 0;
104 chn_pollreset(struct pcm_channel *c)
106 struct snd_dbuf *bs = c->bufsoft;
109 sndbuf_updateprevtotal(bs);
114 chn_wakeup(struct pcm_channel *c)
116 struct snd_dbuf *bs = c->bufsoft;
119 if (sndbuf_getsel(bs)->si_pid && chn_polltrigger(c))
120 selwakeup(sndbuf_getsel(bs));
125 chn_sleep(struct pcm_channel *c, char *str, int timeout)
127 struct snd_dbuf *bs = c->bufsoft;
132 ret = msleep(bs, c->lock, PRIBIO | PCATCH, str, timeout);
134 ret = tsleep(bs, PRIBIO | PCATCH, str, timeout);
141 * chn_dmaupdate() tracks the status of a dma transfer,
142 * updating pointers. It must be called at spltty().
146 chn_dmaupdate(struct pcm_channel *c)
148 struct snd_dbuf *b = c->bufhard;
149 unsigned int delta, old, hwptr, amt;
151 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
154 old = sndbuf_gethwptr(b);
155 hwptr = chn_getptr(c);
156 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
157 sndbuf_sethwptr(b, hwptr);
160 if (delta >= ((sndbuf_getsize(b) * 15) / 16)) {
161 if (!(c->flags & (CHN_F_CLOSING | CHN_F_ABORTING)))
162 device_printf(c->dev, "hwptr went backwards %d -> %d\n", old, hwptr);
166 if (c->direction == PCMDIR_PLAY) {
167 amt = MIN(delta, sndbuf_getready(b));
169 sndbuf_dispose(b, NULL, amt);
171 amt = MIN(delta, sndbuf_getfree(b));
173 sndbuf_acquire(b, NULL, amt);
180 chn_wrupdate(struct pcm_channel *c)
185 KASSERT(c->direction == PCMDIR_PLAY, ("chn_wrupdate on bad channel"));
187 if ((c->flags & (CHN_F_MAPPED | CHN_F_VIRTUAL)) || !(c->flags & CHN_F_TRIGGERED))
191 /* tell the driver we've updated the primary buffer */
192 chn_trigger(c, PCMTRIG_EMLDMAWR);
194 printf("chn_wrupdate: chn_wrfeed returned %d\n", ret);)
199 chn_wrfeed(struct pcm_channel *c)
201 struct snd_dbuf *b = c->bufhard;
202 struct snd_dbuf *bs = c->bufsoft;
203 unsigned int ret, amt;
207 if (c->flags & CHN_F_CLOSING) {
208 sndbuf_dump(b, "b", 0x02);
209 sndbuf_dump(bs, "bs", 0x02);
212 if (c->flags & CHN_F_MAPPED)
213 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
215 amt = sndbuf_getfree(b);
216 if (sndbuf_getready(bs) < amt)
219 ret = (amt > 0)? sndbuf_feed(bs, b, c, c->feeder, amt) : ENOSPC;
220 if (ret == 0 && sndbuf_getfree(b) < amt)
227 chn_wrintr(struct pcm_channel *c)
232 /* update pointers in primary buffer */
234 /* ...and feed from secondary to primary */
236 /* tell the driver we've updated the primary buffer */
237 chn_trigger(c, PCMTRIG_EMLDMAWR);
239 printf("chn_wrintr: chn_wrfeed returned %d\n", ret);)
243 * user write routine - uiomove data into secondary buffer, trigger if necessary
244 * if blocking, sleep, rinse and repeat.
246 * called externally, so must handle locking
250 chn_write(struct pcm_channel *c, struct uio *buf)
252 int ret, timeout, newsize, count, sz;
253 struct snd_dbuf *bs = c->bufsoft;
257 * XXX Certain applications attempt to write larger size
258 * of pcm data than c->blocksize2nd without blocking,
259 * resulting partial write. Expand the block size so that
260 * the write operation avoids blocking.
262 if ((c->flags & CHN_F_NBIO) && buf->uio_resid > sndbuf_getblksz(bs)) {
263 DEB(device_printf(c->dev, "broken app, nbio and tried to write %d bytes with fragsz %d\n",
264 buf->uio_resid, sndbuf_getblksz(bs)));
266 while (newsize < min(buf->uio_resid, CHN_2NDBUFMAXSIZE / 2))
268 chn_setblocksize(c, sndbuf_getblkcnt(bs), newsize);
269 DEB(device_printf(c->dev, "frags reset to %d x %d\n", sndbuf_getblkcnt(bs), sndbuf_getblksz(bs)));
274 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
275 sz = sndbuf_getfree(bs);
277 if (c->flags & CHN_F_NBIO)
280 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
284 ret = chn_sleep(c, "pcmwr", timeout);
285 if (ret == EWOULDBLOCK) {
292 sz = MIN(sz, buf->uio_resid);
293 KASSERT(sz > 0, ("confusion in chn_write"));
294 /* printf("sz: %d\n", sz); */
295 ret = sndbuf_uiomove(bs, buf, sz);
296 if (ret == 0 && !(c->flags & CHN_F_TRIGGERED))
300 /* printf("ret: %d left: %d\n", ret, buf->uio_resid); */
303 c->flags |= CHN_F_DEAD;
304 printf("%s: play interrupt timeout, channel dead\n", c->name);
311 chn_rddump(struct pcm_channel *c, unsigned int cnt)
313 struct snd_dbuf *b = c->bufhard;
316 sndbuf_setxrun(b, sndbuf_getxrun(b) + cnt);
317 return sndbuf_dispose(b, NULL, cnt);
321 * Feed new data from the read buffer. Can be called in the bottom half.
322 * Hence must be called at spltty.
325 chn_rdfeed(struct pcm_channel *c)
327 struct snd_dbuf *b = c->bufhard;
328 struct snd_dbuf *bs = c->bufsoft;
329 unsigned int ret, amt;
333 if (c->flags & CHN_F_CLOSING) {
334 sndbuf_dump(b, "b", 0x02);
335 sndbuf_dump(bs, "bs", 0x02);
338 amt = sndbuf_getready(b);
339 if (sndbuf_getfree(bs) < amt) {
341 amt = sndbuf_getfree(bs);
343 ret = (amt > 0)? sndbuf_feed(b, bs, c, c->feeder, amt) : 0;
345 amt = sndbuf_getready(b);
355 chn_rdupdate(struct pcm_channel *c)
360 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
362 if ((c->flags & CHN_F_MAPPED) || !(c->flags & CHN_F_TRIGGERED))
364 chn_trigger(c, PCMTRIG_EMLDMARD);
368 printf("chn_rdfeed: %d\n", ret);
372 /* read interrupt routine. Must be called with interrupts blocked. */
374 chn_rdintr(struct pcm_channel *c)
379 /* tell the driver to update the primary buffer if non-dma */
380 chn_trigger(c, PCMTRIG_EMLDMARD);
381 /* update pointers in primary buffer */
383 /* ...and feed from primary to secondary */
388 * user read routine - trigger if necessary, uiomove data from secondary buffer
389 * if blocking, sleep, rinse and repeat.
391 * called externally, so must handle locking
395 chn_read(struct pcm_channel *c, struct uio *buf)
397 int ret, timeout, sz, count;
398 struct snd_dbuf *bs = c->bufsoft;
401 if (!(c->flags & CHN_F_TRIGGERED))
406 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
407 sz = MIN(buf->uio_resid, sndbuf_getready(bs));
410 ret = sndbuf_uiomove(bs, buf, sz);
412 if (c->flags & CHN_F_NBIO) {
415 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
418 ret = chn_sleep(c, "pcmrd", timeout);
419 if (ret == EWOULDBLOCK) {
431 c->flags |= CHN_F_DEAD;
432 printf("%s: record interrupt timeout, channel dead\n", c->name);
439 chn_intr(struct pcm_channel *c)
443 if (c->direction == PCMDIR_PLAY)
451 chn_start(struct pcm_channel *c, int force)
454 struct snd_dbuf *b = c->bufhard;
455 struct snd_dbuf *bs = c->bufsoft;
458 /* if we're running, or if we're prevented from triggering, bail */
459 if ((c->flags & CHN_F_TRIGGERED) || ((c->flags & CHN_F_NOTRIGGER) && !force))
462 i = (c->direction == PCMDIR_PLAY)? sndbuf_getready(bs) : sndbuf_getfree(bs);
463 j = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(b) : sndbuf_getready(b);
464 if (force || (i >= j)) {
465 c->flags |= CHN_F_TRIGGERED;
467 * if we're starting because a vchan started, don't feed any data
468 * or it becomes impossible to start vchans synchronised with the
469 * first one. the hardbuf should be empty so we top it up with
470 * silence to give it something to chew. the real data will be
471 * fed at the first irq.
473 if (c->direction == PCMDIR_PLAY) {
474 if (SLIST_EMPTY(&c->children))
477 sndbuf_fillsilence(b);
481 chn_trigger(c, PCMTRIG_START);
489 chn_resetbuf(struct pcm_channel *c)
491 struct snd_dbuf *b = c->bufhard;
492 struct snd_dbuf *bs = c->bufsoft;
500 * chn_sync waits until the space in the given channel goes above
501 * a threshold. The threshold is checked against fl or rl respectively.
502 * Assume that the condition can become true, do not check here...
505 chn_sync(struct pcm_channel *c, int threshold)
509 struct snd_dbuf *bs = c->bufsoft;
513 rdy = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
514 if (rdy <= threshold) {
515 ret = chn_sleep(c, "pcmsyn", 1);
516 if (ret == ERESTART || ret == EINTR) {
517 DEB(printf("chn_sync: tsleep returns %d\n", ret));
526 /* called externally, handle locking */
528 chn_poll(struct pcm_channel *c, int ev, struct proc *p)
530 struct snd_dbuf *bs = c->bufsoft;
534 if (!(c->flags & CHN_F_MAPPED) && !(c->flags & CHN_F_TRIGGERED))
537 if (chn_polltrigger(c) && chn_pollreset(c))
540 selrecord(p, sndbuf_getsel(bs));
545 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
546 * it returns the number of bytes that have not been transferred.
548 * called from: dsp_close, dsp_ioctl, with channel locked
551 chn_abort(struct pcm_channel *c)
554 struct snd_dbuf *b = c->bufhard;
555 struct snd_dbuf *bs = c->bufsoft;
558 if (!(c->flags & CHN_F_TRIGGERED))
560 c->flags |= CHN_F_ABORTING;
562 c->flags &= ~CHN_F_TRIGGERED;
563 /* kill the channel */
564 chn_trigger(c, PCMTRIG_ABORT);
566 if (!(c->flags & CHN_F_VIRTUAL))
568 missing = sndbuf_getready(bs) + sndbuf_getready(b);
570 c->flags &= ~CHN_F_ABORTING;
575 * this routine tries to flush the dma transfer. It is called
576 * on a close. We immediately abort any read DMA
577 * operation, and then wait for the play buffer to drain.
579 * called from: dsp_close
583 chn_flush(struct pcm_channel *c)
585 int ret, count, resid, resid_p;
586 struct snd_dbuf *b = c->bufhard;
587 struct snd_dbuf *bs = c->bufsoft;
590 KASSERT(c->direction == PCMDIR_PLAY, ("chn_wrupdate on bad channel"));
591 DEB(printf("chn_flush c->flags 0x%08x\n", c->flags));
592 if (!(c->flags & CHN_F_TRIGGERED))
595 c->flags |= CHN_F_CLOSING;
596 resid = sndbuf_getready(bs) + sndbuf_getready(b);
600 while ((count > 0) && (resid > sndbuf_getsize(b)) && (ret == 0)) {
601 /* still pending output data. */
602 ret = chn_sleep(c, "pcmflu", hz / 10);
603 if (ret == EWOULDBLOCK)
606 resid = sndbuf_getready(bs) + sndbuf_getready(b);
607 if (resid >= resid_p)
613 DEB(printf("chn_flush: timeout\n"));
615 c->flags &= ~CHN_F_TRIGGERED;
616 /* kill the channel */
617 chn_trigger(c, PCMTRIG_ABORT);
620 c->flags &= ~CHN_F_CLOSING;
625 fmtvalid(u_int32_t fmt, u_int32_t *fmtlist)
629 for (i = 0; fmtlist[i]; i++)
630 if (fmt == fmtlist[i])
636 chn_reset(struct pcm_channel *c, u_int32_t fmt)
641 c->flags &= CHN_F_RESET;
645 r = CHANNEL_RESET(c->methods, c->devinfo);
647 hwspd = DSP_DEFAULT_SPEED;
648 /* only do this on a record channel until feederbuilder works */
649 if (c->direction == PCMDIR_REC)
650 RANGE(hwspd, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
654 r = chn_setformat(c, fmt);
656 r = chn_setspeed(c, hwspd);
658 r = chn_setvolume(c, 100, 100);
661 r = chn_setblocksize(c, 0, 0);
664 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
670 chn_init(struct pcm_channel *c, void *devinfo, int dir)
672 struct feeder_class *fc;
673 struct snd_dbuf *b, *bs;
685 fc = feeder_getclass(NULL);
688 if (chn_addfeeder(c, fc, NULL))
692 b = sndbuf_create(c->dev, c->name, "primary");
695 bs = sndbuf_create(c->dev, c->name, "secondary");
698 sndbuf_setup(bs, NULL, 0);
705 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, dir);
706 if (c->devinfo == NULL)
710 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
713 ret = chn_setdir(c, dir);
717 ret = sndbuf_setfmt(b, AFMT_U8);
721 ret = sndbuf_setfmt(bs, AFMT_U8);
729 if (CHANNEL_FREE(c->methods, c->devinfo))
736 c->flags |= CHN_F_DEAD;
747 chn_kill(struct pcm_channel *c)
749 struct snd_dbuf *b = c->bufhard;
750 struct snd_dbuf *bs = c->bufsoft;
753 if (c->flags & CHN_F_TRIGGERED)
754 chn_trigger(c, PCMTRIG_ABORT);
755 while (chn_removefeeder(c) == 0);
756 if (CHANNEL_FREE(c->methods, c->devinfo))
758 c->flags |= CHN_F_DEAD;
766 chn_setdir(struct pcm_channel *c, int dir)
768 struct snd_dbuf *b = c->bufhard;
773 r = CHANNEL_SETDIR(c->methods, c->devinfo, c->direction);
774 if (!r && ISA_DMA(b))
775 sndbuf_isadmasetdir(b, c->direction);
780 chn_setvolume(struct pcm_channel *c, int left, int right)
783 /* could add a feeder for volume changing if channel returns -1 */
784 c->volume = (left << 8) | right;
789 chn_tryspeed(struct pcm_channel *c, int speed)
791 struct pcm_feeder *f;
792 struct snd_dbuf *b = c->bufhard;
793 struct snd_dbuf *bs = c->bufsoft;
798 DEB(printf("setspeed, channel %s\n", c->name));
799 DEB(printf("want speed %d, ", speed));
805 sndbuf_setspd(bs, speed);
806 RANGE(speed, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
807 DEB(printf("try speed %d, ", speed));
808 sndbuf_setspd(b, CHANNEL_SETSPEED(c->methods, c->devinfo, speed));
809 DEB(printf("got speed %d\n", sndbuf_getspd(b)));
811 delta = sndbuf_getspd(b) - sndbuf_getspd(bs);
815 c->feederflags &= ~(1 << FEEDER_RATE);
817 c->feederflags |= 1 << FEEDER_RATE;
819 sndbuf_setspd(bs, sndbuf_getspd(b));
821 r = chn_buildfeeder(c);
822 DEB(printf("r = %d\n", r));
826 r = chn_setblocksize(c, 0, 0);
830 if (!(c->feederflags & (1 << FEEDER_RATE)))
834 f = chn_findfeeder(c, FEEDER_RATE);
835 DEB(printf("feedrate = %p\n", f));
839 x = (c->direction == PCMDIR_REC)? b : bs;
840 r = FEEDER_SET(f, FEEDRATE_SRC, sndbuf_getspd(x));
841 DEB(printf("feeder_set(FEEDRATE_SRC, %d) = %d\n", sndbuf_getspd(x), r));
845 x = (c->direction == PCMDIR_REC)? bs : b;
846 r = FEEDER_SET(f, FEEDRATE_DST, sndbuf_getspd(x));
847 DEB(printf("feeder_set(FEEDRATE_DST, %d) = %d\n", sndbuf_getspd(x), r));
849 DEB(printf("setspeed done, r = %d\n", r));
856 chn_setspeed(struct pcm_channel *c, int speed)
858 int r, oldspeed = c->speed;
860 r = chn_tryspeed(c, speed);
862 DEB(printf("Failed to set speed %d falling back to %d\n", speed, oldspeed));
863 r = chn_tryspeed(c, oldspeed);
869 chn_tryformat(struct pcm_channel *c, u_int32_t fmt)
871 struct snd_dbuf *b = c->bufhard;
872 struct snd_dbuf *bs = c->bufsoft;
877 DEB(printf("want format %d\n", fmt));
879 r = chn_buildfeeder(c);
881 sndbuf_setfmt(bs, c->format);
883 r = CHANNEL_SETFORMAT(c->methods, c->devinfo, sndbuf_getfmt(b));
885 r = chn_tryspeed(c, c->speed);
893 chn_setformat(struct pcm_channel *c, u_int32_t fmt)
895 u_int32_t oldfmt = c->format;
898 r = chn_tryformat(c, fmt);
900 DEB(printf("Format change %d failed, reverting to %d\n", fmt, oldfmt));
901 chn_tryformat(c, oldfmt);
907 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
909 struct snd_dbuf *b = c->bufhard;
910 struct snd_dbuf *bs = c->bufsoft;
911 int bufsz, irqhz, tmp, ret;
914 if (!CANCHANGE(c) || (c->flags & CHN_F_MAPPED))
918 DEB(printf("%s(%d, %d)\n", __func__, blkcnt, blksz));
919 if (blksz == 0 || blksz == -1) {
921 c->flags &= ~CHN_F_HAS_SIZE;
922 if (!(c->flags & CHN_F_HAS_SIZE)) {
923 blksz = (sndbuf_getbps(bs) * sndbuf_getspd(bs)) / chn_targetirqrate;
929 blkcnt = CHN_2NDBUFMAXSIZE / blksz;
931 RANGE(blksz, 16, CHN_2NDBUFMAXSIZE / 2);
932 RANGE(blkcnt, 2, CHN_2NDBUFMAXSIZE / blksz);
933 DEB(printf("%s: defaulting to (%d, %d)\n", __func__, blkcnt, blksz));
935 blkcnt = sndbuf_getblkcnt(bs);
936 blksz = sndbuf_getblksz(bs);
937 DEB(printf("%s: updating (%d, %d)\n", __func__, blkcnt, blksz));
941 if ((blksz < 16) || (blkcnt < 2) || (blkcnt * blksz > CHN_2NDBUFMAXSIZE))
944 c->flags |= CHN_F_HAS_SIZE;
947 bufsz = blkcnt * blksz;
950 if (sndbuf_remalloc(bs, blkcnt, blksz))
954 /* adjust for different hw format/speed */
955 irqhz = (sndbuf_getbps(bs) * sndbuf_getspd(bs)) / sndbuf_getblksz(bs);
956 DEB(printf("%s: soft bps %d, spd %d, irqhz == %d\n", __func__, sndbuf_getbps(bs), sndbuf_getspd(bs), irqhz));
957 RANGE(irqhz, 16, 512);
959 sndbuf_setblksz(b, (sndbuf_getbps(b) * sndbuf_getspd(b)) / irqhz);
961 /* round down to 2^x */
963 while (blksz <= sndbuf_getblksz(b))
967 /* round down to fit hw buffer size */
968 RANGE(blksz, 16, sndbuf_getmaxsize(b) / 2);
969 DEB(printf("%s: hard blksz requested %d (maxsize %d), ", __func__, blksz, sndbuf_getmaxsize(b)));
971 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods, c->devinfo, blksz));
973 irqhz = (sndbuf_getbps(b) * sndbuf_getspd(b)) / sndbuf_getblksz(b);
974 DEB(printf("got %d, irqhz == %d\n", sndbuf_getblksz(b), irqhz));
982 chn_trigger(struct pcm_channel *c, int go)
984 struct snd_dbuf *b = c->bufhard;
988 if (ISA_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
989 sndbuf_isadmabounce(b);
990 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
996 chn_getptr(struct pcm_channel *c)
999 int a = (1 << c->align) - 1;
1002 hwptr = (c->flags & CHN_F_TRIGGERED)? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
1003 /* don't allow unaligned values in the hwa ptr */
1005 hwptr &= ~a ; /* Apply channel align mask */
1007 hwptr &= DMA_ALIGN_MASK; /* Apply DMA align mask */
1011 struct pcmchan_caps *
1012 chn_getcaps(struct pcm_channel *c)
1015 return CHANNEL_GETCAPS(c->methods, c->devinfo);
1019 chn_getformats(struct pcm_channel *c)
1021 u_int32_t *fmtlist, fmts;
1024 fmtlist = chn_getcaps(c)->fmtlist;
1026 for (i = 0; fmtlist[i]; i++)
1029 /* report software-supported formats */
1030 if (report_soft_formats)
1031 fmts |= AFMT_MU_LAW|AFMT_A_LAW|AFMT_U16_LE|AFMT_U16_BE|
1032 AFMT_S16_LE|AFMT_S16_BE|AFMT_U8|AFMT_S8;
1038 chn_buildfeeder(struct pcm_channel *c)
1040 struct feeder_class *fc;
1041 struct pcm_feederdesc desc;
1042 u_int32_t tmp[2], type, flags, hwfmt;
1046 while (chn_removefeeder(c) == 0);
1047 KASSERT((c->feeder == NULL), ("feeder chain not empty"));
1049 c->align = sndbuf_getalign(c->bufsoft);
1051 if (SLIST_EMPTY(&c->children)) {
1052 fc = feeder_getclass(NULL);
1053 KASSERT(fc != NULL, ("can't find root feeder"));
1055 err = chn_addfeeder(c, fc, NULL);
1057 DEB(printf("can't add root feeder, err %d\n", err));
1061 c->feeder->desc->out = c->format;
1063 desc.type = FEEDER_MIXER;
1065 desc.out = c->format;
1067 fc = feeder_getclass(&desc);
1069 DEB(printf("can't find vchan feeder\n"));
1074 err = chn_addfeeder(c, fc, &desc);
1076 DEB(printf("can't add vchan feeder, err %d\n", err));
1081 flags = c->feederflags;
1083 DEB(printf("not mapped, feederflags %x\n", flags));
1085 for (type = FEEDER_RATE; type <= FEEDER_LAST; type++) {
1086 if (flags & (1 << type)) {
1091 DEB(printf("find feeder type %d, ", type));
1092 fc = feeder_getclass(&desc);
1093 DEB(printf("got %p\n", fc));
1095 DEB(printf("can't find required feeder type %d\n", type));
1100 if (c->feeder->desc->out != fc->desc->in) {
1101 DEB(printf("build fmtchain from %x to %x: ", c->feeder->desc->out, fc->desc->in));
1102 tmp[0] = fc->desc->in;
1104 if (chn_fmtchain(c, tmp) == 0) {
1105 DEB(printf("failed\n"));
1109 DEB(printf("ok\n"));
1112 err = chn_addfeeder(c, fc, fc->desc);
1114 DEB(printf("can't add feeder %p, output %x, err %d\n", fc, fc->desc->out, err));
1118 DEB(printf("added feeder %p, output %x\n", fc, c->feeder->desc->out));
1122 if (fmtvalid(c->feeder->desc->out, chn_getcaps(c)->fmtlist)) {
1123 hwfmt = c->feeder->desc->out;
1125 if (c->direction == PCMDIR_REC) {
1128 hwfmt = chn_fmtchain(c, tmp);
1130 hwfmt = chn_fmtchain(c, chn_getcaps(c)->fmtlist);
1137 sndbuf_setfmt(c->bufhard, hwfmt);
1143 chn_notify(struct pcm_channel *c, u_int32_t flags)
1145 struct pcmchan_children *pce;
1146 struct pcm_channel *child;
1149 if (SLIST_EMPTY(&c->children))
1152 run = (c->flags & CHN_F_TRIGGERED)? 1 : 0;
1154 * if the hwchan is running, we can't change its rate, format or
1158 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
1160 if (flags & CHN_N_RATE) {
1162 * we could do something here, like scan children and decide on
1163 * the most appropriate rate to mix at, but we don't for now
1166 if (flags & CHN_N_FORMAT) {
1168 * we could do something here, like scan children and decide on
1169 * the most appropriate mixer feeder to use, but we don't for now
1172 if (flags & CHN_N_VOLUME) {
1174 * we could do something here but we don't for now
1177 if (flags & CHN_N_BLOCKSIZE) {
1180 * scan the children, find the lowest blocksize and use that
1181 * for the hard blocksize
1183 blksz = sndbuf_getmaxsize(c->bufhard) / 2;
1184 SLIST_FOREACH(pce, &c->children, link) {
1185 child = pce->channel;
1186 if (sndbuf_getblksz(child->bufhard) < blksz)
1187 blksz = sndbuf_getblksz(child->bufhard);
1189 chn_setblocksize(c, 2, blksz);
1191 if (flags & CHN_N_TRIGGER) {
1194 * scan the children, and figure out if any are running
1195 * if so, we need to be running, otherwise we need to be stopped
1196 * if we aren't in our target sstate, move to it
1199 SLIST_FOREACH(pce, &c->children, link) {
1200 child = pce->channel;
1201 if (child->flags & CHN_F_TRIGGERED)