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.5 2006/09/05 00:55:43 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.5 2006/09/05 00:55:43 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; /* dma channel */
47 u_int32_t fmt, spd, bps;
48 unsigned int blksz, blkcnt;
55 char name[SNDBUF_NAMELEN];
59 sndbuf_create(device_t dev, char *drv, char *desc)
63 b = kmalloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
64 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
71 sndbuf_destroy(struct snd_dbuf *b)
77 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
79 struct snd_dbuf *b = (struct snd_dbuf *)arg;
82 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", (unsigned long)segs->ds_addr,
83 (unsigned long)segs->ds_len);
84 printf("%p -> %lx\n", b->buf, (unsigned long)vtophys(b->buf));
89 * Allocate memory for DMA buffer. If the device does not use DMA transfers,
90 * the driver can call malloc(9) and sndbuf_setup() itself.
93 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
97 b->bufsize = b->maxsize;
98 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, &b->dmamap))
100 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, sndbuf_setmap, b, 0))
102 return sndbuf_resize(b, 2, b->maxsize / 2);
106 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
110 b->bufsize = b->maxsize;
111 return sndbuf_resize(b, 2, b->maxsize / 2);
115 sndbuf_free(struct snd_dbuf *b)
118 kfree(b->tmpbuf, M_DEVBUF);
122 bus_dmamap_unload(b->dmatag, b->dmamap);
124 if (b->dmamap && b->buf)
125 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
131 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
139 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize))
141 if (blkcnt == b->blkcnt && blksz == b->blksz)
145 b->bufsize = blkcnt * blksz;
147 kfree(b->tmpbuf, M_DEVBUF);
148 b->tmpbuf = kmalloc(b->bufsize, M_DEVBUF, M_WAITOK);
154 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
156 if (blkcnt < 2 || blksz < 16)
162 b->maxsize = blkcnt * blksz;
163 b->bufsize = b->maxsize;
166 kfree(b->buf, M_DEVBUF);
167 b->buf = kmalloc(b->bufsize, M_DEVBUF, M_WAITOK);
172 kfree(b->tmpbuf, M_DEVBUF);
173 b->tmpbuf = kmalloc(b->bufsize, M_DEVBUF, M_WAITOK);
174 if (b->tmpbuf == NULL)
182 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
189 if (length > b->bufsize)
192 if (b->fmt & AFMT_SIGNED)
197 i = sndbuf_getfreeptr(b);
198 p = sndbuf_getbuf(b);
209 sndbuf_fillsilence(struct snd_dbuf *b)
214 if (b->fmt & AFMT_SIGNED)
220 p = sndbuf_getbuf(b);
221 while (i < b->bufsize)
228 sndbuf_reset(struct snd_dbuf *b)
237 if (b->buf && b->bufsize > 0)
238 sndbuf_clear(b, b->bufsize);
242 sndbuf_getfmt(struct snd_dbuf *b)
248 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
252 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
253 b->bps <<= (b->fmt & AFMT_16BIT)? 1 : 0;
254 b->bps <<= (b->fmt & AFMT_32BIT)? 2 : 0;
259 sndbuf_getspd(struct snd_dbuf *b)
265 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
271 sndbuf_getalign(struct snd_dbuf *b)
273 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
275 return align[b->bps - 1];
279 sndbuf_getblkcnt(struct snd_dbuf *b)
285 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
291 sndbuf_getblksz(struct snd_dbuf *b)
297 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
303 sndbuf_getbps(struct snd_dbuf *b)
309 sndbuf_getbuf(struct snd_dbuf *b)
315 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
317 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
323 sndbuf_getsize(struct snd_dbuf *b)
329 sndbuf_getmaxsize(struct snd_dbuf *b)
335 sndbuf_runsz(struct snd_dbuf *b)
341 sndbuf_setrun(struct snd_dbuf *b, int go)
343 b->dl = go? b->blksz : 0;
347 sndbuf_getsel(struct snd_dbuf *b)
352 /************************************************************/
354 sndbuf_getxrun(struct snd_dbuf *b)
356 SNDBUF_LOCKASSERT(b);
362 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt)
364 SNDBUF_LOCKASSERT(b);
370 sndbuf_gethwptr(struct snd_dbuf *b)
372 SNDBUF_LOCKASSERT(b);
378 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
380 SNDBUF_LOCKASSERT(b);
386 sndbuf_getready(struct snd_dbuf *b)
388 SNDBUF_LOCKASSERT(b);
389 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
395 sndbuf_getreadyptr(struct snd_dbuf *b)
397 SNDBUF_LOCKASSERT(b);
398 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
404 sndbuf_getfree(struct snd_dbuf *b)
406 SNDBUF_LOCKASSERT(b);
407 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
409 return b->bufsize - b->rl;
413 sndbuf_getfreeptr(struct snd_dbuf *b)
415 SNDBUF_LOCKASSERT(b);
416 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
417 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
419 return (b->rp + b->rl) % b->bufsize;
423 sndbuf_getblocks(struct snd_dbuf *b)
425 SNDBUF_LOCKASSERT(b);
427 return b->total / b->blksz;
431 sndbuf_getprevblocks(struct snd_dbuf *b)
433 SNDBUF_LOCKASSERT(b);
435 return b->prev_total / b->blksz;
439 sndbuf_gettotal(struct snd_dbuf *b)
441 SNDBUF_LOCKASSERT(b);
447 sndbuf_updateprevtotal(struct snd_dbuf *b)
449 SNDBUF_LOCKASSERT(b);
451 b->prev_total = b->total;
454 /************************************************************/
457 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
461 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
462 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
466 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
467 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
474 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
480 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
484 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
485 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
488 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
489 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
492 b->rp = (b->rp + l) % b->bufsize;
497 b->rp = (b->rp + count) % b->bufsize;
499 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
505 sndbuf_uiomove(struct snd_dbuf *b, struct uio *uio, unsigned int count)
507 int x, c, p, rd, err;
510 rd = (uio->uio_rw == UIO_READ)? 1 : 0;
511 if (count > uio->uio_resid)
514 if (count > (rd? sndbuf_getready(b) : sndbuf_getfree(b))) {
518 while (err == 0 && count > 0) {
519 p = rd? sndbuf_getreadyptr(b) : sndbuf_getfreeptr(b);
520 c = MIN(count, sndbuf_getsize(b) - p);
522 err = uiomove(sndbuf_getbufofs(b, p), c, uio);
525 x = rd? sndbuf_dispose(b, NULL, x) : sndbuf_acquire(b, NULL, x);
531 /* count is number of bytes we want added to destination buffer */
533 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
535 KASSERT(count > 0, ("can't feed 0 bytes"));
537 if (sndbuf_getfree(to) < count)
540 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
542 sndbuf_acquire(to, to->tmpbuf, count);
543 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
548 /************************************************************/
551 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
555 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
557 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
559 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
561 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
563 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
567 /************************************************************/
569 sndbuf_getflags(struct snd_dbuf *b)
575 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
582 /************************************************************/
585 sndbuf_isadmasetup(struct snd_dbuf *b, struct resource *drq)
587 /* should do isa_dma_acquire/isa_dma_release here */
591 sndbuf_setflags(b, SNDBUF_F_ISADMA, 1);
592 b->isadmachan = rman_get_start(drq);
598 sndbuf_isadmasetdir(struct snd_dbuf *b, int dir)
600 KASSERT(b, ("sndbuf_isadmasetdir called with b == NULL"));
601 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmasetdir called on non-ISA buffer"));
603 b->dmaflags = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ;
608 sndbuf_isadma(struct snd_dbuf *b, int go)
610 KASSERT(b, ("sndbuf_isadma called with b == NULL"));
611 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadma called on non-ISA buffer"));
615 /* isa_dmainit(b->chan, size); */
616 isa_dmastart(b->dmaflags | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
621 isa_dmastop(b->isadmachan);
622 isa_dmadone(b->dmaflags | ISADMA_RAW, b->buf, b->bufsize, b->isadmachan);
626 DEB(printf("buf 0x%p ISA DMA %s, channel %d\n",
628 (go == PCMTRIG_START)? "started" : "stopped",
633 sndbuf_isadmaptr(struct snd_dbuf *b)
637 KASSERT(b, ("sndbuf_isadmaptr called with b == NULL"));
638 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmaptr called on non-ISA buffer"));
640 if (!sndbuf_runsz(b))
642 i = isa_dmastatus(b->isadmachan);
643 KASSERT(i >= 0, ("isa_dmastatus returned %d", i));
644 return b->bufsize - i;
648 sndbuf_isadmabounce(struct snd_dbuf *b)
650 KASSERT(b, ("sndbuf_isadmabounce called with b == NULL"));
651 KASSERT(sndbuf_getflags(b) & SNDBUF_F_ISADMA, ("sndbuf_isadmabounce called on non-ISA buffer"));
653 /* tell isa_dma to bounce data in/out */