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
27 * $FreeBSD: src/sys/dev/sound/pcm/channel.c,v 1.19.2.19 2003/03/11 15:15:41 orion Exp $
28 * $DragonFly: src/sys/dev/sound/pcm/channel.c,v 1.3 2003/06/23 17:55:34 dillon Exp $
31 #include <dev/sound/pcm/sound.h>
33 #include "feeder_if.h"
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/channel.c,v 1.3 2003/06/23 17:55:34 dillon Exp $");
37 #define MIN_CHUNK_SIZE 256 /* for uiomove etc. */
38 #define DMA_ALIGN_THRESHOLD 4
39 #define DMA_ALIGN_MASK (~(DMA_ALIGN_THRESHOLD - 1))
41 #define MIN(x, y) (((x) < (y))? (x) : (y))
42 #define CANCHANGE(c) (!(c->flags & CHN_F_TRIGGERED))
48 static int chn_targetirqrate = 32;
49 TUNABLE_INT("hw.snd.targetirqrate", &chn_targetirqrate);
52 sysctl_hw_snd_targetirqrate(SYSCTL_HANDLER_ARGS)
56 val = chn_targetirqrate;
57 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
58 if (val < 16 || val > 512)
61 chn_targetirqrate = val;
65 SYSCTL_PROC(_hw_snd, OID_AUTO, targetirqrate, CTLTYPE_INT | CTLFLAG_RW,
66 0, sizeof(int), sysctl_hw_snd_targetirqrate, "I", "");
67 static int report_soft_formats = 1;
68 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
69 &report_soft_formats, 1, "report software-emulated formats");
71 static int chn_buildfeeder(struct pcm_channel *c);
74 chn_lockinit(struct pcm_channel *c)
76 c->lock = snd_mtxcreate(c->name, "pcm channel");
80 chn_lockdestroy(struct pcm_channel *c)
86 chn_polltrigger(struct pcm_channel *c)
88 struct snd_dbuf *bs = c->bufsoft;
92 if (c->flags & CHN_F_MAPPED) {
93 if (sndbuf_getprevblocks(bs) == 0)
96 return (sndbuf_getblocks(bs) > sndbuf_getprevblocks(bs))? 1 : 0;
98 amt = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
99 lim = (c->flags & CHN_F_HAS_SIZE)? sndbuf_getblksz(bs) : 1;
101 return (amt >= lim)? 1 : 0;
107 chn_pollreset(struct pcm_channel *c)
109 struct snd_dbuf *bs = c->bufsoft;
112 sndbuf_updateprevtotal(bs);
117 chn_wakeup(struct pcm_channel *c)
119 struct snd_dbuf *bs = c->bufsoft;
122 if (sndbuf_getsel(bs)->si_pid && chn_polltrigger(c))
123 selwakeup(sndbuf_getsel(bs));
128 chn_sleep(struct pcm_channel *c, char *str, int timeout)
130 struct snd_dbuf *bs = c->bufsoft;
135 ret = msleep(bs, c->lock, PRIBIO | PCATCH, str, timeout);
137 ret = tsleep(bs, PRIBIO | PCATCH, str, timeout);
144 * chn_dmaupdate() tracks the status of a dma transfer,
145 * updating pointers. It must be called at spltty().
149 chn_dmaupdate(struct pcm_channel *c)
151 struct snd_dbuf *b = c->bufhard;
152 unsigned int delta, old, hwptr, amt;
154 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
157 old = sndbuf_gethwptr(b);
158 hwptr = chn_getptr(c);
159 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
160 sndbuf_sethwptr(b, hwptr);
163 if (delta >= ((sndbuf_getsize(b) * 15) / 16)) {
164 if (!(c->flags & (CHN_F_CLOSING | CHN_F_ABORTING)))
165 device_printf(c->dev, "hwptr went backwards %d -> %d\n", old, hwptr);
169 if (c->direction == PCMDIR_PLAY) {
170 amt = MIN(delta, sndbuf_getready(b));
172 sndbuf_dispose(b, NULL, amt);
174 amt = MIN(delta, sndbuf_getfree(b));
176 sndbuf_acquire(b, NULL, amt);
183 chn_wrupdate(struct pcm_channel *c)
188 KASSERT(c->direction == PCMDIR_PLAY, ("chn_wrupdate on bad channel"));
190 if ((c->flags & (CHN_F_MAPPED | CHN_F_VIRTUAL)) || !(c->flags & CHN_F_TRIGGERED))
194 /* tell the driver we've updated the primary buffer */
195 chn_trigger(c, PCMTRIG_EMLDMAWR);
197 printf("chn_wrupdate: chn_wrfeed returned %d\n", ret);)
202 chn_wrfeed(struct pcm_channel *c)
204 struct snd_dbuf *b = c->bufhard;
205 struct snd_dbuf *bs = c->bufsoft;
206 unsigned int ret, amt;
210 if (c->flags & CHN_F_CLOSING) {
211 sndbuf_dump(b, "b", 0x02);
212 sndbuf_dump(bs, "bs", 0x02);
215 if (c->flags & CHN_F_MAPPED)
216 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
218 amt = sndbuf_getfree(b);
219 if (sndbuf_getready(bs) < amt)
222 ret = (amt > 0)? sndbuf_feed(bs, b, c, c->feeder, amt) : ENOSPC;
223 if (ret == 0 && sndbuf_getfree(b) < amt)
230 chn_wrintr(struct pcm_channel *c)
235 /* update pointers in primary buffer */
237 /* ...and feed from secondary to primary */
239 /* tell the driver we've updated the primary buffer */
240 chn_trigger(c, PCMTRIG_EMLDMAWR);
242 printf("chn_wrintr: chn_wrfeed returned %d\n", ret);)
246 * user write routine - uiomove data into secondary buffer, trigger if necessary
247 * if blocking, sleep, rinse and repeat.
249 * called externally, so must handle locking
253 chn_write(struct pcm_channel *c, struct uio *buf)
255 int ret, timeout, newsize, count, sz;
256 struct snd_dbuf *bs = c->bufsoft;
260 * XXX Certain applications attempt to write larger size
261 * of pcm data than c->blocksize2nd without blocking,
262 * resulting partial write. Expand the block size so that
263 * the write operation avoids blocking.
265 if ((c->flags & CHN_F_NBIO) && buf->uio_resid > sndbuf_getblksz(bs)) {
266 DEB(device_printf(c->dev, "broken app, nbio and tried to write %d bytes with fragsz %d\n",
267 buf->uio_resid, sndbuf_getblksz(bs)));
269 while (newsize < min(buf->uio_resid, CHN_2NDBUFMAXSIZE / 2))
271 chn_setblocksize(c, sndbuf_getblkcnt(bs), newsize);
272 DEB(device_printf(c->dev, "frags reset to %d x %d\n", sndbuf_getblkcnt(bs), sndbuf_getblksz(bs)));
277 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
278 sz = sndbuf_getfree(bs);
280 if (c->flags & CHN_F_NBIO)
283 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
287 ret = chn_sleep(c, "pcmwr", timeout);
288 if (ret == EWOULDBLOCK) {
295 sz = MIN(sz, buf->uio_resid);
296 KASSERT(sz > 0, ("confusion in chn_write"));
297 /* printf("sz: %d\n", sz); */
298 ret = sndbuf_uiomove(bs, buf, sz);
299 if (ret == 0 && !(c->flags & CHN_F_TRIGGERED))
303 /* printf("ret: %d left: %d\n", ret, buf->uio_resid); */
306 c->flags |= CHN_F_DEAD;
307 printf("%s: play interrupt timeout, channel dead\n", c->name);
314 chn_rddump(struct pcm_channel *c, unsigned int cnt)
316 struct snd_dbuf *b = c->bufhard;
319 sndbuf_setxrun(b, sndbuf_getxrun(b) + cnt);
320 return sndbuf_dispose(b, NULL, cnt);
324 * Feed new data from the read buffer. Can be called in the bottom half.
325 * Hence must be called at spltty.
328 chn_rdfeed(struct pcm_channel *c)
330 struct snd_dbuf *b = c->bufhard;
331 struct snd_dbuf *bs = c->bufsoft;
332 unsigned int ret, amt;
336 if (c->flags & CHN_F_CLOSING) {
337 sndbuf_dump(b, "b", 0x02);
338 sndbuf_dump(bs, "bs", 0x02);
341 amt = sndbuf_getready(b);
342 if (sndbuf_getfree(bs) < amt) {
344 amt = sndbuf_getfree(bs);
346 ret = (amt > 0)? sndbuf_feed(b, bs, c, c->feeder, amt) : 0;
348 amt = sndbuf_getready(b);
358 chn_rdupdate(struct pcm_channel *c)
363 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
365 if ((c->flags & CHN_F_MAPPED) || !(c->flags & CHN_F_TRIGGERED))
367 chn_trigger(c, PCMTRIG_EMLDMARD);
371 printf("chn_rdfeed: %d\n", ret);
375 /* read interrupt routine. Must be called with interrupts blocked. */
377 chn_rdintr(struct pcm_channel *c)
382 /* tell the driver to update the primary buffer if non-dma */
383 chn_trigger(c, PCMTRIG_EMLDMARD);
384 /* update pointers in primary buffer */
386 /* ...and feed from primary to secondary */
391 * user read routine - trigger if necessary, uiomove data from secondary buffer
392 * if blocking, sleep, rinse and repeat.
394 * called externally, so must handle locking
398 chn_read(struct pcm_channel *c, struct uio *buf)
400 int ret, timeout, sz, count;
401 struct snd_dbuf *bs = c->bufsoft;
404 if (!(c->flags & CHN_F_TRIGGERED))
409 while (!ret && (buf->uio_resid > 0) && (count > 0)) {
410 sz = MIN(buf->uio_resid, sndbuf_getready(bs));
413 ret = sndbuf_uiomove(bs, buf, sz);
415 if (c->flags & CHN_F_NBIO) {
418 timeout = (hz * sndbuf_getblksz(bs)) / (sndbuf_getspd(bs) * sndbuf_getbps(bs));
421 ret = chn_sleep(c, "pcmrd", timeout);
422 if (ret == EWOULDBLOCK) {
434 c->flags |= CHN_F_DEAD;
435 printf("%s: record interrupt timeout, channel dead\n", c->name);
442 chn_intr(struct pcm_channel *c)
446 if (c->direction == PCMDIR_PLAY)
454 chn_start(struct pcm_channel *c, int force)
457 struct snd_dbuf *b = c->bufhard;
458 struct snd_dbuf *bs = c->bufsoft;
461 /* if we're running, or if we're prevented from triggering, bail */
462 if ((c->flags & CHN_F_TRIGGERED) || ((c->flags & CHN_F_NOTRIGGER) && !force))
465 i = (c->direction == PCMDIR_PLAY)? sndbuf_getready(bs) : sndbuf_getfree(bs);
466 j = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(b) : sndbuf_getready(b);
467 if (force || (i >= j)) {
468 c->flags |= CHN_F_TRIGGERED;
470 * if we're starting because a vchan started, don't feed any data
471 * or it becomes impossible to start vchans synchronised with the
472 * first one. the hardbuf should be empty so we top it up with
473 * silence to give it something to chew. the real data will be
474 * fed at the first irq.
476 if (c->direction == PCMDIR_PLAY) {
477 if (SLIST_EMPTY(&c->children))
480 sndbuf_fillsilence(b);
484 chn_trigger(c, PCMTRIG_START);
492 chn_resetbuf(struct pcm_channel *c)
494 struct snd_dbuf *b = c->bufhard;
495 struct snd_dbuf *bs = c->bufsoft;
503 * chn_sync waits until the space in the given channel goes above
504 * a threshold. The threshold is checked against fl or rl respectively.
505 * Assume that the condition can become true, do not check here...
508 chn_sync(struct pcm_channel *c, int threshold)
512 struct snd_dbuf *bs = c->bufsoft;
516 rdy = (c->direction == PCMDIR_PLAY)? sndbuf_getfree(bs) : sndbuf_getready(bs);
517 if (rdy <= threshold) {
518 ret = chn_sleep(c, "pcmsyn", 1);
519 if (ret == ERESTART || ret == EINTR) {
520 DEB(printf("chn_sync: tsleep returns %d\n", ret));
529 /* called externally, handle locking */
531 chn_poll(struct pcm_channel *c, int ev, struct proc *p)
533 struct snd_dbuf *bs = c->bufsoft;
537 if (!(c->flags & CHN_F_MAPPED) && !(c->flags & CHN_F_TRIGGERED))
540 if (chn_polltrigger(c) && chn_pollreset(c))
543 selrecord(p->p_thread, sndbuf_getsel(bs));
548 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
549 * it returns the number of bytes that have not been transferred.
551 * called from: dsp_close, dsp_ioctl, with channel locked
554 chn_abort(struct pcm_channel *c)
557 struct snd_dbuf *b = c->bufhard;
558 struct snd_dbuf *bs = c->bufsoft;
561 if (!(c->flags & CHN_F_TRIGGERED))
563 c->flags |= CHN_F_ABORTING;
565 c->flags &= ~CHN_F_TRIGGERED;
566 /* kill the channel */
567 chn_trigger(c, PCMTRIG_ABORT);
569 if (!(c->flags & CHN_F_VIRTUAL))
571 missing = sndbuf_getready(bs) + sndbuf_getready(b);
573 c->flags &= ~CHN_F_ABORTING;
578 * this routine tries to flush the dma transfer. It is called
579 * on a close. We immediately abort any read DMA
580 * operation, and then wait for the play buffer to drain.
582 * called from: dsp_close
586 chn_flush(struct pcm_channel *c)
588 int ret, count, resid, resid_p;
589 struct snd_dbuf *b = c->bufhard;
590 struct snd_dbuf *bs = c->bufsoft;
593 KASSERT(c->direction == PCMDIR_PLAY, ("chn_wrupdate on bad channel"));
594 DEB(printf("chn_flush c->flags 0x%08x\n", c->flags));
595 if (!(c->flags & CHN_F_TRIGGERED))
598 c->flags |= CHN_F_CLOSING;
599 resid = sndbuf_getready(bs) + sndbuf_getready(b);
603 while ((count > 0) && (resid > sndbuf_getsize(b)) && (ret == 0)) {
604 /* still pending output data. */
605 ret = chn_sleep(c, "pcmflu", hz / 10);
606 if (ret == EWOULDBLOCK)
609 resid = sndbuf_getready(bs) + sndbuf_getready(b);
610 if (resid >= resid_p)
616 DEB(printf("chn_flush: timeout\n"));
618 c->flags &= ~CHN_F_TRIGGERED;
619 /* kill the channel */
620 chn_trigger(c, PCMTRIG_ABORT);
623 c->flags &= ~CHN_F_CLOSING;
628 fmtvalid(u_int32_t fmt, u_int32_t *fmtlist)
632 for (i = 0; fmtlist[i]; i++)
633 if (fmt == fmtlist[i])
639 chn_reset(struct pcm_channel *c, u_int32_t fmt)
644 c->flags &= CHN_F_RESET;
648 r = CHANNEL_RESET(c->methods, c->devinfo);
650 hwspd = DSP_DEFAULT_SPEED;
651 /* only do this on a record channel until feederbuilder works */
652 if (c->direction == PCMDIR_REC)
653 RANGE(hwspd, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
657 r = chn_setformat(c, fmt);
659 r = chn_setspeed(c, hwspd);
661 r = chn_setvolume(c, 100, 100);
664 r = chn_setblocksize(c, 0, 0);
667 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
673 chn_init(struct pcm_channel *c, void *devinfo, int dir)
675 struct feeder_class *fc;
676 struct snd_dbuf *b, *bs;
688 fc = feeder_getclass(NULL);
691 if (chn_addfeeder(c, fc, NULL))
695 b = sndbuf_create(c->dev, c->name, "primary");
698 bs = sndbuf_create(c->dev, c->name, "secondary");
701 sndbuf_setup(bs, NULL, 0);
708 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, dir);
709 if (c->devinfo == NULL)
713 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
716 ret = chn_setdir(c, dir);
720 ret = sndbuf_setfmt(b, AFMT_U8);
724 ret = sndbuf_setfmt(bs, AFMT_U8);
732 if (CHANNEL_FREE(c->methods, c->devinfo))
739 c->flags |= CHN_F_DEAD;
750 chn_kill(struct pcm_channel *c)
752 struct snd_dbuf *b = c->bufhard;
753 struct snd_dbuf *bs = c->bufsoft;
756 if (c->flags & CHN_F_TRIGGERED)
757 chn_trigger(c, PCMTRIG_ABORT);
758 while (chn_removefeeder(c) == 0);
759 if (CHANNEL_FREE(c->methods, c->devinfo))
761 c->flags |= CHN_F_DEAD;
769 chn_setdir(struct pcm_channel *c, int dir)
771 struct snd_dbuf *b = c->bufhard;
776 r = CHANNEL_SETDIR(c->methods, c->devinfo, c->direction);
777 if (!r && ISA_DMA(b))
778 sndbuf_isadmasetdir(b, c->direction);
783 chn_setvolume(struct pcm_channel *c, int left, int right)
786 /* could add a feeder for volume changing if channel returns -1 */
787 c->volume = (left << 8) | right;
792 chn_tryspeed(struct pcm_channel *c, int speed)
794 struct pcm_feeder *f;
795 struct snd_dbuf *b = c->bufhard;
796 struct snd_dbuf *bs = c->bufsoft;
801 DEB(printf("setspeed, channel %s\n", c->name));
802 DEB(printf("want speed %d, ", speed));
808 sndbuf_setspd(bs, speed);
809 RANGE(speed, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed);
810 DEB(printf("try speed %d, ", speed));
811 sndbuf_setspd(b, CHANNEL_SETSPEED(c->methods, c->devinfo, speed));
812 DEB(printf("got speed %d\n", sndbuf_getspd(b)));
814 delta = sndbuf_getspd(b) - sndbuf_getspd(bs);
818 c->feederflags &= ~(1 << FEEDER_RATE);
820 c->feederflags |= 1 << FEEDER_RATE;
822 sndbuf_setspd(bs, sndbuf_getspd(b));
824 r = chn_buildfeeder(c);
825 DEB(printf("r = %d\n", r));
829 r = chn_setblocksize(c, 0, 0);
833 if (!(c->feederflags & (1 << FEEDER_RATE)))
837 f = chn_findfeeder(c, FEEDER_RATE);
838 DEB(printf("feedrate = %p\n", f));
842 x = (c->direction == PCMDIR_REC)? b : bs;
843 r = FEEDER_SET(f, FEEDRATE_SRC, sndbuf_getspd(x));
844 DEB(printf("feeder_set(FEEDRATE_SRC, %d) = %d\n", sndbuf_getspd(x), r));
848 x = (c->direction == PCMDIR_REC)? bs : b;
849 r = FEEDER_SET(f, FEEDRATE_DST, sndbuf_getspd(x));
850 DEB(printf("feeder_set(FEEDRATE_DST, %d) = %d\n", sndbuf_getspd(x), r));
852 DEB(printf("setspeed done, r = %d\n", r));
859 chn_setspeed(struct pcm_channel *c, int speed)
861 int r, oldspeed = c->speed;
863 r = chn_tryspeed(c, speed);
865 DEB(printf("Failed to set speed %d falling back to %d\n", speed, oldspeed));
866 r = chn_tryspeed(c, oldspeed);
872 chn_tryformat(struct pcm_channel *c, u_int32_t fmt)
874 struct snd_dbuf *b = c->bufhard;
875 struct snd_dbuf *bs = c->bufsoft;
880 DEB(printf("want format %d\n", fmt));
882 r = chn_buildfeeder(c);
884 sndbuf_setfmt(bs, c->format);
886 r = CHANNEL_SETFORMAT(c->methods, c->devinfo, sndbuf_getfmt(b));
888 r = chn_tryspeed(c, c->speed);
896 chn_setformat(struct pcm_channel *c, u_int32_t fmt)
898 u_int32_t oldfmt = c->format;
901 r = chn_tryformat(c, fmt);
903 DEB(printf("Format change %d failed, reverting to %d\n", fmt, oldfmt));
904 chn_tryformat(c, oldfmt);
910 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
912 struct snd_dbuf *b = c->bufhard;
913 struct snd_dbuf *bs = c->bufsoft;
914 int bufsz, irqhz, tmp, ret;
917 if (!CANCHANGE(c) || (c->flags & CHN_F_MAPPED))
921 DEB(printf("%s(%d, %d)\n", __func__, blkcnt, blksz));
922 if (blksz == 0 || blksz == -1) {
924 c->flags &= ~CHN_F_HAS_SIZE;
925 if (!(c->flags & CHN_F_HAS_SIZE)) {
926 blksz = (sndbuf_getbps(bs) * sndbuf_getspd(bs)) / chn_targetirqrate;
932 blkcnt = CHN_2NDBUFMAXSIZE / blksz;
934 RANGE(blksz, 16, CHN_2NDBUFMAXSIZE / 2);
935 RANGE(blkcnt, 2, CHN_2NDBUFMAXSIZE / blksz);
936 DEB(printf("%s: defaulting to (%d, %d)\n", __func__, blkcnt, blksz));
938 blkcnt = sndbuf_getblkcnt(bs);
939 blksz = sndbuf_getblksz(bs);
940 DEB(printf("%s: updating (%d, %d)\n", __func__, blkcnt, blksz));
944 if ((blksz < 16) || (blkcnt < 2) || (blkcnt * blksz > CHN_2NDBUFMAXSIZE))
947 c->flags |= CHN_F_HAS_SIZE;
950 bufsz = blkcnt * blksz;
953 if (sndbuf_remalloc(bs, blkcnt, blksz))
957 /* adjust for different hw format/speed */
958 irqhz = (sndbuf_getbps(bs) * sndbuf_getspd(bs)) / sndbuf_getblksz(bs);
959 DEB(printf("%s: soft bps %d, spd %d, irqhz == %d\n", __func__, sndbuf_getbps(bs), sndbuf_getspd(bs), irqhz));
960 RANGE(irqhz, 16, 512);
962 sndbuf_setblksz(b, (sndbuf_getbps(b) * sndbuf_getspd(b)) / irqhz);
964 /* round down to 2^x */
966 while (blksz <= sndbuf_getblksz(b))
970 /* round down to fit hw buffer size */
971 RANGE(blksz, 16, sndbuf_getmaxsize(b) / 2);
972 DEB(printf("%s: hard blksz requested %d (maxsize %d), ", __func__, blksz, sndbuf_getmaxsize(b)));
974 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods, c->devinfo, blksz));
976 irqhz = (sndbuf_getbps(b) * sndbuf_getspd(b)) / sndbuf_getblksz(b);
977 DEB(printf("got %d, irqhz == %d\n", sndbuf_getblksz(b), irqhz));
985 chn_trigger(struct pcm_channel *c, int go)
987 struct snd_dbuf *b = c->bufhard;
991 if (ISA_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
992 sndbuf_isadmabounce(b);
993 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
999 chn_getptr(struct pcm_channel *c)
1002 int a = (1 << c->align) - 1;
1005 hwptr = (c->flags & CHN_F_TRIGGERED)? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
1006 /* don't allow unaligned values in the hwa ptr */
1008 hwptr &= ~a ; /* Apply channel align mask */
1010 hwptr &= DMA_ALIGN_MASK; /* Apply DMA align mask */
1014 struct pcmchan_caps *
1015 chn_getcaps(struct pcm_channel *c)
1018 return CHANNEL_GETCAPS(c->methods, c->devinfo);
1022 chn_getformats(struct pcm_channel *c)
1024 u_int32_t *fmtlist, fmts;
1027 fmtlist = chn_getcaps(c)->fmtlist;
1029 for (i = 0; fmtlist[i]; i++)
1032 /* report software-supported formats */
1033 if (report_soft_formats)
1034 fmts |= AFMT_MU_LAW|AFMT_A_LAW|AFMT_U16_LE|AFMT_U16_BE|
1035 AFMT_S16_LE|AFMT_S16_BE|AFMT_U8|AFMT_S8;
1041 chn_buildfeeder(struct pcm_channel *c)
1043 struct feeder_class *fc;
1044 struct pcm_feederdesc desc;
1045 u_int32_t tmp[2], type, flags, hwfmt;
1049 while (chn_removefeeder(c) == 0);
1050 KASSERT((c->feeder == NULL), ("feeder chain not empty"));
1052 c->align = sndbuf_getalign(c->bufsoft);
1054 if (SLIST_EMPTY(&c->children)) {
1055 fc = feeder_getclass(NULL);
1056 KASSERT(fc != NULL, ("can't find root feeder"));
1058 err = chn_addfeeder(c, fc, NULL);
1060 DEB(printf("can't add root feeder, err %d\n", err));
1064 c->feeder->desc->out = c->format;
1066 desc.type = FEEDER_MIXER;
1068 desc.out = c->format;
1070 fc = feeder_getclass(&desc);
1072 DEB(printf("can't find vchan feeder\n"));
1077 err = chn_addfeeder(c, fc, &desc);
1079 DEB(printf("can't add vchan feeder, err %d\n", err));
1084 flags = c->feederflags;
1086 DEB(printf("not mapped, feederflags %x\n", flags));
1088 for (type = FEEDER_RATE; type <= FEEDER_LAST; type++) {
1089 if (flags & (1 << type)) {
1094 DEB(printf("find feeder type %d, ", type));
1095 fc = feeder_getclass(&desc);
1096 DEB(printf("got %p\n", fc));
1098 DEB(printf("can't find required feeder type %d\n", type));
1103 if (c->feeder->desc->out != fc->desc->in) {
1104 DEB(printf("build fmtchain from %x to %x: ", c->feeder->desc->out, fc->desc->in));
1105 tmp[0] = fc->desc->in;
1107 if (chn_fmtchain(c, tmp) == 0) {
1108 DEB(printf("failed\n"));
1112 DEB(printf("ok\n"));
1115 err = chn_addfeeder(c, fc, fc->desc);
1117 DEB(printf("can't add feeder %p, output %x, err %d\n", fc, fc->desc->out, err));
1121 DEB(printf("added feeder %p, output %x\n", fc, c->feeder->desc->out));
1125 if (fmtvalid(c->feeder->desc->out, chn_getcaps(c)->fmtlist)) {
1126 hwfmt = c->feeder->desc->out;
1128 if (c->direction == PCMDIR_REC) {
1131 hwfmt = chn_fmtchain(c, tmp);
1133 hwfmt = chn_fmtchain(c, chn_getcaps(c)->fmtlist);
1140 sndbuf_setfmt(c->bufhard, hwfmt);
1146 chn_notify(struct pcm_channel *c, u_int32_t flags)
1148 struct pcmchan_children *pce;
1149 struct pcm_channel *child;
1152 if (SLIST_EMPTY(&c->children))
1155 run = (c->flags & CHN_F_TRIGGERED)? 1 : 0;
1157 * if the hwchan is running, we can't change its rate, format or
1161 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
1163 if (flags & CHN_N_RATE) {
1165 * we could do something here, like scan children and decide on
1166 * the most appropriate rate to mix at, but we don't for now
1169 if (flags & CHN_N_FORMAT) {
1171 * we could do something here, like scan children and decide on
1172 * the most appropriate mixer feeder to use, but we don't for now
1175 if (flags & CHN_N_VOLUME) {
1177 * we could do something here but we don't for now
1180 if (flags & CHN_N_BLOCKSIZE) {
1183 * scan the children, find the lowest blocksize and use that
1184 * for the hard blocksize
1186 blksz = sndbuf_getmaxsize(c->bufhard) / 2;
1187 SLIST_FOREACH(pce, &c->children, link) {
1188 child = pce->channel;
1189 if (sndbuf_getblksz(child->bufhard) < blksz)
1190 blksz = sndbuf_getblksz(child->bufhard);
1192 chn_setblocksize(c, 2, blksz);
1194 if (flags & CHN_N_TRIGGER) {
1197 * scan the children, and figure out if any are running
1198 * if so, we need to be running, otherwise we need to be stopped
1199 * if we aren't in our target sstate, move to it
1202 SLIST_FOREACH(pce, &c->children, link) {
1203 child = pce->channel;
1204 if (child->flags & CHN_F_TRIGGERED)