2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/sound/pcm/buffer.c,v 1.1.2.4 2002/04/22 15:49:35 cg Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.2 2003/06/17 04:28:31 dillon Exp $
30 #include <dev/sound/pcm/sound.h>
32 #include "feeder_if.h"
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.2 2003/06/17 04:28:31 dillon Exp $");
36 #define MIN(x, y) (((x) < (y))? (x) : (y))
38 #define SNDBUF_NAMELEN 48
41 u_int8_t *buf, *tmpbuf;
42 unsigned int bufsize, maxsize;
43 volatile int dl; /* transfer size */
44 volatile int rp; /* pointers to the ready area */
45 volatile int rl; /* length of ready area */
47 volatile u_int32_t total, prev_total;
48 int isadmachan, dir; /* dma channel */
49 u_int32_t fmt, spd, bps;
50 unsigned int blksz, blkcnt;
56 char name[SNDBUF_NAMELEN];
60 sndbuf_create(device_t dev, char *drv, char *desc)
64 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
65 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
72 sndbuf_destroy(struct snd_dbuf *b)
78 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
80 struct snd_dbuf *b = (struct snd_dbuf *)arg;
83 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
84 (unsigned long)segs->ds_len);
85 printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
90 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
91 * the driver can call malloc(9) and sndbuf_setup() itself.
94 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
98 b->bufsize = b->maxsize;
99 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, &b->dmamap))
101 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, sndbuf_setmap, b, 0))
103 return sndbuf_resize(b, 2, b->maxsize / 2);
107 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
111 b->bufsize = b->maxsize;
112 return sndbuf_resize(b, 2, b->maxsize / 2);
116 sndbuf_free(struct snd_dbuf *b)
119 free(b->tmpbuf, M_DEVBUF);
123 bus_dmamap_unload(b->dmatag, b->dmamap);
125 if (b->dmamap && b->buf)
126 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
132 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
140 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize))
142 if (blkcnt == b->blkcnt && blksz == b->blksz)
146 b->bufsize = blkcnt * blksz;
148 free(b->tmpbuf, M_DEVBUF);
149 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
155 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
157 if (blkcnt < 2 || blksz < 16)
163 b->maxsize = blkcnt * blksz;
164 b->bufsize = b->maxsize;
167 free(b->buf, M_DEVBUF);
168 b->buf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
173 free(b->tmpbuf, M_DEVBUF);
174 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
175 if (b->tmpbuf == NULL)
183 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
190 if (length > b->bufsize)
193 if (b->fmt & AFMT_SIGNED)
198 i = sndbuf_getfreeptr(b);
199 p = sndbuf_getbuf(b);
210 sndbuf_fillsilence(struct snd_dbuf *b)
215 if (b->fmt & AFMT_SIGNED)
221 p = sndbuf_getbuf(b);
222 while (i < b->bufsize)
229 sndbuf_reset(struct snd_dbuf *b)
238 if (b->buf && b->bufsize > 0)
239 sndbuf_clear(b, b->bufsize);
243 sndbuf_getfmt(struct snd_dbuf *b)
249 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
253 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
254 b->bps <<= (b->fmt & AFMT_16BIT)? 1 : 0;
255 b->bps <<= (b->fmt & AFMT_32BIT)? 2 : 0;
260 sndbuf_getspd(struct snd_dbuf *b)
266 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
272 sndbuf_getalign(struct snd_dbuf *b)
274 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
276 return align[b->bps - 1];
280 sndbuf_getblkcnt(struct snd_dbuf *b)
286 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
292 sndbuf_getblksz(struct snd_dbuf *b)
298 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
304 sndbuf_getbps(struct snd_dbuf *b)
310 sndbuf_getbuf(struct snd_dbuf *b)
316 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
318 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
324 sndbuf_getsize(struct snd_dbuf *b)
330 sndbuf_getmaxsize(struct snd_dbuf *b)
336 sndbuf_runsz(struct snd_dbuf *b)
342 sndbuf_setrun(struct snd_dbuf *b, int go)
344 b->dl = go? b->blksz : 0;
348 sndbuf_getsel(struct snd_dbuf *b)
353 /************************************************************/
355 sndbuf_getxrun(struct snd_dbuf *b)
357 SNDBUF_LOCKASSERT(b);
363 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
365 SNDBUF_LOCKASSERT(b);
371 sndbuf_gethwptr(struct snd_dbuf *b)
373 SNDBUF_LOCKASSERT(b);
379 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
381 SNDBUF_LOCKASSERT(b);
387 sndbuf_getready(struct snd_dbuf *b)
389 SNDBUF_LOCKASSERT(b);
390 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
396 sndbuf_getreadyptr(struct snd_dbuf *b)
398 SNDBUF_LOCKASSERT(b);
399 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
405 sndbuf_getfree(struct snd_dbuf *b)
407 SNDBUF_LOCKASSERT(b);
408 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
410 return b->bufsize - b->rl;
414 sndbuf_getfreeptr(struct snd_dbuf *b)
416 SNDBUF_LOCKASSERT(b);
417 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
418 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
420 return (b->rp + b->rl) % b->bufsize;
424 sndbuf_getblocks(struct snd_dbuf *b)
426 SNDBUF_LOCKASSERT(b);
428 return b->total / b->blksz;
432 sndbuf_getprevblocks(struct snd_dbuf *b)
434 SNDBUF_LOCKASSERT(b);
436 return b->prev_total / b->blksz;
440 sndbuf_gettotal(struct snd_dbuf *b)
442 SNDBUF_LOCKASSERT(b);
448 sndbuf_updateprevtotal(struct snd_dbuf *b)
450 SNDBUF_LOCKASSERT(b);
452 b->prev_total = b->total;
455 /************************************************************/
458 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
462 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
463 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
467 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
468 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
475 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
481 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
485 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
486 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
489 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
490 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
493 b->rp = (b->rp + l) % b->bufsize;
498 b->rp = (b->rp + count) % b->bufsize;
500 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
506 sndbuf_uiomove(struct snd_dbuf *b, struct uio *uio, unsigned int count)
508 int x, c, p, rd, err;
511 rd = (uio->uio_rw == UIO_READ)? 1 : 0;
512 if (count > uio->uio_resid)
515 if (count > (rd? sndbuf_getready(b) : sndbuf_getfree(b))) {
519 while (err == 0 && count > 0) {
520 p = rd? sndbuf_getreadyptr(b) : sndbuf_getfreeptr(b);
521 c = MIN(count, sndbuf_getsize(b) - p);
523 err = uiomove(sndbuf_getbufofs(b, p), c, uio);
526 x = rd? sndbuf_dispose(b, NULL, x) : sndbuf_acquire(b, NULL, x);
532 /* count is number of bytes we want added to destination buffer */
534 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
536 KASSERT(count > 0, ("can't feed 0 bytes"));
538 if (sndbuf_getfree(to) < count)
541 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
543 sndbuf_acquire(to, to->tmpbuf, count);
544 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
549 /************************************************************/
552 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
556 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
558 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
560 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
562 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
564 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
568 /************************************************************/
570 sndbuf_getflags(struct snd_dbuf *b)
576 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
583 /************************************************************/
586 sndbuf_isadmasetup(struct snd_dbuf *b, struct resource *drq)
588 /* should do isa_dma_acquire/isa_dma_release here */
592 sndbuf_setflags(b, SNDBUF_F_ISADMA, 1);
593 b->isadmachan = rman_get_start(drq);
599 sndbuf_isadmasetdir(struct snd_dbuf *b, int dir)
601 KASSERT(b, ("sndbuf_isadmasetdir called with b == NULL"));
602 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmasetdir called on non-ISA buffer"));
604 b->dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
609 sndbuf_isadma(struct snd_dbuf *b, int go)
611 KASSERT(b, ("sndbuf_isadma called with b == NULL"));
612 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadma called on non-ISA buffer"));
616 /* isa_dmainit(b->chan, size); */
617 isa_dmastart(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
622 isa_dmastop(b->isadmachan);
623 isa_dmadone(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
627 DEB(printf("buf 0x%p ISA DMA %s, channel %d\n",
629 (go == PCMTRIG_START)? "started" : "stopped",
634 sndbuf_isadmaptr(struct snd_dbuf *b)
638 KASSERT(b, ("sndbuf_isadmaptr called with b == NULL"));
639 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmaptr called on non-ISA buffer"));
641 if (!sndbuf_runsz(b))
643 i = isa_dmastatus(b->isadmachan);
644 KASSERT(i >= 0, ("isa_dmastatus returned %d", i));
645 return b->bufsize - i;
649 sndbuf_isadmabounce(struct snd_dbuf *b)
651 KASSERT(b, ("sndbuf_isadmabounce called with b == NULL"));
652 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmabounce called on non-ISA buffer"));
654 /* tell isa_dma to bounce data in/out */