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