1098ca2bdSWarner Losh /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro F. Giffuni * 43f225978SCameron Grant * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 5350a5fafSCameron Grant * All rights reserved. 6350a5fafSCameron Grant * 7350a5fafSCameron Grant * Redistribution and use in source and binary forms, with or without 8350a5fafSCameron Grant * modification, are permitted provided that the following conditions 9350a5fafSCameron Grant * are met: 10350a5fafSCameron Grant * 1. Redistributions of source code must retain the above copyright 11350a5fafSCameron Grant * notice, this list of conditions and the following disclaimer. 12350a5fafSCameron Grant * 2. Redistributions in binary form must reproduce the above copyright 13350a5fafSCameron Grant * notice, this list of conditions and the following disclaimer in the 14350a5fafSCameron Grant * documentation and/or other materials provided with the distribution. 15350a5fafSCameron Grant * 16350a5fafSCameron Grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17350a5fafSCameron Grant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18350a5fafSCameron Grant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19350a5fafSCameron Grant * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20350a5fafSCameron Grant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21350a5fafSCameron Grant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22350a5fafSCameron Grant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23350a5fafSCameron Grant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24350a5fafSCameron Grant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25350a5fafSCameron Grant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26350a5fafSCameron Grant * SUCH DAMAGE. 27350a5fafSCameron Grant * 28350a5fafSCameron Grant * $FreeBSD$ 29350a5fafSCameron Grant */ 30350a5fafSCameron Grant 313febcc57SYoshihiro Takahashi #define SND_DMA(b) (sndbuf_getflags((b)) & SNDBUF_F_DMA) 3266ef8af5SCameron Grant #define SNDBUF_LOCKASSERT(b) 33350a5fafSCameron Grant 343febcc57SYoshihiro Takahashi #define SNDBUF_F_DMA 0x00000001 3566ef8af5SCameron Grant #define SNDBUF_F_XRUN 0x00000002 3666ef8af5SCameron Grant #define SNDBUF_F_RUNNING 0x00000004 379e4c8259SAriff Abdullah #define SNDBUF_F_MANAGED 0x00000008 38350a5fafSCameron Grant 393febcc57SYoshihiro Takahashi #define SNDBUF_NAMELEN 48 403febcc57SYoshihiro Takahashi 413febcc57SYoshihiro Takahashi struct snd_dbuf { 423febcc57SYoshihiro Takahashi device_t dev; 433febcc57SYoshihiro Takahashi u_int8_t *buf, *tmpbuf; 44b611c801SAlexander Leidinger u_int8_t *shadbuf; /**< shadow buffer used w/ S_D_SILENCE/SKIP */ 45b611c801SAlexander Leidinger volatile int sl; /**< shadbuf ready length in # of bytes */ 46504e00afSAriff Abdullah unsigned int bufsize, maxsize, allocsize; 473febcc57SYoshihiro Takahashi volatile int dl; /* transfer size */ 483febcc57SYoshihiro Takahashi volatile int rp; /* pointers to the ready area */ 493febcc57SYoshihiro Takahashi volatile int rl; /* length of ready area */ 503febcc57SYoshihiro Takahashi volatile int hp; 5190da2b28SAriff Abdullah volatile u_int64_t total, prev_total; 523febcc57SYoshihiro Takahashi int dmachan, dir; /* dma channel */ 5390da2b28SAriff Abdullah u_int32_t fmt, spd, bps, align; 543febcc57SYoshihiro Takahashi unsigned int blksz, blkcnt; 553febcc57SYoshihiro Takahashi int xrun; 563febcc57SYoshihiro Takahashi u_int32_t flags; 573febcc57SYoshihiro Takahashi bus_dmamap_t dmamap; 583febcc57SYoshihiro Takahashi bus_dma_tag_t dmatag; 5964a11326SAriff Abdullah bus_addr_t buf_addr; 602e334adfSAriff Abdullah int dmaflags; 613febcc57SYoshihiro Takahashi struct selinfo sel; 6212e524a2SDon Lewis struct pcm_channel *channel; 633febcc57SYoshihiro Takahashi char name[SNDBUF_NAMELEN]; 643febcc57SYoshihiro Takahashi }; 653febcc57SYoshihiro Takahashi 6612e524a2SDon Lewis struct snd_dbuf *sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel); 6766ef8af5SCameron Grant void sndbuf_destroy(struct snd_dbuf *b); 6866ef8af5SCameron Grant 6966ef8af5SCameron Grant void sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what); 7066ef8af5SCameron Grant 712e334adfSAriff Abdullah int sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags, unsigned int size); 7266ef8af5SCameron Grant int sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size); 7366ef8af5SCameron Grant void sndbuf_free(struct snd_dbuf *b); 7466ef8af5SCameron Grant int sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz); 7566ef8af5SCameron Grant int sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz); 7666ef8af5SCameron Grant void sndbuf_reset(struct snd_dbuf *b); 7766ef8af5SCameron Grant void sndbuf_clear(struct snd_dbuf *b, unsigned int length); 78fc60109dSCameron Grant void sndbuf_fillsilence(struct snd_dbuf *b); 79fd578f65SAlexander Motin void sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl); 80b611c801SAlexander Leidinger void sndbuf_softreset(struct snd_dbuf *b); 81b611c801SAlexander Leidinger void sndbuf_clearshadow(struct snd_dbuf *b); 8266ef8af5SCameron Grant 8366ef8af5SCameron Grant u_int32_t sndbuf_getfmt(struct snd_dbuf *b); 8466ef8af5SCameron Grant int sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt); 8566ef8af5SCameron Grant unsigned int sndbuf_getspd(struct snd_dbuf *b); 8666ef8af5SCameron Grant void sndbuf_setspd(struct snd_dbuf *b, unsigned int spd); 8766ef8af5SCameron Grant unsigned int sndbuf_getbps(struct snd_dbuf *b); 8866ef8af5SCameron Grant 8938cc9942SOlivier Houchard bus_addr_t sndbuf_getbufaddr(struct snd_dbuf *buf); 9038cc9942SOlivier Houchard 9166ef8af5SCameron Grant void *sndbuf_getbuf(struct snd_dbuf *b); 9266ef8af5SCameron Grant void *sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs); 9366ef8af5SCameron Grant unsigned int sndbuf_getsize(struct snd_dbuf *b); 9466ef8af5SCameron Grant unsigned int sndbuf_getmaxsize(struct snd_dbuf *b); 95504e00afSAriff Abdullah unsigned int sndbuf_getallocsize(struct snd_dbuf *b); 9666ef8af5SCameron Grant unsigned int sndbuf_getalign(struct snd_dbuf *b); 9766ef8af5SCameron Grant unsigned int sndbuf_getblkcnt(struct snd_dbuf *b); 9866ef8af5SCameron Grant void sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt); 9966ef8af5SCameron Grant unsigned int sndbuf_getblksz(struct snd_dbuf *b); 10066ef8af5SCameron Grant void sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz); 10166ef8af5SCameron Grant unsigned int sndbuf_runsz(struct snd_dbuf *b); 10266ef8af5SCameron Grant void sndbuf_setrun(struct snd_dbuf *b, int go); 10366ef8af5SCameron Grant struct selinfo *sndbuf_getsel(struct snd_dbuf *b); 10466ef8af5SCameron Grant 10566ef8af5SCameron Grant unsigned int sndbuf_getxrun(struct snd_dbuf *b); 106d55d96f6SAlexander Leidinger void sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun); 10766ef8af5SCameron Grant unsigned int sndbuf_gethwptr(struct snd_dbuf *b); 10866ef8af5SCameron Grant void sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr); 10966ef8af5SCameron Grant unsigned int sndbuf_getfree(struct snd_dbuf *b); 11066ef8af5SCameron Grant unsigned int sndbuf_getfreeptr(struct snd_dbuf *b); 11166ef8af5SCameron Grant unsigned int sndbuf_getready(struct snd_dbuf *b); 11266ef8af5SCameron Grant unsigned int sndbuf_getreadyptr(struct snd_dbuf *b); 11390da2b28SAriff Abdullah u_int64_t sndbuf_getblocks(struct snd_dbuf *b); 11490da2b28SAriff Abdullah u_int64_t sndbuf_getprevblocks(struct snd_dbuf *b); 11590da2b28SAriff Abdullah u_int64_t sndbuf_gettotal(struct snd_dbuf *b); 11690da2b28SAriff Abdullah u_int64_t sndbuf_getprevtotal(struct snd_dbuf *b); 117a580b31aSAriff Abdullah unsigned int sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to); 118a580b31aSAriff Abdullah u_int8_t sndbuf_zerodata(u_int32_t fmt); 11966ef8af5SCameron Grant void sndbuf_updateprevtotal(struct snd_dbuf *b); 12066ef8af5SCameron Grant 12166ef8af5SCameron Grant int sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count); 12266ef8af5SCameron Grant int sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count); 12366ef8af5SCameron Grant int sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count); 12466ef8af5SCameron Grant 12566ef8af5SCameron Grant u_int32_t sndbuf_getflags(struct snd_dbuf *b); 12666ef8af5SCameron Grant void sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on); 12766ef8af5SCameron Grant 1283febcc57SYoshihiro Takahashi int sndbuf_dmasetup(struct snd_dbuf *b, struct resource *drq); 1293febcc57SYoshihiro Takahashi int sndbuf_dmasetdir(struct snd_dbuf *b, int dir); 1303febcc57SYoshihiro Takahashi void sndbuf_dma(struct snd_dbuf *b, int go); 1313febcc57SYoshihiro Takahashi int sndbuf_dmaptr(struct snd_dbuf *b); 1323febcc57SYoshihiro Takahashi void sndbuf_dmabounce(struct snd_dbuf *b); 133b611c801SAlexander Leidinger 134b611c801SAlexander Leidinger #ifdef OSSV4_EXPERIMENT 135b611c801SAlexander Leidinger void sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp); 136b611c801SAlexander Leidinger #endif 137c909aebcSJung-uk Kim 138c909aebcSJung-uk Kim static inline u_int32_t 139c909aebcSJung-uk Kim snd_xbytes(u_int32_t v, u_int32_t from, u_int32_t to) 140c909aebcSJung-uk Kim { 141c909aebcSJung-uk Kim 142c909aebcSJung-uk Kim if (from == to) 143c909aebcSJung-uk Kim return (v); 144c909aebcSJung-uk Kim if (from == 0) 145c909aebcSJung-uk Kim return (0); 146c909aebcSJung-uk Kim return ((u_int64_t)v * to / from); 147c909aebcSJung-uk Kim } 148