xref: /freebsd/sys/dev/sound/pcm/buffer.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #define SNDBUF_LOCKASSERT(b)
30 
31 #define	SNDBUF_F_MANAGED	0x00000008
32 
33 #define SNDBUF_NAMELEN	48
34 
35 struct snd_dbuf {
36 	device_t dev;
37 	u_int8_t *buf, *tmpbuf;
38 	u_int8_t *shadbuf; /**< shadow buffer used w/ S_D_SILENCE/SKIP */
39 	volatile int sl; /**< shadbuf ready length in # of bytes */
40 	unsigned int bufsize, maxsize, allocsize;
41 	volatile int dl; /* transfer size */
42 	volatile int rp; /* pointers to the ready area */
43 	volatile int rl; /* length of ready area */
44 	volatile int hp;
45 	volatile u_int64_t total, prev_total;
46 	int dmachan, dir;       /* dma channel */
47 	u_int32_t fmt, spd, bps, align;
48 	unsigned int blksz, blkcnt;
49 	int xrun;
50 	u_int32_t flags;
51 	bus_dmamap_t dmamap;
52 	bus_dma_tag_t dmatag;
53 	bus_addr_t buf_addr;
54 	int dmaflags;
55 	struct selinfo sel;
56 	struct pcm_channel *channel;
57 	char name[SNDBUF_NAMELEN];
58 };
59 
60 struct snd_dbuf *sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel);
61 void sndbuf_destroy(struct snd_dbuf *b);
62 
63 void sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what);
64 
65 int sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, int dmaflags, unsigned int size);
66 int sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size);
67 void sndbuf_free(struct snd_dbuf *b);
68 int sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz);
69 int sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz);
70 void sndbuf_reset(struct snd_dbuf *b);
71 void sndbuf_clear(struct snd_dbuf *b, unsigned int length);
72 void sndbuf_fillsilence(struct snd_dbuf *b);
73 void sndbuf_fillsilence_rl(struct snd_dbuf *b, u_int rl);
74 void sndbuf_softreset(struct snd_dbuf *b);
75 void sndbuf_clearshadow(struct snd_dbuf *b);
76 
77 u_int32_t sndbuf_getfmt(struct snd_dbuf *b);
78 int sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt);
79 unsigned int sndbuf_getspd(struct snd_dbuf *b);
80 void sndbuf_setspd(struct snd_dbuf *b, unsigned int spd);
81 unsigned int sndbuf_getbps(struct snd_dbuf *b);
82 
83 bus_addr_t sndbuf_getbufaddr(struct snd_dbuf *buf);
84 
85 void *sndbuf_getbuf(struct snd_dbuf *b);
86 void *sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs);
87 unsigned int sndbuf_getsize(struct snd_dbuf *b);
88 unsigned int sndbuf_getmaxsize(struct snd_dbuf *b);
89 unsigned int sndbuf_getallocsize(struct snd_dbuf *b);
90 unsigned int sndbuf_getalign(struct snd_dbuf *b);
91 unsigned int sndbuf_getblkcnt(struct snd_dbuf *b);
92 void sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt);
93 unsigned int sndbuf_getblksz(struct snd_dbuf *b);
94 void sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz);
95 unsigned int sndbuf_runsz(struct snd_dbuf *b);
96 void sndbuf_setrun(struct snd_dbuf *b, int go);
97 struct selinfo *sndbuf_getsel(struct snd_dbuf *b);
98 
99 unsigned int sndbuf_getxrun(struct snd_dbuf *b);
100 void sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun);
101 unsigned int sndbuf_gethwptr(struct snd_dbuf *b);
102 void sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr);
103 unsigned int sndbuf_getfree(struct snd_dbuf *b);
104 unsigned int sndbuf_getfreeptr(struct snd_dbuf *b);
105 unsigned int sndbuf_getready(struct snd_dbuf *b);
106 unsigned int sndbuf_getreadyptr(struct snd_dbuf *b);
107 u_int64_t sndbuf_getblocks(struct snd_dbuf *b);
108 u_int64_t sndbuf_getprevblocks(struct snd_dbuf *b);
109 u_int64_t sndbuf_gettotal(struct snd_dbuf *b);
110 u_int64_t sndbuf_getprevtotal(struct snd_dbuf *b);
111 unsigned int sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to);
112 u_int8_t sndbuf_zerodata(u_int32_t fmt);
113 void sndbuf_updateprevtotal(struct snd_dbuf *b);
114 
115 int sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count);
116 int sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count);
117 int sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count);
118 
119 u_int32_t sndbuf_getflags(struct snd_dbuf *b);
120 void sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on);
121 
122 #ifdef OSSV4_EXPERIMENT
123 void sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp);
124 #endif
125 
126 static inline u_int32_t
127 snd_xbytes(u_int32_t v, u_int32_t from, u_int32_t to)
128 {
129 
130 	if (from == to)
131 		return (v);
132 	if (from == 0)
133 		return (0);
134 	return ((u_int64_t)v * to / from);
135 }
136