xref: /freebsd/sys/dev/sound/pcm/sound.h (revision 52c2bb75163559a6e2866ad374a7de67a4ea1273)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 /*
34  * first, include kernel header files.
35  */
36 
37 #ifndef _OS_H_
38 #define _OS_H_
39 
40 #ifdef _KERNEL
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/eventhandler.h>
44 #include <sys/ioccom.h>
45 #include <sys/filio.h>
46 #include <sys/sockio.h>
47 #include <sys/fcntl.h>
48 #include <sys/selinfo.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h> /* for DATA_SET */
51 #include <sys/module.h>
52 #include <sys/conf.h>
53 #include <sys/file.h>
54 #include <sys/uio.h>
55 #include <sys/syslog.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/bus.h>
59 #include <machine/resource.h>
60 #include <machine/bus.h>
61 #include <sys/rman.h>
62 #include <sys/limits.h>
63 #include <sys/mman.h>
64 #include <sys/poll.h>
65 #include <sys/sbuf.h>
66 #include <sys/soundcard.h>
67 #include <sys/sysctl.h>
68 #include <sys/kobj.h>
69 #include <vm/vm.h>
70 #include <vm/pmap.h>
71 
72 #include <sys/lock.h>
73 #include <sys/mutex.h>
74 #include <sys/condvar.h>
75 
76 #ifndef KOBJMETHOD_END
77 #define KOBJMETHOD_END	{ NULL, NULL }
78 #endif
79 
80 struct pcm_channel;
81 struct pcm_feeder;
82 struct snd_dbuf;
83 struct snd_mixer;
84 
85 #include <dev/sound/pcm/buffer.h>
86 #include <dev/sound/pcm/matrix.h>
87 #include <dev/sound/pcm/matrix_map.h>
88 #include <dev/sound/pcm/channel.h>
89 #include <dev/sound/pcm/feeder.h>
90 #include <dev/sound/pcm/mixer.h>
91 #include <dev/sound/pcm/dsp.h>
92 #include <dev/sound/clone.h>
93 #include <dev/sound/unit.h>
94 
95 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
96 
97 #define SND_STATUSLEN	64
98 
99 #define SOUND_MODVER	5
100 
101 #define SOUND_MINVER	SOUND_MODVER
102 #define SOUND_PREFVER	SOUND_MODVER
103 #define SOUND_MAXVER	SOUND_MODVER
104 
105 /*
106  * We're abusing the fact that MAXMINOR still have enough room
107  * for our bit twiddling and nobody ever need 512 unique soundcards,
108  * 32 unique device types and 1024 unique cloneable devices for the
109  * next 100 years...
110  */
111 
112 #define PCMMAXUNIT		(snd_max_u())
113 #define PCMMAXDEV		(snd_max_d())
114 #define PCMMAXCHAN		(snd_max_c())
115 
116 #define PCMMAXCLONE		PCMMAXCHAN
117 
118 #define PCMUNIT(x)		(snd_unit2u(dev2unit(x)))
119 #define PCMDEV(x)		(snd_unit2d(dev2unit(x)))
120 #define PCMCHAN(x)		(snd_unit2c(dev2unit(x)))
121 
122 /* XXX unit2minor compat */
123 #define PCMMINOR(x)	(x)
124 
125 /*
126  * By design, limit possible channels for each direction.
127  */
128 #define SND_MAXHWCHAN		256
129 #define SND_MAXVCHANS		SND_MAXHWCHAN
130 
131 #define SD_F_SIMPLEX		0x00000001
132 #define SD_F_AUTOVCHAN		0x00000002
133 #define SD_F_SOFTPCMVOL		0x00000004
134 /*
135  * Obsolete due to better matrixing
136  */
137 #if 0
138 #define SD_F_PSWAPLR		0x00000008
139 #define SD_F_RSWAPLR		0x00000010
140 #endif
141 #define SD_F_DYING		0x00000008
142 #define SD_F_SUICIDE		0x00000010
143 #define SD_F_BUSY		0x00000020
144 #define SD_F_MPSAFE		0x00000040
145 #define SD_F_REGISTERED		0x00000080
146 #define SD_F_BITPERFECT		0x00000100
147 #define SD_F_VPC		0x00000200	/* volume-per-channel */
148 #define SD_F_EQ			0x00000400	/* EQ */
149 #define SD_F_EQ_ENABLED		0x00000800	/* EQ enabled */
150 #define SD_F_EQ_BYPASSED	0x00001000	/* EQ bypassed */
151 #define SD_F_EQ_PC		0x00002000	/* EQ per-channel */
152 
153 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
154 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
155 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
156 
157 #define SD_F_PRIO_RD		0x10000000
158 #define SD_F_PRIO_WR		0x20000000
159 #define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
160 #define SD_F_DIR_SET		0x40000000
161 #define SD_F_TRANSIENT		0xf0000000
162 
163 #define SD_F_BITS		"\020"					\
164 				"\001SIMPLEX"				\
165 				"\002AUTOVCHAN"				\
166 				"\003SOFTPCMVOL"			\
167 				"\004DYING"				\
168 				"\005SUICIDE"				\
169 				"\006BUSY"				\
170 				"\007MPSAFE"				\
171 				"\010REGISTERED"			\
172 				"\011BITPERFECT"			\
173 				"\012VPC"				\
174 				"\013EQ"				\
175 				"\014EQ_ENABLED"			\
176 				"\015EQ_BYPASSED"			\
177 				"\016EQ_PC"				\
178 				"\035PRIO_RD"				\
179 				"\036PRIO_WR"				\
180 				"\037DIR_SET"
181 
182 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL &&	\
183 				 !((x)->flags & SD_F_DYING))
184 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) &&			\
185 				 ((x)->flags & SD_F_REGISTERED))
186 
187 /* many variables should be reduced to a range. Here define a macro */
188 #define RANGE(var, low, high) (var) = \
189 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
190 #define DSP_BUFFSIZE (8192)
191 
192 /* make figuring out what a format is easier. got AFMT_STEREO already */
193 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
194 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
195 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
196 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
197 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
198 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
199 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
200 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
201 			AFMT_S16_BE | AFMT_U16_BE)
202 
203 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
204 				 AFMT_32BIT)
205 
206 /* Supported vchan mixing formats */
207 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
208 
209 #define AFMT_PASSTHROUGH		AFMT_AC3
210 #define AFMT_PASSTHROUGH_RATE		48000
211 #define AFMT_PASSTHROUGH_CHANNEL	2
212 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
213 
214 /*
215  * We're simply using unused, contiguous bits from various AFMT_ definitions.
216  * ~(0xb00ff7ff)
217  */
218 #define AFMT_ENCODING_MASK	0xf00fffff
219 #define AFMT_CHANNEL_MASK	0x07f00000
220 #define AFMT_CHANNEL_SHIFT	20
221 #define AFMT_CHANNEL_MAX	0x7f
222 #define AFMT_EXTCHANNEL_MASK	0x08000000
223 #define AFMT_EXTCHANNEL_SHIFT	27
224 #define AFMT_EXTCHANNEL_MAX	1
225 
226 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
227 
228 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
229 				AFMT_EXTCHANNEL_SHIFT)
230 
231 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
232 				AFMT_CHANNEL_SHIFT)
233 
234 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
235 				(((v) & AFMT_24BIT) ? 24 :		\
236 				((((v) & AFMT_16BIT) ||			\
237 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
238 
239 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
240 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
241 
242 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
243 				(((c) << AFMT_CHANNEL_SHIFT) &		\
244 				AFMT_CHANNEL_MASK) |			\
245 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
246 				AFMT_EXTCHANNEL_MASK))
247 
248 #define AFMT_U8_NE	AFMT_U8
249 #define AFMT_S8_NE	AFMT_S8
250 
251 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
252 
253 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
254 			 AFMT_U24_NE | AFMT_U32_NE)
255 
256 /*
257  * Minor numbers for the sound driver.
258  *
259  * Unfortunately Creative called the codec chip of SB as a DSP. For this
260  * reason the /dev/dsp is reserved for digitized audio use. There is a
261  * device for true DSP processors but it will be called something else.
262  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
263  */
264 
265 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
266 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
267 #define SND_DEV_MIDIN	2	/* Raw midi access */
268 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
269 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
270 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
271 #define SND_DEV_STATUS	6	/* /dev/sndstat */
272 				/* #7 not in use now. */
273 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
274 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
275 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
276 #define SND_DEV_NORESET	10
277 
278 #define SND_DEV_DSPHW_PLAY	11	/* specific playback channel */
279 #define SND_DEV_DSPHW_VPLAY	12	/* specific virtual playback channel */
280 #define SND_DEV_DSPHW_REC	13	/* specific record channel */
281 #define SND_DEV_DSPHW_VREC	14	/* specific virtual record channel */
282 
283 #define SND_DEV_DSPHW_CD	15	/* s16le/stereo 44100Hz CD */
284 
285 /*
286  * OSSv4 compatible device. For now, it serve no purpose and
287  * the cloning itself will forward the request to ordinary /dev/dsp
288  * instead.
289  */
290 #define SND_DEV_DSP_MMAP	16	/* /dev/dsp_mmap     */
291 #define SND_DEV_DSP_AC3		17	/* /dev/dsp_ac3      */
292 #define SND_DEV_DSP_MULTICH	18	/* /dev/dsp_multich  */
293 #define SND_DEV_DSP_SPDIFOUT	19	/* /dev/dsp_spdifout */
294 #define SND_DEV_DSP_SPDIFIN	20	/* /dev/dsp_spdifin  */
295 
296 #define SND_DEV_LAST		SND_DEV_DSP_SPDIFIN
297 #define SND_DEV_MAX		PCMMAXDEV
298 
299 #define DSP_DEFAULT_SPEED	8000
300 
301 #define ON		1
302 #define OFF		0
303 
304 extern int pcm_veto_load;
305 extern int snd_unit;
306 extern int snd_maxautovchans;
307 extern int snd_verbose;
308 extern devclass_t pcm_devclass;
309 extern struct unrhdr *pcmsg_unrhdr;
310 
311 /*
312  * some macros for debugging purposes
313  * DDB/DEB to enable/disable debugging stuff
314  * BVDDB   to enable debugging when bootverbose
315  */
316 #define BVDDB(x) if (bootverbose) x
317 
318 #ifndef DEB
319 #define DEB(x)
320 #endif
321 
322 SYSCTL_DECL(_hw_snd);
323 
324 int pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num);
325 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
326     pid_t pid, char *comm, int devunit);
327 int pcm_chnrelease(struct pcm_channel *c);
328 int pcm_chnref(struct pcm_channel *c, int ref);
329 int pcm_inprog(struct snddev_info *d, int delta);
330 
331 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
332 int pcm_chn_destroy(struct pcm_channel *ch);
333 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
334 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
335 
336 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
337 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
338 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
339 int pcm_unregister(device_t dev);
340 int pcm_setstatus(device_t dev, char *str);
341 u_int32_t pcm_getflags(device_t dev);
342 void pcm_setflags(device_t dev, u_int32_t val);
343 void *pcm_getdevinfo(device_t dev);
344 
345 
346 int snd_setup_intr(device_t dev, struct resource *res, int flags,
347 		   driver_intr_t hand, void *param, void **cookiep);
348 
349 void *snd_mtxcreate(const char *desc, const char *type);
350 void snd_mtxfree(void *m);
351 void snd_mtxassert(void *m);
352 #define	snd_mtxlock(m) mtx_lock(m)
353 #define	snd_mtxunlock(m) mtx_unlock(m)
354 
355 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
356 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
357 int sndstat_registerfile(char *str);
358 int sndstat_unregister(device_t dev);
359 int sndstat_unregisterfile(char *str);
360 
361 #define SND_DECLARE_FILE(version) \
362 	_SND_DECLARE_FILE(__LINE__, version)
363 
364 #define _SND_DECLARE_FILE(uniq, version) \
365 	__SND_DECLARE_FILE(uniq, version)
366 
367 #define __SND_DECLARE_FILE(uniq, version) \
368 	static char sndstat_vinfo[] = version; \
369 	SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
370 	SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
371 
372 /* usage of flags in device config entry (config file) */
373 #define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
374 #define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
375 
376 /* ought to be made obsolete but still used by mss */
377 #define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
378 #define	DV_F_DEV_SHIFT	8		/* force device type/class */
379 
380 /*
381  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
382  * so that the macro versions of pcm_{,un}lock can dereference them.
383  * we also have to do this now makedev() has gone away.
384  */
385 
386 struct snddev_info {
387 	struct {
388 		struct {
389 			SLIST_HEAD(, pcm_channel) head;
390 			struct {
391 				SLIST_HEAD(, pcm_channel) head;
392 			} busy;
393 			struct {
394 				SLIST_HEAD(, pcm_channel) head;
395 			} opened;
396 		} pcm;
397 	} channels;
398 	TAILQ_HEAD(dsp_cdevinfo_linkhead, dsp_cdevinfo) dsp_cdevinfo_pool;
399 	struct snd_clone *clones;
400 	unsigned devcount, playcount, reccount, pvchancount, rvchancount ;
401 	unsigned flags;
402 	int inprog;
403 	unsigned int bufsz;
404 	void *devinfo;
405 	device_t dev;
406 	char status[SND_STATUSLEN];
407 	struct mtx *lock;
408 	struct cdev *mixer_dev;
409 	uint32_t pvchanrate, pvchanformat;
410 	uint32_t rvchanrate, rvchanformat;
411 	int32_t eqpreamp;
412 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
413 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
414 	struct cv cv;
415 };
416 
417 void	sound_oss_sysinfo(oss_sysinfo *);
418 int	sound_oss_card_info(oss_card_info *);
419 
420 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
421 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
422 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
423 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
424 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
425 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
426 
427 /*
428  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
429  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
430  */
431 #ifdef SND_DIAGNOSTIC
432 #define PCM_WAIT(x)		do {					\
433 	if (!PCM_LOCKOWNED(x))						\
434 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
435 		    __func__, __LINE__);				\
436 	while ((x)->flags & SD_F_BUSY) {				\
437 		if (snd_verbose > 3)					\
438 			device_printf((x)->dev,				\
439 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
440 			    __func__, __LINE__);			\
441 		cv_wait(&(x)->cv, (x)->lock);				\
442 	}								\
443 } while (0)
444 
445 #define PCM_ACQUIRE(x)		do {					\
446 	if (!PCM_LOCKOWNED(x))						\
447 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
448 		    __func__, __LINE__);				\
449 	if ((x)->flags & SD_F_BUSY)					\
450 		panic("%s(%d): [PCM ACQUIRE] "				\
451 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
452 	(x)->flags |= SD_F_BUSY;					\
453 } while (0)
454 
455 #define PCM_RELEASE(x)		do {					\
456 	if (!PCM_LOCKOWNED(x))						\
457 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
458 		    __func__, __LINE__);				\
459 	if ((x)->flags & SD_F_BUSY) {					\
460 		(x)->flags &= ~SD_F_BUSY;				\
461 		if ((x)->cv.cv_waiters != 0) {				\
462 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
463 				device_printf((x)->dev,			\
464 				    "%s(%d): [PCM RELEASE] "		\
465 				    "cv_waiters=%d > 1!\n",		\
466 				    __func__, __LINE__,			\
467 				    (x)->cv.cv_waiters);		\
468 			cv_broadcast(&(x)->cv);				\
469 		}							\
470 	} else								\
471 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
472 		    __func__, __LINE__);				\
473 } while (0)
474 
475 /* Quick version, for shorter path. */
476 #define PCM_ACQUIRE_QUICK(x)	do {					\
477 	if (PCM_LOCKOWNED(x))						\
478 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
479 		    __func__, __LINE__);				\
480 	PCM_LOCK(x);							\
481 	PCM_WAIT(x);							\
482 	PCM_ACQUIRE(x);							\
483 	PCM_UNLOCK(x);							\
484 } while (0)
485 
486 #define PCM_RELEASE_QUICK(x)	do {					\
487 	if (PCM_LOCKOWNED(x))						\
488 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
489 		    __func__, __LINE__);				\
490 	PCM_LOCK(x);							\
491 	PCM_RELEASE(x);							\
492 	PCM_UNLOCK(x);							\
493 } while (0)
494 
495 #define PCM_BUSYASSERT(x)	do {					\
496 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
497 		panic("%s(%d): [PCM BUSYASSERT] "			\
498 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
499 } while (0)
500 
501 #define PCM_GIANT_ENTER(x)	do {					\
502 	int _pcm_giant = 0;						\
503 	if (PCM_LOCKOWNED(x))						\
504 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
505 		    __func__, __LINE__);				\
506 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
507 		device_printf((x)->dev,					\
508 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
509 		    __func__, __LINE__);				\
510 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
511 		do {							\
512 			mtx_lock(&Giant);				\
513 			_pcm_giant = 1;					\
514 		} while (0)
515 
516 #define PCM_GIANT_EXIT(x)	do {					\
517 	if (PCM_LOCKOWNED(x))						\
518 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
519 		    __func__, __LINE__);				\
520 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
521 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
522 		    __func__, __LINE__);				\
523 	if ((x)->flags & SD_F_MPSAFE) {					\
524 		if (_pcm_giant == 1)					\
525 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
526 			    __func__, __LINE__);			\
527 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
528 			device_printf((x)->dev,				\
529 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
530 			    __func__, __LINE__);			\
531 	}								\
532 	if (_pcm_giant != 0) {						\
533 		if (mtx_owned(&Giant) == 0)				\
534 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
535 			    __func__, __LINE__);			\
536 		_pcm_giant = 0;						\
537 		mtx_unlock(&Giant);					\
538 	}								\
539 } while (0)
540 #else /* SND_DIAGNOSTIC */
541 #define PCM_WAIT(x)		do {					\
542 	PCM_LOCKASSERT(x);						\
543 	while ((x)->flags & SD_F_BUSY)					\
544 		cv_wait(&(x)->cv, (x)->lock);				\
545 } while (0)
546 
547 #define PCM_ACQUIRE(x)		do {					\
548 	PCM_LOCKASSERT(x);						\
549 	KASSERT(!((x)->flags & SD_F_BUSY),				\
550 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
551 	    __func__, __LINE__));					\
552 	(x)->flags |= SD_F_BUSY;					\
553 } while (0)
554 
555 #define PCM_RELEASE(x)		do {					\
556 	PCM_LOCKASSERT(x);						\
557 	KASSERT((x)->flags & SD_F_BUSY,					\
558 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
559 	    __func__, __LINE__));					\
560 	(x)->flags &= ~SD_F_BUSY;					\
561 	if ((x)->cv.cv_waiters != 0)					\
562 		cv_broadcast(&(x)->cv);					\
563 } while (0)
564 
565 /* Quick version, for shorter path. */
566 #define PCM_ACQUIRE_QUICK(x)	do {					\
567 	PCM_UNLOCKASSERT(x);						\
568 	PCM_LOCK(x);							\
569 	PCM_WAIT(x);							\
570 	PCM_ACQUIRE(x);							\
571 	PCM_UNLOCK(x);							\
572 } while (0)
573 
574 #define PCM_RELEASE_QUICK(x)	do {					\
575 	PCM_UNLOCKASSERT(x);						\
576 	PCM_LOCK(x);							\
577 	PCM_RELEASE(x);							\
578 	PCM_UNLOCK(x);							\
579 } while (0)
580 
581 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
582 				    ((x)->flags & SD_F_BUSY),		\
583 				    ("%s(%d): [PCM BUSYASSERT] "	\
584 				    "Failed, snddev_info=%p",		\
585 				    __func__, __LINE__, x))
586 
587 #define PCM_GIANT_ENTER(x)	do {					\
588 	int _pcm_giant = 0;						\
589 	PCM_UNLOCKASSERT(x);						\
590 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
591 		do {							\
592 			mtx_lock(&Giant);				\
593 			_pcm_giant = 1;					\
594 		} while (0)
595 
596 #define PCM_GIANT_EXIT(x)	do {					\
597 	PCM_UNLOCKASSERT(x);						\
598 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
599 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
600 	    __func__, __LINE__));					\
601 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
602 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
603 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
604 	    __func__, __LINE__));					\
605 	if (_pcm_giant != 0) {						\
606 		mtx_assert(&Giant, MA_OWNED);				\
607 		_pcm_giant = 0;						\
608 		mtx_unlock(&Giant);					\
609 	}								\
610 } while (0)
611 #endif /* !SND_DIAGNOSTIC */
612 
613 #define PCM_GIANT_LEAVE(x)						\
614 	PCM_GIANT_EXIT(x);						\
615 } while (0)
616 
617 #ifdef KLD_MODULE
618 #define PCM_KLDSTRING(a) ("kld " # a)
619 #else
620 #define PCM_KLDSTRING(a) ""
621 #endif
622 
623 #endif /* _KERNEL */
624 
625 #endif	/* _OS_H_ */
626