xref: /src/sys/dev/sound/pcm/sound.h (revision bafa8c951d8cfaec75e774af03d7ab6fadb448b9)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
6  * Copyright (c) 1995 Hannu Savolainen
7  * All rights reserved.
8  * Copyright (c) 2024-2025 The FreeBSD Foundation
9  *
10  * Portions of this software were developed by Christos Margiolis
11  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * first, include kernel header files.
37  */
38 
39 #ifndef _OS_H_
40 #define _OS_H_
41 
42 #ifdef _KERNEL
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/eventhandler.h>
46 #include <sys/ioccom.h>
47 #include <sys/filio.h>
48 #include <sys/sockio.h>
49 #include <sys/fcntl.h>
50 #include <sys/selinfo.h>
51 #include <sys/proc.h>
52 #include <sys/kernel.h> /* for DATA_SET */
53 #include <sys/module.h>
54 #include <sys/conf.h>
55 #include <sys/file.h>
56 #include <sys/uio.h>
57 #include <sys/syslog.h>
58 #include <sys/errno.h>
59 #include <sys/malloc.h>
60 #include <sys/bus.h>
61 #include <machine/resource.h>
62 #include <machine/bus.h>
63 #include <sys/rman.h>
64 #include <sys/limits.h>
65 #include <sys/mman.h>
66 #include <sys/poll.h>
67 #include <sys/sbuf.h>
68 #include <sys/soundcard.h>
69 #include <sys/sysctl.h>
70 #include <sys/kobj.h>
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 
74 #include <sys/lock.h>
75 #include <sys/mutex.h>
76 #include <sys/condvar.h>
77 
78 struct pcm_channel;
79 struct pcm_feeder;
80 struct snd_dbuf;
81 struct snd_mixer;
82 
83 #include <dev/sound/pcm/buffer.h>
84 #include <dev/sound/pcm/matrix.h>
85 #include <dev/sound/pcm/channel.h>
86 #include <dev/sound/pcm/feeder.h>
87 #include <dev/sound/pcm/mixer.h>
88 #include <dev/sound/pcm/dsp.h>
89 
90 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
91 
92 #define SND_STATUSLEN	64
93 
94 #define SOUND_MODVER	5
95 
96 #define SOUND_MINVER	SOUND_MODVER
97 #define SOUND_PREFVER	SOUND_MODVER
98 #define SOUND_MAXVER	SOUND_MODVER
99 
100 #define SD_F_SIMPLEX		0x00000001
101 /* unused			0x00000002 */
102 #define SD_F_SOFTPCMVOL		0x00000004
103 #define SD_F_BUSY		0x00000008
104 #define SD_F_MPSAFE		0x00000010
105 #define SD_F_REGISTERED		0x00000020
106 #define SD_F_BITPERFECT		0x00000040
107 #define SD_F_VPC		0x00000080	/* volume-per-channel */
108 #define SD_F_EQ			0x00000100	/* EQ */
109 #define SD_F_EQ_ENABLED		0x00000200	/* EQ enabled */
110 #define SD_F_EQ_BYPASSED	0x00000400	/* EQ bypassed */
111 #define SD_F_EQ_PC		0x00000800	/* EQ per-channel */
112 #define SD_F_PVCHANS		0x00001000	/* Playback vchans enabled */
113 #define SD_F_RVCHANS		0x00002000	/* Recording vchans enabled */
114 
115 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
116 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
117 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
118 
119 #define SD_F_BITS		"\020"					\
120 				"\001SIMPLEX"				\
121 				/* "\002 */				\
122 				"\003SOFTPCMVOL"			\
123 				"\004BUSY"				\
124 				"\005MPSAFE"				\
125 				"\006REGISTERED"			\
126 				"\007BITPERFECT"			\
127 				"\010VPC"				\
128 				"\011EQ"				\
129 				"\012EQ_ENABLED"			\
130 				"\013EQ_BYPASSED"			\
131 				"\014EQ_PC"				\
132 				"\015PVCHANS"				\
133 				"\016RVCHANS"
134 
135 #define PCM_REGISTERED(x)	\
136 	((x) != NULL && ((x)->flags & SD_F_REGISTERED))
137 
138 #define	PCM_MAXCHANS		10000
139 #define	PCM_CHANCOUNT(d)	\
140 	(d->playcount + d->pvchancount + d->reccount + d->rvchancount)
141 
142 /* many variables should be reduced to a range. Here define a macro */
143 #define RANGE(var, low, high) (var) = \
144 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
145 
146 extern int snd_unit;
147 extern int snd_verbose;
148 extern devclass_t pcm_devclass;
149 extern struct unrhdr *pcmsg_unrhdr;
150 
151 #ifndef DEB
152 #define DEB(x)
153 #endif
154 
155 SYSCTL_DECL(_hw_snd);
156 
157 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
158 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
159 void pcm_init(device_t dev, void *devinfo);
160 int pcm_register(device_t dev, char *str);
161 int pcm_unregister(device_t dev);
162 u_int32_t pcm_getflags(device_t dev);
163 void pcm_setflags(device_t dev, u_int32_t val);
164 void *pcm_getdevinfo(device_t dev);
165 
166 int snd_setup_intr(device_t dev, struct resource *res, int flags,
167 		   driver_intr_t hand, void *param, void **cookiep);
168 
169 /* These are the function codes assigned to the children of sound cards. */
170 enum {
171 	SCF_PCM,
172 	SCF_MIDI,
173 };
174 
175 /*
176  * This is the device information struct, used by a bridge device to pass the
177  * device function code to the children.
178  */
179 struct sndcard_func {
180 	int func;	/* The function code. */
181 	void *varinfo;	/* Bridge-specific information. */
182 };
183 
184 /*
185  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
186  * so that the macro versions of pcm_{,un}lock can dereference them.
187  * we also have to do this now makedev() has gone away.
188  */
189 
190 struct snddev_info {
191 	struct {
192 		struct {
193 			SLIST_HEAD(, pcm_channel) head;
194 			struct {
195 				SLIST_HEAD(, pcm_channel) head;
196 			} busy;
197 			struct {
198 				SLIST_HEAD(, pcm_channel) head;
199 			} opened;
200 			struct {
201 				SLIST_HEAD(, pcm_channel) head;
202 			} primary;
203 		} pcm;
204 	} channels;
205 	unsigned playcount, reccount, pvchancount, rvchancount;
206 	unsigned flags;
207 	unsigned int bufsz;
208 	void *devinfo;
209 	device_t dev;
210 	char status[SND_STATUSLEN];
211 	struct mtx lock;
212 	struct cdev *mixer_dev;
213 	struct cdev *dsp_dev;
214 	uint32_t pvchanrate, pvchanformat, pvchanmode;
215 	uint32_t rvchanrate, rvchanformat, rvchanmode;
216 	int32_t eqpreamp;
217 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
218 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
219 	struct cv cv;
220 	struct unrhdr *p_unr;
221 	struct unrhdr *vp_unr;
222 	struct unrhdr *r_unr;
223 	struct unrhdr *vr_unr;
224 };
225 
226 void	sound_oss_sysinfo(oss_sysinfo *);
227 int	sound_oss_card_info(oss_card_info *);
228 
229 #define	PCM_MODE_MIXER		0x01
230 #define	PCM_MODE_PLAY		0x02
231 #define	PCM_MODE_REC		0x04
232 
233 #define PCM_LOCKOWNED(d)	mtx_owned(&(d)->lock)
234 #define	PCM_LOCK(d)		mtx_lock(&(d)->lock)
235 #define	PCM_UNLOCK(d)		mtx_unlock(&(d)->lock)
236 #define PCM_TRYLOCK(d)		mtx_trylock(&(d)->lock)
237 #define PCM_LOCKASSERT(d)	mtx_assert(&(d)->lock, MA_OWNED)
238 #define PCM_UNLOCKASSERT(d)	mtx_assert(&(d)->lock, MA_NOTOWNED)
239 
240 /*
241  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
242  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
243  */
244 #define PCM_WAIT(x)		do {					\
245 	PCM_LOCKASSERT(x);						\
246 	while ((x)->flags & SD_F_BUSY)					\
247 		cv_wait(&(x)->cv, &(x)->lock);				\
248 } while (0)
249 
250 #define PCM_ACQUIRE(x)		do {					\
251 	PCM_LOCKASSERT(x);						\
252 	KASSERT(!((x)->flags & SD_F_BUSY),				\
253 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
254 	    __func__, __LINE__));					\
255 	(x)->flags |= SD_F_BUSY;					\
256 } while (0)
257 
258 #define PCM_RELEASE(x)		do {					\
259 	PCM_LOCKASSERT(x);						\
260 	KASSERT((x)->flags & SD_F_BUSY,					\
261 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
262 	    __func__, __LINE__));					\
263 	(x)->flags &= ~SD_F_BUSY;					\
264 	cv_broadcast(&(x)->cv);						\
265 } while (0)
266 
267 /* Quick version, for shorter path. */
268 #define PCM_ACQUIRE_QUICK(x)	do {					\
269 	PCM_UNLOCKASSERT(x);						\
270 	PCM_LOCK(x);							\
271 	PCM_WAIT(x);							\
272 	PCM_ACQUIRE(x);							\
273 	PCM_UNLOCK(x);							\
274 } while (0)
275 
276 #define PCM_RELEASE_QUICK(x)	do {					\
277 	PCM_UNLOCKASSERT(x);						\
278 	PCM_LOCK(x);							\
279 	PCM_RELEASE(x);							\
280 	PCM_UNLOCK(x);							\
281 } while (0)
282 
283 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
284 				    ((x)->flags & SD_F_BUSY),		\
285 				    ("%s(%d): [PCM BUSYASSERT] "	\
286 				    "Failed, snddev_info=%p",		\
287 				    __func__, __LINE__, x))
288 
289 #define PCM_GIANT_ENTER(x)	do {					\
290 	int _pcm_giant = 0;						\
291 	PCM_UNLOCKASSERT(x);						\
292 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
293 		do {							\
294 			mtx_lock(&Giant);				\
295 			_pcm_giant = 1;					\
296 		} while (0)
297 
298 #define PCM_GIANT_EXIT(x)	do {					\
299 	PCM_UNLOCKASSERT(x);						\
300 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
301 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
302 	    __func__, __LINE__));					\
303 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
304 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
305 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
306 	    __func__, __LINE__));					\
307 	if (_pcm_giant != 0) {						\
308 		mtx_assert(&Giant, MA_OWNED);				\
309 		_pcm_giant = 0;						\
310 		mtx_unlock(&Giant);					\
311 	}								\
312 } while (0)
313 
314 #define PCM_GIANT_LEAVE(x)						\
315 	PCM_GIANT_EXIT(x);						\
316 } while (0)
317 
318 #endif /* _KERNEL */
319 
320 /* make figuring out what a format is easier. got AFMT_STEREO already */
321 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE | \
322 			AFMT_F32_LE | AFMT_F32_BE)
323 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
324 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
325 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
326 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
327 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_F32_LE | AFMT_F32_BE | \
328 			AFMT_S24_LE | AFMT_S24_BE | \
329 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
330 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_F32_BE | \
331 			AFMT_S24_BE | AFMT_U24_BE | AFMT_S16_BE | AFMT_U16_BE)
332 
333 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
334 				 AFMT_32BIT)
335 
336 /* Supported vchan mixing formats */
337 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
338 
339 #define AFMT_PASSTHROUGH		AFMT_AC3
340 #define AFMT_PASSTHROUGH_RATE		48000
341 #define AFMT_PASSTHROUGH_CHANNEL	2
342 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
343 
344 /*
345  * We're simply using unused, contiguous bits from various AFMT_ definitions.
346  * ~(0xb00ff7ff)
347  */
348 #define AFMT_ENCODING_MASK	0xf00fffff
349 #define AFMT_CHANNEL_MASK	0x07f00000
350 #define AFMT_CHANNEL_SHIFT	20
351 #define AFMT_CHANNEL_MAX	0x7f
352 #define AFMT_EXTCHANNEL_MASK	0x08000000
353 #define AFMT_EXTCHANNEL_SHIFT	27
354 #define AFMT_EXTCHANNEL_MAX	1
355 
356 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
357 
358 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
359 				AFMT_EXTCHANNEL_SHIFT)
360 
361 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
362 				AFMT_CHANNEL_SHIFT)
363 
364 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
365 				(((v) & AFMT_24BIT) ? 24 :		\
366 				((((v) & AFMT_16BIT) ||			\
367 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
368 
369 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
370 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
371 
372 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
373 				(((c) << AFMT_CHANNEL_SHIFT) &		\
374 				AFMT_CHANNEL_MASK) |			\
375 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
376 				AFMT_EXTCHANNEL_MASK))
377 
378 #define AFMT_U8_NE	AFMT_U8
379 #define AFMT_S8_NE	AFMT_S8
380 
381 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | \
382 			AFMT_S32_NE | AFMT_F32_NE)
383 
384 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
385 			 AFMT_U24_NE | AFMT_U32_NE)
386 
387 #endif	/* _OS_H_ */
388