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.3 2003/07/30 00:20:39 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.3 2003/07/30 00:20:39 dillon Exp $");
36 #define SNDBUF_NAMELEN 48
39 u_int8_t *buf, *tmpbuf;
40 unsigned int bufsize, maxsize;
41 volatile int dl; /* transfer size */
42 volatile int rp; /* pointers to the ready area */
43 volatile int rl; /* length of ready area */
45 volatile u_int32_t total, prev_total;
46 int isadmachan, dir; /* dma channel */
47 u_int32_t fmt, spd, bps;
48 unsigned int blksz, blkcnt;
54 char name[SNDBUF_NAMELEN];
58 sndbuf_create(device_t dev, char *drv, char *desc)
62 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
63 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
70 sndbuf_destroy(struct snd_dbuf *b)
76 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
78 struct snd_dbuf *b = (struct snd_dbuf *)arg;
81 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
82 (unsigned long)segs->ds_len);
83 printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
88 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
89 * the driver can call malloc(9) and sndbuf_setup() itself.
92 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
96 b->bufsize = b->maxsize;
97 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, &b->dmamap))
99 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, sndbuf_setmap, b, 0))
101 return sndbuf_resize(b, 2, b->maxsize / 2);
105 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
109 b->bufsize = b->maxsize;
110 return sndbuf_resize(b, 2, b->maxsize / 2);
114 sndbuf_free(struct snd_dbuf *b)
117 free(b->tmpbuf, M_DEVBUF);
121 bus_dmamap_unload(b->dmatag, b->dmamap);
123 if (b->dmamap && b->buf)
124 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
130 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
138 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize))
140 if (blkcnt == b->blkcnt && blksz == b->blksz)
144 b->bufsize = blkcnt * blksz;
146 free(b->tmpbuf, M_DEVBUF);
147 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
153 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
155 if (blkcnt < 2 || blksz < 16)
161 b->maxsize = blkcnt * blksz;
162 b->bufsize = b->maxsize;
165 free(b->buf, M_DEVBUF);
166 b->buf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
171 free(b->tmpbuf, M_DEVBUF);
172 b->tmpbuf = malloc(b->bufsize, M_DEVBUF, M_WAITOK);
173 if (b->tmpbuf == NULL)
181 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
188 if (length > b->bufsize)
191 if (b->fmt & AFMT_SIGNED)
196 i = sndbuf_getfreeptr(b);
197 p = sndbuf_getbuf(b);
208 sndbuf_fillsilence(struct snd_dbuf *b)
213 if (b->fmt & AFMT_SIGNED)
219 p = sndbuf_getbuf(b);
220 while (i < b->bufsize)
227 sndbuf_reset(struct snd_dbuf *b)
236 if (b->buf && b->bufsize > 0)
237 sndbuf_clear(b, b->bufsize);
241 sndbuf_getfmt(struct snd_dbuf *b)
247 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
251 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
252 b->bps <<= (b->fmt & AFMT_16BIT)? 1 : 0;
253 b->bps <<= (b->fmt & AFMT_32BIT)? 2 : 0;
258 sndbuf_getspd(struct snd_dbuf *b)
264 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
270 sndbuf_getalign(struct snd_dbuf *b)
272 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
274 return align[b->bps - 1];
278 sndbuf_getblkcnt(struct snd_dbuf *b)
284 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
290 sndbuf_getblksz(struct snd_dbuf *b)
296 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
302 sndbuf_getbps(struct snd_dbuf *b)
308 sndbuf_getbuf(struct snd_dbuf *b)
314 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
316 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
322 sndbuf_getsize(struct snd_dbuf *b)
328 sndbuf_getmaxsize(struct snd_dbuf *b)
334 sndbuf_runsz(struct snd_dbuf *b)
340 sndbuf_setrun(struct snd_dbuf *b, int go)
342 b->dl = go? b->blksz : 0;
346 sndbuf_getsel(struct snd_dbuf *b)
351 /************************************************************/
353 sndbuf_getxrun(struct snd_dbuf *b)
355 SNDBUF_LOCKASSERT(b);
361 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
363 SNDBUF_LOCKASSERT(b);
369 sndbuf_gethwptr(struct snd_dbuf *b)
371 SNDBUF_LOCKASSERT(b);
377 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
379 SNDBUF_LOCKASSERT(b);
385 sndbuf_getready(struct snd_dbuf *b)
387 SNDBUF_LOCKASSERT(b);
388 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
394 sndbuf_getreadyptr(struct snd_dbuf *b)
396 SNDBUF_LOCKASSERT(b);
397 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
403 sndbuf_getfree(struct snd_dbuf *b)
405 SNDBUF_LOCKASSERT(b);
406 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
408 return b->bufsize - b->rl;
412 sndbuf_getfreeptr(struct snd_dbuf *b)
414 SNDBUF_LOCKASSERT(b);
415 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
416 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
418 return (b->rp + b->rl) % b->bufsize;
422 sndbuf_getblocks(struct snd_dbuf *b)
424 SNDBUF_LOCKASSERT(b);
426 return b->total / b->blksz;
430 sndbuf_getprevblocks(struct snd_dbuf *b)
432 SNDBUF_LOCKASSERT(b);
434 return b->prev_total / b->blksz;
438 sndbuf_gettotal(struct snd_dbuf *b)
440 SNDBUF_LOCKASSERT(b);
446 sndbuf_updateprevtotal(struct snd_dbuf *b)
448 SNDBUF_LOCKASSERT(b);
450 b->prev_total = b->total;
453 /************************************************************/
456 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
460 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
461 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
465 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
466 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
473 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
479 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
483 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
484 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
487 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
488 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
491 b->rp = (b->rp + l) % b->bufsize;
496 b->rp = (b->rp + count) % b->bufsize;
498 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
504 sndbuf_uiomove(struct snd_dbuf *b, struct uio *uio, unsigned int count)
506 int x, c, p, rd, err;
509 rd = (uio->uio_rw == UIO_READ)? 1 : 0;
510 if (count > uio->uio_resid)
513 if (count > (rd? sndbuf_getready(b) : sndbuf_getfree(b))) {
517 while (err == 0 && count > 0) {
518 p = rd? sndbuf_getreadyptr(b) : sndbuf_getfreeptr(b);
519 c = MIN(count, sndbuf_getsize(b) - p);
521 err = uiomove(sndbuf_getbufofs(b, p), c, uio);
524 x = rd? sndbuf_dispose(b, NULL, x) : sndbuf_acquire(b, NULL, x);
530 /* count is number of bytes we want added to destination buffer */
532 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
534 KASSERT(count > 0, ("can't feed 0 bytes"));
536 if (sndbuf_getfree(to) < count)
539 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
541 sndbuf_acquire(to, to->tmpbuf, count);
542 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
547 /************************************************************/
550 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
554 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
556 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
558 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
560 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
562 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
566 /************************************************************/
568 sndbuf_getflags(struct snd_dbuf *b)
574 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
581 /************************************************************/
584 sndbuf_isadmasetup(struct snd_dbuf *b, struct resource *drq)
586 /* should do isa_dma_acquire/isa_dma_release here */
590 sndbuf_setflags(b, SNDBUF_F_ISADMA, 1);
591 b->isadmachan = rman_get_start(drq);
597 sndbuf_isadmasetdir(struct snd_dbuf *b, int dir)
599 KASSERT(b, ("sndbuf_isadmasetdir called with b == NULL"));
600 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmasetdir called on non-ISA buffer"));
602 b->dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
607 sndbuf_isadma(struct snd_dbuf *b, int go)
609 KASSERT(b, ("sndbuf_isadma called with b == NULL"));
610 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadma called on non-ISA buffer"));
614 /* isa_dmainit(b->chan, size); */
615 isa_dmastart(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
620 isa_dmastop(b->isadmachan);
621 isa_dmadone(b->dir | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
625 DEB(printf("buf 0x%p ISA DMA %s, channel %d\n",
627 (go == PCMTRIG_START)? "started" : "stopped",
632 sndbuf_isadmaptr(struct snd_dbuf *b)
636 KASSERT(b, ("sndbuf_isadmaptr called with b == NULL"));
637 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmaptr called on non-ISA buffer"));
639 if (!sndbuf_runsz(b))
641 i = isa_dmastatus(b->isadmachan);
642 KASSERT(i >= 0, ("isa_dmastatus returned %d", i));
643 return b->bufsize - i;
647 sndbuf_isadmabounce(struct snd_dbuf *b)
649 KASSERT(b, ("sndbuf_isadmabounce called with b == NULL"));
650 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmabounce called on non-ISA buffer"));
652 /* tell isa_dma to bounce data in/out */